make-directive-index.py 8.1 KB
Newer Older
1 2 3 4
#  -*- Mode: python; coding: utf-8; indent-tabs-mode: nil -*- */
#
#  This file is part of systemd.
#
5
#  Copyright 2012-2013 Zbigniew Jędrzejewski-Szmek
6 7 8 9 10 11 12 13 14 15 16 17 18 19
#
#  systemd is free software; you can redistribute it and/or modify it
#  under the terms of the GNU Lesser General Public License as published by
#  the Free Software Foundation; either version 2.1 of the License, or
#  (at your option) any later version.
#
#  systemd is distributed in the hope that it will be useful, but
#  WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
#  Lesser General Public License for more details.
#
#  You should have received a copy of the GNU Lesser General Public License
#  along with systemd; If not, see <http://www.gnu.org/licenses/>.

20 21 22
import sys
import collections
import xml.etree.ElementTree as tree
23
import re
24 25

TEMPLATE = '''\
26
<refentry id="systemd.directives" conditional="HAVE_PYTHON">
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43

        <refentryinfo>
                <title>systemd.directives</title>
                <productname>systemd</productname>

                <authorgroup>
                        <author>
                                <contrib>Developer</contrib>
                                <firstname>Zbigniew</firstname>
                                <surname>Jędrzejewski-Szmek</surname>
                                <email>zbyszek@in.waw.pl</email>
                        </author>
                </authorgroup>
        </refentryinfo>

        <refmeta>
                <refentrytitle>systemd.directives</refentrytitle>
44
                <manvolnum>7</manvolnum>
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
        </refmeta>

        <refnamediv>
                <refname>systemd.directives</refname>
                <refpurpose>Index of configuration directives</refpurpose>
        </refnamediv>

        <refsect1>
                <title>Unit directives</title>

                <para>Directives for configuring units, used in unit
                files.</para>

                <variablelist id='unit-directives' />
        </refsect1>
60

61 62 63 64 65 66
        <refsect1>
                <title>Options on the kernel command line</title>

                <para>Kernel boot options for configuring the behaviour of the
                systemd process.</para>

67
                <variablelist id='kernel-commandline-options' />
68 69 70 71 72
        </refsect1>

        <refsect1>
                <title>Environment variables</title>

73 74
                <para>Environment variables understood by the systemd
                manager and other programs.</para>
75 76 77 78

                <variablelist id='environment-variables' />
        </refsect1>

79 80 81 82 83 84 85 86
        <refsect1>
                <title>UDEV directives</title>

                <para>Directives for configuring systemd units through the
                udev database.</para>

                <variablelist id='udev-directives' />
        </refsect1>
87 88

        <refsect1>
89
                <title>Journal fields</title>
90

91
                <para>Fields in the journal events with a well known meaning.</para>
92 93 94

                <variablelist id='journal-directives' />
        </refsect1>
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
        <refsect1>
                <title>PAM configuration directives</title>

                <para>Directives for configuring PAM behaviour.</para>

                <variablelist id='pam-directives' />
        </refsect1>

        <refsect1>
                <title>crypttab options</title>

                <para>Options which influence mounted filesystems and
                encrypted volumes.</para>

                <variablelist id='crypttab-options' />
        </refsect1>

        <refsect1>
                <title>System manager directives</title>

                <para>Directives for configuring the behaviour of the
                systemd process.</para>

                <variablelist id='systemd-directives' />
        </refsect1>

122 123 124 125 126 127 128 129
        <refsect1>
                <title>bootchart.conf directives</title>

                <para>Directives for configuring the behaviour of the
                systemd-bootchart process.</para>

                <variablelist id='bootchart-directives' />
        </refsect1>
130

131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
        <refsect1>
                <title>command-line options</title>

                <para>Command-line options accepted by programs in the
                systemd suite.</para>

                <variablelist id='options' />
        </refsect1>

        <refsect1>
                <title>Miscellaneous options and directives</title>

                <para>Other configuration elements which don't fit in
                any of the above groups.</para>

                <variablelist id='miscellaneous' />
        </refsect1>

149 150 151 152
        <refsect1>
                <title>Colophon</title>
                <para id='colophon' />
        </refsect1>
153 154 155
</refentry>
'''

