]> git.ipfire.org Git - thirdparty/systemd.git/blob - tools/make-directive-index.py
Merge pull request #6365 from keszybz/fast-tests
[thirdparty/systemd.git] / tools / make-directive-index.py
1 #!/usr/bin/env python3
2 # -*- Mode: python; coding: utf-8; indent-tabs-mode: nil -*- */
3 #
4 # This file is part of systemd.
5 #
6 # Copyright 2012-2013 Zbigniew Jędrzejewski-Szmek
7 #
8 # systemd is free software; you can redistribute it and/or modify it
9 # under the terms of the GNU Lesser General Public License as published by
10 # the Free Software Foundation; either version 2.1 of the License, or
11 # (at your option) any later version.
12 #
13 # systemd is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 # Lesser General Public License for more details.
17 #
18 # You should have received a copy of the GNU Lesser General Public License
19 # along with systemd; If not, see <http://www.gnu.org/licenses/>.
20
21 import sys
22 import collections
23 import re
24 from xml_helper import xml_parse, xml_print, tree
25 from copy import deepcopy
26
27 TEMPLATE = '''\
28 <refentry id="systemd.directives" conditional="HAVE_PYTHON">
29
30 <refentryinfo>
31 <title>systemd.directives</title>
32 <productname>systemd</productname>
33
34 <authorgroup>
35 <author>
36 <contrib>Developer</contrib>
37 <firstname>Zbigniew</firstname>
38 <surname>Jędrzejewski-Szmek</surname>
39 <email>zbyszek@in.waw.pl</email>
40 </author>
41 </authorgroup>
42 </refentryinfo>
43
44 <refmeta>
45 <refentrytitle>systemd.directives</refentrytitle>
46 <manvolnum>7</manvolnum>
47 </refmeta>
48
49 <refnamediv>
50 <refname>systemd.directives</refname>
51 <refpurpose>Index of configuration directives</refpurpose>
52 </refnamediv>
53
54 <refsect1>
55 <title>Unit directives</title>
56
57 <para>Directives for configuring units, used in unit
58 files.</para>
59
60 <variablelist id='unit-directives' />
61 </refsect1>
62
63 <refsect1>
64 <title>Options on the kernel command line</title>
65
66 <para>Kernel boot options for configuring the behaviour of the
67 systemd process.</para>
68
69 <variablelist id='kernel-commandline-options' />
70 </refsect1>
71
72 <refsect1>
73 <title>Environment variables</title>
74
75 <para>Environment variables understood by the systemd
76 manager and other programs.</para>
77
78 <variablelist id='environment-variables' />
79 </refsect1>
80
81 <refsect1>
82 <title>UDEV directives</title>
83
84 <para>Directives for configuring systemd units through the
85 udev database.</para>
86
87 <variablelist id='udev-directives' />
88 </refsect1>
89
90 <refsect1>
91 <title>Network directives</title>
92
93 <para>Directives for configuring network links through the
94 net-setup-link udev builtin and networks through
95 systemd-networkd.</para>
96
97 <variablelist id='network-directives' />
98 </refsect1>
99
100 <refsect1>
101 <title>Journal fields</title>
102
103 <para>Fields in the journal events with a well known meaning.</para>
104
105 <variablelist id='journal-directives' />
106 </refsect1>
107
108 <refsect1>
109 <title>PAM configuration directives</title>
110
111 <para>Directives for configuring PAM behaviour.</para>
112
113 <variablelist id='pam-directives' />
114 </refsect1>
115
116 <refsect1>
117 <title><filename>/etc/crypttab</filename> and
118 <filename>/etc/fstab</filename> options</title>
119
120 <para>Options which influence mounted filesystems and
121 encrypted volumes.</para>
122
123 <variablelist id='fstab-options' />
124 </refsect1>
125
126 <refsect1>
127 <title>System manager directives</title>
128
129 <para>Directives for configuring the behaviour of the
130 systemd process.</para>
131
132 <variablelist id='systemd-directives' />
133 </refsect1>
134
135 <refsect1>
136 <title>command line options</title>
137
138 <para>Command-line options accepted by programs in the
139 systemd suite.</para>
140
141 <variablelist id='options' />
142 </refsect1>
143
144 <refsect1>
145 <title>Constants</title>
146
147 <para>Various constant used and/or defined by systemd.</para>
148
149 <variablelist id='constants' />
150 </refsect1>
151
152 <refsect1>
153 <title>Miscellaneous options and directives</title>
154
155 <para>Other configuration elements which don't fit in
156 any of the above groups.</para>
157
158 <variablelist id='miscellaneous' />
159 </refsect1>
160
161 <refsect1>
162 <title>Files and directories</title>
163
164 <para>Paths and file names referred to in the
165 documentation.</para>
166
167 <variablelist id='filenames' />
168 </refsect1>
169
170 <refsect1>
171 <title>Colophon</title>
172 <para id='colophon' />
173 </refsect1>
174 </refentry>
175 '''
176
177 COLOPHON = '''\
178 This index contains {count} entries in {sections} sections,
179 referring to {pages} individual manual pages.
180 '''
181
182 def _extract_directives(directive_groups, formatting, page):
183 t = xml_parse(page)
184 section = t.find('./refmeta/manvolnum').text
185 pagename = t.find('./refmeta/refentrytitle').text
186
187 storopt = directive_groups['options']
188 for variablelist in t.iterfind('.//variablelist'):
189 klass = variablelist.attrib.get('class')
190 storvar = directive_groups[klass or 'miscellaneous']
191 # <option>s go in OPTIONS, unless class is specified
192 for xpath, stor in (('./varlistentry/term/varname', storvar),
193 ('./varlistentry/term/option',
194 storvar if klass else storopt)):
195 for name in variablelist.iterfind(xpath):
196 text = re.sub(r'([= ]).*', r'\1', name.text).rstrip()
197 stor[text].append((pagename, section))
198 if text not in formatting:
199 # use element as formatted display
200 if name.text[-1] in '= ':
201 name.clear()
202 else:
203 name.tail = ''
204 name.text = text
205 formatting[text] = name
206
207 storfile = directive_groups['filenames']
208 for xpath, absolute_only in (('.//refsynopsisdiv//filename', False),
209 ('.//refsynopsisdiv//command', False),
210 ('.//filename', True)):
211 for name in t.iterfind(xpath):
212 if absolute_only and not (name.text and name.text.startswith('/')):
213 continue
214 if name.attrib.get('noindex'):
215 continue
216 name.tail = ''
217 if name.text:
218 if name.text.endswith('*'):
219 name.text = name.text[:-1]
220 if not name.text.startswith('.'):
221 text = name.text.partition(' ')[0]
222 if text != name.text:
223 name.clear()
224 name.text = text
225 if text.endswith('/'):
226 text = text[:-1]
227 storfile[text].append((pagename, section))
228 if text not in formatting:
229 # use element as formatted display
230 formatting[text] = name
231 else:
232 text = ' '.join(name.itertext())
233 storfile[text].append((pagename, section))
234 formatting[text] = name
235
236 storfile = directive_groups['constants']
237 for name in t.iterfind('.//constant'):
238 if name.attrib.get('noindex'):
239 continue
240 name.tail = ''
241 if name.text.startswith('('): # a cast, strip it
242 name.text = name.text.partition(' ')[2]
243 storfile[name.text].append((pagename, section))
244 formatting[name.text] = name
245
246 def _make_section(template, name, directives, formatting):
247 varlist = template.find(".//*[@id='{}']".format(name))
248 for varname, manpages in sorted(directives.items()):
249 entry = tree.SubElement(varlist, 'varlistentry')
250 term = tree.SubElement(entry, 'term')
251 display = deepcopy(formatting[varname])
252 term.append(display)
253
254 para = tree.SubElement(tree.SubElement(entry, 'listitem'), 'para')
255
256 b = None
257 for manpage, manvolume in sorted(set(manpages)):
258 if b is not None:
259 b.tail = ', '
260 b = tree.SubElement(para, 'citerefentry')
261 c = tree.SubElement(b, 'refentrytitle')
262 c.text = manpage
263 c.attrib['target'] = varname
264 d = tree.SubElement(b, 'manvolnum')
265 d.text = manvolume
266 entry.tail = '\n\n'
267
268 def _make_colophon(template, groups):
269 count = 0
270 pages = set()
271 for group in groups:
272 count += len(group)
273 for pagelist in group.values():
274 pages |= set(pagelist)
275
276 para = template.find(".//para[@id='colophon']")
277 para.text = COLOPHON.format(count=count,
278 sections=len(groups),
279 pages=len(pages))
280
281 def _make_page(template, directive_groups, formatting):
282 """Create an XML tree from directive_groups.
283
284 directive_groups = {
285 'class': {'variable': [('manpage', 'manvolume'), ...],
286 'variable2': ...},
287 ...
288 }
289 """
290 for name, directives in directive_groups.items():
291 _make_section(template, name, directives, formatting)
292
293 _make_colophon(template, directive_groups.values())
294
295 return template
296
297 def make_page(*xml_files):
298 "Extract directives from xml_files and return XML index tree."
299 template = tree.fromstring(TEMPLATE)
300 names = [vl.get('id') for vl in template.iterfind('.//variablelist')]
301 directive_groups = {name:collections.defaultdict(list)
302 for name in names}
303 formatting = {}
304 for page in xml_files:
305 try:
306 _extract_directives(directive_groups, formatting, page)
307 except Exception:
308 raise ValueError("failed to process " + page)
309
310 return _make_page(template, directive_groups, formatting)
311
312 if __name__ == '__main__':
313 with open(sys.argv[1], 'wb') as f:
314 f.write(xml_print(make_page(*sys.argv[2:])))