]> git.ipfire.org Git - thirdparty/systemd.git/blob - tools/make-directive-index.py
man: move os-release, machine-info, vconsole.conf vars to envvar section
[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>UDEV directives</title>
57
58 <para>Directives for configuring systemd units through the
59 udev database.</para>
60
61 <variablelist id='udev-directives' />
62 </refsect1>
63
64 <refsect1>
65 <title>Network directives</title>
66
67 <para>Directives for configuring network links through the
68 net-setup-link udev builtin and networks through
69 systemd-networkd.</para>
70
71 <variablelist id='network-directives' />
72 </refsect1>
73
74 <refsect1>
75 <title>Journal fields</title>
76
77 <para>Fields in the journal events with a well known meaning.</para>
78
79 <variablelist id='journal-directives' />
80 </refsect1>
81
82 <refsect1>
83 <title>PAM configuration directives</title>
84
85 <para>Directives for configuring PAM behaviour.</para>
86
87 <variablelist id='pam-directives' />
88 </refsect1>
89
90 <refsect1>
91 <title><filename>/etc/crypttab</filename> and
92 <filename>/etc/fstab</filename> options</title>
93
94 <para>Options which influence mounted filesystems and
95 encrypted volumes.</para>
96
97 <variablelist id='fstab-options' />
98 </refsect1>
99
100 <refsect1>
101 <title>System manager directives</title>
102
103 <para>Directives for configuring the behaviour of the
104 systemd process.</para>
105
106 <variablelist id='systemd-directives' />
107 </refsect1>
108
109 <refsect1>
110 <title>command line options</title>
111
112 <para>Command-line options accepted by programs in the
113 systemd suite.</para>
114
115 <variablelist id='options' />
116 </refsect1>
117
118 <refsect1>
119 <title>Constants</title>
120
121 <para>Various constant used and/or defined by systemd.</para>
122
123 <variablelist id='constants' />
124 </refsect1>
125
126 <refsect1>
127 <title>Miscellaneous options and directives</title>
128
129 <para>Other configuration elements which don't fit in
130 any of the above groups.</para>
131
132 <variablelist id='miscellaneous' />
133 </refsect1>
134
135 <refsect1>
136 <title>Files and directories</title>
137
138 <para>Paths and file names referred to in the
139 documentation.</para>
140
141 <variablelist id='filenames' />
142 </refsect1>
143
144 <refsect1>
145 <title>Colophon</title>
146 <para id='colophon' />
147 </refsect1>
148 </refentry>
149 '''
150
151 COLOPHON = '''\
152 This index contains {count} entries in {sections} sections,
153 referring to {pages} individual manual pages.
154 '''
155
156 def _extract_directives(directive_groups, formatting, page):
157 t = xml_parse(page)
158 section = t.find('./refmeta/manvolnum').text
159 pagename = t.find('./refmeta/refentrytitle').text
160
161 storopt = directive_groups['options']
162 for variablelist in t.iterfind('.//variablelist'):
163 klass = variablelist.attrib.get('class')
164 storvar = directive_groups[klass or 'miscellaneous']
165 # <option>s go in OPTIONS, unless class is specified
166 for xpath, stor in (('./varlistentry/term/varname', storvar),
167 ('./varlistentry/term/option',
168 storvar if klass else storopt)):
169 for name in variablelist.iterfind(xpath):
170 text = re.sub(r'([= ]).*', r'\1', name.text).rstrip()
171 stor[text].append((pagename, section))
172 if text not in formatting:
173 # use element as formatted display
174 if name.text[-1] in '= ':
175 name.clear()
176 else:
177 name.tail = ''
178 name.text = text
179 formatting[text] = name
180
181 storfile = directive_groups['filenames']
182 for xpath, absolute_only in (('.//refsynopsisdiv//filename', False),
183 ('.//refsynopsisdiv//command', False),
184 ('.//filename', True)):
185 for name in t.iterfind(xpath):
186 if absolute_only and not (name.text and name.text.startswith('/')):
187 continue
188 if name.attrib.get('noindex'):
189 continue
190 name.tail = ''
191 if name.text:
192 if name.text.endswith('*'):
193 name.text = name.text[:-1]
194 if not name.text.startswith('.'):
195 text = name.text.partition(' ')[0]
196 if text != name.text:
197 name.clear()
198 name.text = text
199 if text.endswith('/'):
200 text = text[:-1]
201 storfile[text].append((pagename, section))
202 if text not in formatting:
203 # use element as formatted display
204 formatting[text] = name
205 else:
206 text = ' '.join(name.itertext())
207 storfile[text].append((pagename, section))
208 formatting[text] = name
209
210 storfile = directive_groups['constants']
211 for name in t.iterfind('.//constant'):
212 if name.attrib.get('noindex'):
213 continue
214 name.tail = ''
215 if name.text.startswith('('): # a cast, strip it
216 name.text = name.text.partition(' ')[2]
217 storfile[name.text].append((pagename, section))
218 formatting[name.text] = name
219
220 def _make_section(template, name, directives, formatting):
221 varlist = template.find(".//*[@id='{}']".format(name))
222 for varname, manpages in sorted(directives.items()):
223 entry = tree.SubElement(varlist, 'varlistentry')
224 term = tree.SubElement(entry, 'term')
225 display = deepcopy(formatting[varname])
226 term.append(display)
227
228 para = tree.SubElement(tree.SubElement(entry, 'listitem'), 'para')
229
230 b = None
231 for manpage, manvolume in sorted(set(manpages)):
232 if b is not None:
233 b.tail = ', '
234 b = tree.SubElement(para, 'citerefentry')
235 c = tree.SubElement(b, 'refentrytitle')
236 c.text = manpage
237 c.attrib['target'] = varname
238 d = tree.SubElement(b, 'manvolnum')
239 d.text = manvolume
240 entry.tail = '\n\n'
241
242 def _make_colophon(template, groups):
243 count = 0
244 pages = set()
245 for group in groups:
246 count += len(group)
247 for pagelist in group.values():
248 pages |= set(pagelist)
249
250 para = template.find(".//para[@id='colophon']")
251 para.text = COLOPHON.format(count=count,
252 sections=len(groups),
253 pages=len(pages))
254
255 def _make_page(template, directive_groups, formatting):
256 """Create an XML tree from directive_groups.
257
258 directive_groups = {
259 'class': {'variable': [('manpage', 'manvolume'), ...],
260 'variable2': ...},
261 ...
262 }
263 """
264 for name, directives in directive_groups.items():
265 _make_section(template, name, directives, formatting)
266
267 _make_colophon(template, directive_groups.values())
268
269 return template
270
271 def make_page(*xml_files):
272 "Extract directives from xml_files and return XML index tree."
273 template = tree.fromstring(TEMPLATE)
274 names = [vl.get('id') for vl in template.iterfind('.//variablelist')]
275 directive_groups = {name:collections.defaultdict(list)
276 for name in names}
277 formatting = {}
278 for page in xml_files:
279 try:
280 _extract_directives(directive_groups, formatting, page)
281 except Exception:
282 raise ValueError("failed to process " + page)
283
284 return _make_page(template, directive_groups, formatting)
285
286 if __name__ == '__main__':
287 with open(sys.argv[1], 'wb') as f:
288 f.write(xml_print(make_page(*sys.argv[2:])))