File: ipabase.py

package info (click to toggle)
freeipa 4.12.4-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 100,668 kB
  • sloc: python: 298,952; javascript: 71,606; ansic: 49,369; sh: 6,547; makefile: 2,553; xml: 343; sed: 16
file content (177 lines) | stat: -rw-r--r-- 4,561 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#
# Copyright (C) 2020  FreeIPA Contributors see COPYING for license
#
"""IPA API initialization for Sphinx
"""
import os
import re
import sys

try:
    from sphinx.util.display import progress_message
except ImportError:
    # sphinx < 6.1.0
    from sphinx.util import progress_message
from sphinx.ext.autodoc import mock as autodoc_mock

HERE = os.path.dirname(os.path.abspath(__file__))
ROOT = os.path.abspath(os.path.join(HERE, os.pardir))
VERSION_M4 = os.path.abspath(os.path.join(ROOT, "VERSION.m4"))

if ROOT not in sys.path:
    sys.path.insert(0, ROOT)


ipa_mock_imports = [
    # no binary wheels available
    "dbus",
    "gssapi",
    "ldap",
    "ldif",  # python-ldap
    "ldapurl",  # python-ldap
    # dogtag-pki is client-only
    "pki",
    # PyPI packages not available
    "pyhbac",
    "pysss",
    "pysss_murmur",
    "pysss_nss_idmap",
    "samba",
    "SSSDConfig",
]


def parse_version_m4(filename=VERSION_M4):
    """Poor man's macro parser for VERSION.m4
    """
    def_re = re.compile(r"^define\(([\w]+)+,\s*(.*)\)\s*$")
    defs = {}

    with open(filename) as f:
        for line in f:
            mo = def_re.match(line)
            if mo is not None:
                k, v = mo.groups()
                try:
                    v = int(v)
                except ValueError:
                    pass
                defs[k] = v

    defs["IPA_NUM_VERSION"] = (
        "{IPA_VERSION_MAJOR:d}"
        "{IPA_VERSION_MINOR:02d}"
        "{IPA_VERSION_RELEASE:02d}"
    ).format(**defs)

    defs["IPA_API_VERSION"] = (
        "{IPA_API_VERSION_MAJOR}.{IPA_API_VERSION_MINOR}"
    ).format(**defs)

    if defs["IPA_VERSION_IS_GIT_SNAPSHOT"] == "yes":
        defs["IPA_GIT_VERSION"] = ".dev"
    else:
        defs["IPA_GIT_VERSION"] = ""

    defs["IPA_VERSION"] = (
        "{IPA_VERSION_MAJOR}."
        "{IPA_VERSION_MINOR}."
        "{IPA_VERSION_RELEASE}"
        "{IPA_VERSION_PRE_RELEASE}"
        "{IPA_GIT_VERSION}"
    ).format(**defs)
    return defs


def fake_ipaython_version(defs):
    """Fake ipapython.version module

    We don't want and cannot run autoconf on read the docs. Fake the auto-
    generated ipapython.version module.
    """

    class FakeIpapythonVersion:
        __name__ = "ipapython.version"

        VERSION = defs["IPA_VERSION"]
        VENDOR_VERSION = defs["IPA_VERSION"]
        NUM_VERSION = defs["IPA_NUM_VERSION"]
        API_VERSION = defs["IPA_API_VERSION"]
        DEFAULT_PLUGINS = frozenset()

    fake = FakeIpapythonVersion()
    sys.modules[fake.__name__] = fake


def init_api(
    context="doc",
    domain="ipa.example",
    server="server.ipa.example",
    in_server=True,
):
    import ipalib

    ipalib.api.bootstrap(
        context=context,
        in_server=in_server,
        logdir=None,
        log=None,
        domain=domain,
        realm=domain.upper(),
        server=server,
    )
    ipalib.api.finalize()
    return ipalib.api


def inject_mock_imports(app, config):
    """Add additional module mocks for ipaserver
    """
    mock_imports = set(getattr(config, "autodoc_mock_imports", []))
    mock_imports.update(ipa_mock_imports)
    config.autodoc_mock_imports = list(mock_imports)

    # ldap is a mocked package
    # ensure that ipapython.dn still use ctypes wrappers for str2dn/dn2str
    # otherwise api won't be able to initialize properly
    import ipapython.dn

    assert ipapython.dn.str2dn("cn=ipa") == [[("cn", "ipa", 1)]]


def init_ipalib_api(app, config):
    """Initialize ipalib.api

    1. Parse VERSION.m4
    2. Create fake ipapython.version module
    3. Initialize the API with mocked imports
    """
    defs = parse_version_m4()
    fake_ipaython_version(defs)

    with progress_message("initializing ipalib.api"):
        with autodoc_mock(config.autodoc_mock_imports):
            init_api(
                context=config.ipa_context,
                domain=config.ipa_domain,
                server=config.ipa_server_fqdn,
                in_server=config.ipa_in_server,
            )


def setup(app):
    app.setup_extension("sphinx.ext.autodoc")

    app.add_config_value("ipa_context", "doc", "env")
    app.add_config_value("ipa_domain", "ipa.example", "env")
    app.add_config_value("ipa_server_fqdn", "server.ipa.example", "env")
    app.add_config_value("ipa_in_server", True, "env")

    app.connect("config-inited", inject_mock_imports)
    app.connect("config-inited", init_ipalib_api)

    return {
        "version": "0.1",
        "parallel_read_safe": True,
        "parallel_write_safe": True,
    }