chiark / gitweb /
build-sys: add silent rules for gperf generation
[elogind.git] / make-directive-index.py
1 #  -*- Mode: python; coding: utf-8; indent-tabs-mode: nil -*- */
2 #
3 #  This file is part of systemd.
4 #
5 #  Copyright 2012 Zbigniew JÄ™drzejewski-Szmek
6 #
7 #  systemd is free software; you can redistribute it and/or modify it
8 #  under the terms of the GNU Lesser General Public License as published by
9 #  the Free Software Foundation; either version 2.1 of the License, or
10 #  (at your option) any later version.
11 #
12 #  systemd is distributed in the hope that it will be useful, but
13 #  WITHOUT ANY WARRANTY; without even the implied warranty of
14 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 #  Lesser General Public License for more details.
16 #
17 #  You should have received a copy of the GNU Lesser General Public License
18 #  along with systemd; If not, see <http://www.gnu.org/licenses/>.
19
20 import sys
21 import collections
22 import xml.etree.ElementTree as tree
23
24 TEMPLATE = '''\
25 <refentry id="systemd.directives">
26
27         <refentryinfo>
28                 <title>systemd.directives</title>
29                 <productname>systemd</productname>
30
31                 <authorgroup>
32                         <author>
33                                 <contrib>Developer</contrib>
34                                 <firstname>Zbigniew</firstname>
35                                 <surname>JÄ™drzejewski-Szmek</surname>
36                                 <email>zbyszek@in.waw.pl</email>
37                         </author>
38                 </authorgroup>
39         </refentryinfo>
40
41         <refmeta>
42                 <refentrytitle>systemd.directives</refentrytitle>
43                 <manvolnum>7</manvolnum>
44         </refmeta>
45
46         <refnamediv>
47                 <refname>systemd.directives</refname>
48                 <refpurpose>Index of configuration directives</refpurpose>
49         </refnamediv>
50
51         <refsect1>
52                 <title>Unit directives</title>
53
54                 <para>Directives for configuring units, used in unit
55                 files.</para>
56
57                 <variablelist id='unit-directives' />
58         </refsect1>
59
60         <refsect1>
61                 <title>System manager directives</title>
62
63                 <para>Directives for configuring the behaviour of the
64                 systemd process.</para>
65
66                 <variablelist id='systemd-directives' />
67         </refsect1>
68
69         <refsect1>
70                 <title>Options on the kernel command line</title>
71
72                 <para>Kernel boot options for configuring the behaviour of the
73                 systemd process.</para>
74
75                 <variablelist id='kernel-commandline-directives' />
76         </refsect1>
77
78         <refsect1>
79                 <title>Environment variables</title>
80
81                 <para>Environment variables understood by the systemd process.</para>
82
83                 <variablelist id='environment-variables' />
84         </refsect1>
85
86         <refsect1>
87                 <title>UDEV directives</title>
88
89                 <para>Directives for configuring systemd units through the
90                 udev database.</para>
91
92                 <variablelist id='udev-directives' />
93         </refsect1>
94
95         <refsect1>
96                 <title>Journal directives</title>
97
98                 <para>Directives for configuring the behaviour of the
99                 journald process.</para>
100
101                 <variablelist id='journal-directives' />
102         </refsect1>
103
104         <refsect1>
105                 <title>bootchart.conf directives</title>
106
107                 <para>Directives for configuring the behaviour of the
108                 systemd-bootchart process.</para>
109
110                 <variablelist id='bootchart-directives' />
111         </refsect1>
112
113         <refsect1>
114                 <title>Colophon</title>
115                 <para id='colophon' />
116         </refsect1>
117 </refentry>
118 '''
119
120 COLOPHON = '''\
121 This index contains {count} entries in {sections} sections,
122 referring to {pages} individual manual pages.
123 '''
124
125 def _extract_directives(directive_groups, page):
126     t = tree.parse(page)
127     section = t.find('./refmeta/manvolnum').text
128     pagename = t.find('./refmeta/refentrytitle').text
129     for variablelist in t.iterfind('.//variablelist'):
130         klass = variablelist.attrib.get('class') or 'unit-directives'
131         stor = directive_groups[klass]
132         for varname in variablelist.iterfind('./varlistentry/term/varname'):
133             text = ''.join(varname.text.partition('=')[:2])
134             stor[text].append((pagename, section))
135
136 def _make_section(template, name, directives):
137     varlist = template.find(".//*[@id='{}']".format(name))
138     for varname, manpages in sorted(directives.items()):
139         entry = tree.SubElement(varlist, 'varlistentry')
140         a = tree.SubElement(tree.SubElement(entry, 'term'), 'varname')
141         a.text = varname
142         para = tree.SubElement(tree.SubElement(entry, 'listitem'), 'para')
143
144         b = None
145         for manpage, manvolume in sorted(manpages):
146                 if b is not None:
147                         b.tail = ', '
148                 b = tree.SubElement(para, 'citerefentry')
149                 c = tree.SubElement(b, 'refentrytitle')
150                 c.text = manpage
151                 d = tree.SubElement(b, 'manvolnum')
152                 d.text = manvolume
153         entry.tail = '\n\n'
154
155 def _make_colophon(template, groups):
156     count = 0
157     pages = set()
158     for group in groups:
159         count += len(group)
160         for pagelist in group.values():
161             pages |= set(pagelist)
162
163     para = template.find(".//para[@id='colophon']")
164     para.text = COLOPHON.format(count=count,
165                                 sections=len(groups),
166                                 pages=len(pages))
167
168 def _make_page(template, directive_groups):
169     """Create an XML tree from directive_groups.
170
171     directive_groups = {
172        'class': {'variable': [('manpage', 'manvolume'), ...],
173                  'variable2': ...},
174        ...
175     }
176     """
177     for name, directives in directive_groups.items():
178             _make_section(template, name, directives)
179
180     _make_colophon(template, directive_groups.values())
181
182     return template
183
184 def make_page(xml_files):
185     "Extract directives from xml_files and return XML index tree."
186     template = tree.fromstring(TEMPLATE)
187     names = [vl.get('id') for vl in template.iterfind('.//variablelist')]
188     directive_groups = {name:collections.defaultdict(list)
189                         for name in names}
190     for page in xml_files:
191         _extract_directives(directive_groups, page)
192
193     return _make_page(template, directive_groups)
194
195 if __name__ == '__main__':
196     tree.dump(make_page(sys.argv[1:]))