156 157 158 159 160
COLOPHON = '''\
This index contains {count} entries in {sections} sections,
referring to {pages} individual manual pages.
'''

161
def _extract_directives(directive_groups, formatting, page):
162 163 164 165
    t = tree.parse(page)
    section = t.find('./refmeta/manvolnum').text
    pagename = t.find('./refmeta/refentrytitle').text
    for variablelist in t.iterfind('.//variablelist'):
166 167 168 169 170 171 172 173 174 175
        klass = variablelist.attrib.get('class')
        storvar = directive_groups[klass or 'miscellaneous']
        storopt = directive_groups['options']
        # <option>s go in OPTIONS, unless class is specified
        for xpath, stor in (('./varlistentry/term/varname', storvar),
                            ('./varlistentry/term/option',
                             storvar if klass else storopt)):
            for name in variablelist.iterfind(xpath):
                text = re.sub(r'([= ]).*', r'\1', name.text).rstrip()
                stor[text].append((pagename, section))
176 177 178 179 180
                if text not in formatting:
                    # use element as formatted display
                    name.tail = ''
                    name.text = text
                    formatting[text] = name
181

182
def _make_section(template, name, directives, formatting):
183
    varlist = template.find(".//*[@id='{}']".format(name))
184 185
    for varname, manpages in sorted(directives.items()):
        entry = tree.SubElement(varlist, 'varlistentry')
186 187 188
        term = tree.SubElement(entry, 'term')
        term.append(formatting[varname])

189 190 191
        para = tree.SubElement(tree.SubElement(entry, 'listitem'), 'para')

        b = None
192
        for manpage, manvolume in sorted(set(manpages)):
193 194 195 196 197 198 199 200 201
                if b is not None:
                        b.tail = ', '
                b = tree.SubElement(para, 'citerefentry')
                c = tree.SubElement(b, 'refentrytitle')
                c.text = manpage
                d = tree.SubElement(b, 'manvolnum')
                d.text = manvolume
        entry.tail = '\n\n'

202 203 204 205 206 207 208 209 210 211 212 213 214
def _make_colophon(template, groups):
    count = 0
    pages = set()
    for group in groups:
        count += len(group)
        for pagelist in group.values():
            pages |= set(pagelist)

    para = template.find(".//para[@id='colophon']")
    para.text = COLOPHON.format(count=count,
                                sections=len(groups),
                                pages=len(pages))

215
def _make_page(template, directive_groups, formatting):
216 217 218 219 220 221 222 223 224
    """Create an XML tree from directive_groups.

    directive_groups = {
       'class': {'variable': [('manpage', 'manvolume'), ...],
                 'variable2': ...},
       ...
    }
    """
    for name, directives in directive_groups.items():
225
            _make_section(template, name, directives, formatting)
226

227 228
    _make_colophon(template, directive_groups.values())

229
    return template
230

231
def make_page(*xml_files):
232
    "Extract directives from xml_files and return XML index tree."
233 234
    template = tree.fromstring(TEMPLATE)
    names = [vl.get('id') for vl in template.iterfind('.//variablelist')]
235
    directive_groups = {name:collections.defaultdict(list)
236
                        for name in names}
237
    formatting = {}
238
    for page in xml_files:
239
        try:
240
            _extract_directives(directive_groups, formatting, page)
241 242
        except Exception:
            raise ValueError("failed to process " + page)
243

244
    return _make_page(template, directive_groups, formatting)
245 246

if __name__ == '__main__':
247
    tree.dump(make_page(*sys.argv[1:]))