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