]> git.ipfire.org Git - thirdparty/systemd.git/blame - tools/make-directive-index.py
Merge pull request #14105 from keszybz/man-directives-cleanup
[thirdparty/systemd.git] / tools / make-directive-index.py
CommitLineData
3e67e5c9 1#!/usr/bin/env python3
35df7443 2# SPDX-License-Identifier: LGPL-2.1+
e7098b69 3
d9cfd694
ZJS
4import sys
5import collections
ccc9a4f9 6import re
1c6c3ef0 7from xml_helper import xml_parse, xml_print, tree
827f70eb 8from copy import deepcopy
d9cfd694
ZJS
9
10TEMPLATE = '''\
56ba3c78 11<refentry id="systemd.directives" conditional="HAVE_PYTHON">
d9cfd694
ZJS
12
13 <refentryinfo>
14 <title>systemd.directives</title>
15 <productname>systemd</productname>
d9cfd694
ZJS
16 </refentryinfo>
17
18 <refmeta>
19 <refentrytitle>systemd.directives</refentrytitle>
9cc2c8b7 20 <manvolnum>7</manvolnum>
d9cfd694
ZJS
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>
e1abd3ef 36
08177c85
ZJS
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
ccc9a4f9 43 <variablelist id='kernel-commandline-options' />
08177c85
ZJS
44 </refsect1>
45
46 <refsect1>
47 <title>Environment variables</title>
48
833fc980
ZJS
49 <para>Environment variables understood by the systemd manager
50 and other programs and environment variable-compatible settings.</para>
08177c85
ZJS
51
52 <variablelist id='environment-variables' />
53 </refsect1>
54
bc61c2b1
ZJS
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
e1abd3ef
ZJS
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>
f6c2e28b 73
1f06807c
TG
74 <refsect1>
75 <title>Network directives</title>
76
77 <para>Directives for configuring network links through the
46b0925d
TG
78 net-setup-link udev builtin and networks through
79 systemd-networkd.</para>
1f06807c
TG
80
81 <variablelist id='network-directives' />
82 </refsect1>
83
f6c2e28b 84 <refsect1>
ccc9a4f9 85 <title>Journal fields</title>
f6c2e28b 86
ccc9a4f9 87 <para>Fields in the journal events with a well known meaning.</para>
f6c2e28b
ZJS
88
89 <variablelist id='journal-directives' />
90 </refsect1>
4a431c9a 91
ccc9a4f9
ZJS
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>
63b03c0b
ZJS
101 <title><filename>/etc/crypttab</filename> and
102 <filename>/etc/fstab</filename> options</title>
ccc9a4f9
ZJS
103
104 <para>Options which influence mounted filesystems and
105 encrypted volumes.</para>
106
63b03c0b 107 <variablelist id='fstab-options' />
ccc9a4f9
ZJS
108 </refsect1>
109
37dac218
ZJS
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
ccc9a4f9 119 <refsect1>
d2acdcc6 120 <title>Program configuration options</title>
ccc9a4f9
ZJS
121
122 <para>Directives for configuring the behaviour of the
d2acdcc6 123 systemd process and other tools through configuration files.</para>
ccc9a4f9 124
d2acdcc6 125 <variablelist id='config-directives' />
ccc9a4f9
ZJS
126 </refsect1>
127
ccc9a4f9 128 <refsect1>
37dac218 129 <title>Command line options</title>
ccc9a4f9
ZJS
130
131 <para>Command-line options accepted by programs in the
132 systemd suite.</para>
133
134 <variablelist id='options' />
135 </refsect1>
136
785a51eb
ZJS
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
ccc9a4f9
ZJS
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
a4e0b94d
ZJS
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
0acfdd61
ZJS
163 <refsect1>
164 <title>Colophon</title>
165 <para id='colophon' />
166 </refsect1>
d9cfd694
ZJS
167</refentry>
168'''
169
0acfdd61
ZJS
170COLOPHON = '''\
171This index contains {count} entries in {sections} sections,
172referring to {pages} individual manual pages.
173'''
174
d970bd6d 175def _extract_directives(directive_groups, formatting, page):
1a13e31d 176 t = xml_parse(page)
d9cfd694
ZJS
177 section = t.find('./refmeta/manvolnum').text
178 pagename = t.find('./refmeta/refentrytitle').text
a4e0b94d
ZJS
179
180 storopt = directive_groups['options']
d9cfd694 181 for variablelist in t.iterfind('.//variablelist'):
ccc9a4f9
ZJS
182 klass = variablelist.attrib.get('class')
183 storvar = directive_groups[klass or 'miscellaneous']
ccc9a4f9
ZJS
184 # <option>s go in OPTIONS, unless class is specified
185 for xpath, stor in (('./varlistentry/term/varname', storvar),
186 ('./varlistentry/term/option',
187 storvar if klass else storopt)):
188 for name in variablelist.iterfind(xpath):
189 text = re.sub(r'([= ]).*', r'\1', name.text).rstrip()
8c6c56c3
ZJS
190 if text.startswith('-'):
191 # for options, merge options with and without mandatory arg
192 text = text.partition('=')[0]
ccc9a4f9 193 stor[text].append((pagename, section))
d970bd6d
ZJS
194 if text not in formatting:
195 # use element as formatted display
f8b68539 196 if name.text[-1] in "= '":
699ad6c0
ZJS
197 name.clear()
198 else:
199 name.tail = ''
d970bd6d
ZJS
200 name.text = text
201 formatting[text] = name
d9cfd694 202
a4e0b94d 203 storfile = directive_groups['filenames']
845c5324
ZJS
204 for xpath, absolute_only in (('.//refsynopsisdiv//filename', False),
205 ('.//refsynopsisdiv//command', False),
206 ('.//filename', True)):
a4e0b94d 207 for name in t.iterfind(xpath):
845c5324
ZJS
208 if absolute_only and not (name.text and name.text.startswith('/')):
209 continue
b0343f8c 210 if name.attrib.get('index') == 'false':
845c5324 211 continue
a4e0b94d
ZJS
212 name.tail = ''
213 if name.text:
845c5324
ZJS
214 if name.text.endswith('*'):
215 name.text = name.text[:-1]
a4e0b94d
ZJS
216 if not name.text.startswith('.'):
217 text = name.text.partition(' ')[0]
218 if text != name.text:
219 name.clear()
220 name.text = text
845c5324
ZJS
221 if text.endswith('/'):
222 text = text[:-1]
a4e0b94d
ZJS
223 storfile[text].append((pagename, section))
224 if text not in formatting:
225 # use element as formatted display
226 formatting[text] = name
227 else:
228 text = ' '.join(name.itertext())
229 storfile[text].append((pagename, section))
230 formatting[text] = name
231
785a51eb
ZJS
232 storfile = directive_groups['constants']
233 for name in t.iterfind('.//constant'):
b0343f8c 234 if name.attrib.get('index') == 'false':
785a51eb
ZJS
235 continue
236 name.tail = ''
237 if name.text.startswith('('): # a cast, strip it
238 name.text = name.text.partition(' ')[2]
239 storfile[name.text].append((pagename, section))
240 formatting[name.text] = name
241
d970bd6d 242def _make_section(template, name, directives, formatting):
eeb019b5 243 varlist = template.find(".//*[@id='{}']".format(name))
d9cfd694
ZJS
244 for varname, manpages in sorted(directives.items()):
245 entry = tree.SubElement(varlist, 'varlistentry')
d970bd6d 246 term = tree.SubElement(entry, 'term')
827f70eb
ZJS
247 display = deepcopy(formatting[varname])
248 term.append(display)
d970bd6d 249
d9cfd694
ZJS
250 para = tree.SubElement(tree.SubElement(entry, 'listitem'), 'para')
251
252 b = None
ccc9a4f9 253 for manpage, manvolume in sorted(set(manpages)):
827f70eb
ZJS
254 if b is not None:
255 b.tail = ', '
256 b = tree.SubElement(para, 'citerefentry')
257 c = tree.SubElement(b, 'refentrytitle')
258 c.text = manpage
958caa58 259 c.attrib['target'] = varname
827f70eb
ZJS
260 d = tree.SubElement(b, 'manvolnum')
261 d.text = manvolume
d9cfd694
ZJS
262 entry.tail = '\n\n'
263
0acfdd61
ZJS
264def _make_colophon(template, groups):
265 count = 0
266 pages = set()
267 for group in groups:
268 count += len(group)
269 for pagelist in group.values():
270 pages |= set(pagelist)
271
272 para = template.find(".//para[@id='colophon']")
273 para.text = COLOPHON.format(count=count,
274 sections=len(groups),
275 pages=len(pages))
276
d970bd6d 277def _make_page(template, directive_groups, formatting):
d9cfd694
ZJS
278 """Create an XML tree from directive_groups.
279
280 directive_groups = {
281 'class': {'variable': [('manpage', 'manvolume'), ...],
282 'variable2': ...},
283 ...
284 }
285 """
d9cfd694 286 for name, directives in directive_groups.items():
827f70eb 287 _make_section(template, name, directives, formatting)
d9cfd694 288
0acfdd61
ZJS
289 _make_colophon(template, directive_groups.values())
290
eeb019b5 291 return template
d9cfd694 292
ccc9a4f9 293def make_page(*xml_files):
d9cfd694 294 "Extract directives from xml_files and return XML index tree."
eeb019b5
ZJS
295 template = tree.fromstring(TEMPLATE)
296 names = [vl.get('id') for vl in template.iterfind('.//variablelist')]
d9cfd694 297 directive_groups = {name:collections.defaultdict(list)
eeb019b5 298 for name in names}
d970bd6d 299 formatting = {}
d9cfd694 300 for page in xml_files:
ccc9a4f9 301 try:
d970bd6d 302 _extract_directives(directive_groups, formatting, page)
ccc9a4f9
ZJS
303 except Exception:
304 raise ValueError("failed to process " + page)
d9cfd694 305
d970bd6d 306 return _make_page(template, directive_groups, formatting)
d9cfd694
ZJS
307
308if __name__ == '__main__':
1a13e31d
ZJS
309 with open(sys.argv[1], 'wb') as f:
310 f.write(xml_print(make_page(*sys.argv[2:])))