blob: 039efaa4345001fe4f22fa16c07c3fcc55ae632b [file] [log] [blame]
Zbigniew Jędrzejewski-Szmeke7098b62012-11-13 18:39:18 +01001# -*- Mode: python; coding: utf-8; indent-tabs-mode: nil -*- */
2#
3# This file is part of systemd.
4#
Zbigniew Jędrzejewski-Szmekccc9a4f2013-01-26 10:47:16 -05005# Copyright 2012-2013 Zbigniew Jędrzejewski-Szmek
Zbigniew Jędrzejewski-Szmeke7098b62012-11-13 18:39:18 +01006#
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
Zbigniew Jędrzejewski-Szmekd9cfd692012-08-09 18:08:14 +020020import sys
21import collections
Zbigniew Jędrzejewski-Szmek80cb9172013-03-07 20:13:40 -050022try:
23 from lxml import etree as tree
24 PRETTY = dict(pretty_print=True)
25except ImportError:
26 import xml.etree.ElementTree as tree
27 PRETTY = {}
Zbigniew Jędrzejewski-Szmekccc9a4f2013-01-26 10:47:16 -050028import re
Zbigniew Jędrzejewski-Szmekd9cfd692012-08-09 18:08:14 +020029
30TEMPLATE = '''\
Zbigniew Jędrzejewski-Szmek56ba3c72013-02-02 22:47:47 -050031<refentry id="systemd.directives" conditional="HAVE_PYTHON">
Zbigniew Jędrzejewski-Szmekd9cfd692012-08-09 18:08:14 +020032
33 <refentryinfo>
34 <title>systemd.directives</title>
35 <productname>systemd</productname>
36
37 <authorgroup>
38 <author>
39 <contrib>Developer</contrib>
40 <firstname>Zbigniew</firstname>
41 <surname>Jędrzejewski-Szmek</surname>
42 <email>zbyszek@in.waw.pl</email>
43 </author>
44 </authorgroup>
45 </refentryinfo>
46
47 <refmeta>
48 <refentrytitle>systemd.directives</refentrytitle>
Zbigniew Jędrzejewski-Szmek9cc2c8b2013-01-14 22:08:33 -050049 <manvolnum>7</manvolnum>
Zbigniew Jędrzejewski-Szmekd9cfd692012-08-09 18:08:14 +020050 </refmeta>
51
52 <refnamediv>
53 <refname>systemd.directives</refname>
54 <refpurpose>Index of configuration directives</refpurpose>
55 </refnamediv>
56
57 <refsect1>
58 <title>Unit directives</title>
59
60 <para>Directives for configuring units, used in unit
61 files.</para>
62
63 <variablelist id='unit-directives' />
64 </refsect1>
Zbigniew Jędrzejewski-Szmeke1abd3e2012-09-16 11:11:34 +020065
66 <refsect1>
Zbigniew Jędrzejewski-Szmek08177c82013-01-14 21:58:22 -050067 <title>Options on the kernel command line</title>
68
69 <para>Kernel boot options for configuring the behaviour of the
70 systemd process.</para>
71
Zbigniew Jędrzejewski-Szmekccc9a4f2013-01-26 10:47:16 -050072 <variablelist id='kernel-commandline-options' />
Zbigniew Jędrzejewski-Szmek08177c82013-01-14 21:58:22 -050073 </refsect1>
74
75 <refsect1>
76 <title>Environment variables</title>
77
Zbigniew Jędrzejewski-Szmekccc9a4f2013-01-26 10:47:16 -050078 <para>Environment variables understood by the systemd
79 manager and other programs.</para>
Zbigniew Jędrzejewski-Szmek08177c82013-01-14 21:58:22 -050080
81 <variablelist id='environment-variables' />
82 </refsect1>
83
84 <refsect1>
Zbigniew Jędrzejewski-Szmeke1abd3e2012-09-16 11:11:34 +020085 <title>UDEV directives</title>
86
87 <para>Directives for configuring systemd units through the
88 udev database.</para>
89
90 <variablelist id='udev-directives' />
91 </refsect1>
Zbigniew Jędrzejewski-Szmekf6c2e282012-08-10 19:35:43 +020092
93 <refsect1>
Zbigniew Jędrzejewski-Szmekccc9a4f2013-01-26 10:47:16 -050094 <title>Journal fields</title>
Zbigniew Jędrzejewski-Szmekf6c2e282012-08-10 19:35:43 +020095
Zbigniew Jędrzejewski-Szmekccc9a4f2013-01-26 10:47:16 -050096 <para>Fields in the journal events with a well known meaning.</para>
Zbigniew Jędrzejewski-Szmekf6c2e282012-08-10 19:35:43 +020097
98 <variablelist id='journal-directives' />
99 </refsect1>
Zbigniew Jędrzejewski-Szmek4a431c92013-01-14 20:18:36 -0500100
101 <refsect1>
Zbigniew Jędrzejewski-Szmekccc9a4f2013-01-26 10:47:16 -0500102 <title>PAM configuration directives</title>
103
104 <para>Directives for configuring PAM behaviour.</para>
105
106 <variablelist id='pam-directives' />
107 </refsect1>
108
109 <refsect1>
110 <title>crypttab options</title>
111
112 <para>Options which influence mounted filesystems and
113 encrypted volumes.</para>
114
115 <variablelist id='crypttab-options' />
116 </refsect1>
117
118 <refsect1>
119 <title>System manager directives</title>
120
121 <para>Directives for configuring the behaviour of the
122 systemd process.</para>
123
124 <variablelist id='systemd-directives' />
125 </refsect1>
126
127 <refsect1>
Zbigniew Jędrzejewski-Szmek4a431c92013-01-14 20:18:36 -0500128 <title>bootchart.conf directives</title>
129
130 <para>Directives for configuring the behaviour of the
131 systemd-bootchart process.</para>
132
133 <variablelist id='bootchart-directives' />
134 </refsect1>
Zbigniew Jędrzejewski-Szmek0acfdd62013-01-14 21:34:19 -0500135
136 <refsect1>
Zbigniew Jędrzejewski-Szmekccc9a4f2013-01-26 10:47:16 -0500137 <title>command-line options</title>
138
139 <para>Command-line options accepted by programs in the
140 systemd suite.</para>
141
142 <variablelist id='options' />
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>
Zbigniew Jędrzejewski-Szmeka4e0b942013-02-13 21:51:31 -0500155 <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>
Zbigniew Jędrzejewski-Szmek0acfdd62013-01-14 21:34:19 -0500164 <title>Colophon</title>
165 <para id='colophon' />
166 </refsect1>
Zbigniew Jędrzejewski-Szmekd9cfd692012-08-09 18:08:14 +0200167</refentry>
168'''
169
Zbigniew Jędrzejewski-Szmek0acfdd62013-01-14 21:34:19 -0500170COLOPHON = '''\
171This index contains {count} entries in {sections} sections,
172referring to {pages} individual manual pages.
173'''
174
Zbigniew Jędrzejewski-Szmekd970bd62013-01-26 11:27:35 -0500175def _extract_directives(directive_groups, formatting, page):
Zbigniew Jędrzejewski-Szmekd9cfd692012-08-09 18:08:14 +0200176 t = tree.parse(page)
177 section = t.find('./refmeta/manvolnum').text
178 pagename = t.find('./refmeta/refentrytitle').text
Zbigniew Jędrzejewski-Szmeka4e0b942013-02-13 21:51:31 -0500179
180 storopt = directive_groups['options']
Zbigniew Jędrzejewski-Szmekd9cfd692012-08-09 18:08:14 +0200181 for variablelist in t.iterfind('.//variablelist'):
Zbigniew Jędrzejewski-Szmekccc9a4f2013-01-26 10:47:16 -0500182 klass = variablelist.attrib.get('class')
183 storvar = directive_groups[klass or 'miscellaneous']
Zbigniew Jędrzejewski-Szmekccc9a4f2013-01-26 10:47:16 -0500184 # <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()
190 stor[text].append((pagename, section))
Zbigniew Jędrzejewski-Szmekd970bd62013-01-26 11:27:35 -0500191 if text not in formatting:
192 # use element as formatted display
Zbigniew Jędrzejewski-Szmek699ad6c2013-02-27 21:31:02 -0500193 if name.text[-1] in '= ':
194 name.clear()
195 else:
196 name.tail = ''
Zbigniew Jędrzejewski-Szmekd970bd62013-01-26 11:27:35 -0500197 name.text = text
198 formatting[text] = name
Zbigniew Jędrzejewski-Szmekd9cfd692012-08-09 18:08:14 +0200199
Zbigniew Jędrzejewski-Szmeka4e0b942013-02-13 21:51:31 -0500200 storfile = directive_groups['filenames']
201 for xpath in ('.//refsynopsisdiv//filename',
202 './/refsynopsisdiv//command'):
203 for name in t.iterfind(xpath):
204 name.tail = ''
205 if name.text:
206 if not name.text.startswith('.'):
207 text = name.text.partition(' ')[0]
208 if text != name.text:
209 name.clear()
210 name.text = text
211 storfile[text].append((pagename, section))
212 if text not in formatting:
213 # use element as formatted display
214 formatting[text] = name
215 else:
216 text = ' '.join(name.itertext())
217 storfile[text].append((pagename, section))
218 formatting[text] = name
219
Zbigniew Jędrzejewski-Szmekd970bd62013-01-26 11:27:35 -0500220def _make_section(template, name, directives, formatting):
Zbigniew Jędrzejewski-Szmekeeb019b2013-01-14 21:02:49 -0500221 varlist = template.find(".//*[@id='{}']".format(name))
Zbigniew Jędrzejewski-Szmekd9cfd692012-08-09 18:08:14 +0200222 for varname, manpages in sorted(directives.items()):
223 entry = tree.SubElement(varlist, 'varlistentry')
Zbigniew Jędrzejewski-Szmekd970bd62013-01-26 11:27:35 -0500224 term = tree.SubElement(entry, 'term')
225 term.append(formatting[varname])
226
Zbigniew Jędrzejewski-Szmekd9cfd692012-08-09 18:08:14 +0200227 para = tree.SubElement(tree.SubElement(entry, 'listitem'), 'para')
228
229 b = None
Zbigniew Jędrzejewski-Szmekccc9a4f2013-01-26 10:47:16 -0500230 for manpage, manvolume in sorted(set(manpages)):
Zbigniew Jędrzejewski-Szmekd9cfd692012-08-09 18:08:14 +0200231 if b is not None:
232 b.tail = ', '
233 b = tree.SubElement(para, 'citerefentry')
234 c = tree.SubElement(b, 'refentrytitle')
235 c.text = manpage
236 d = tree.SubElement(b, 'manvolnum')
237 d.text = manvolume
238 entry.tail = '\n\n'
239
Zbigniew Jędrzejewski-Szmek0acfdd62013-01-14 21:34:19 -0500240def _make_colophon(template, groups):
241 count = 0
242 pages = set()
243 for group in groups:
244 count += len(group)
245 for pagelist in group.values():
246 pages |= set(pagelist)
247
248 para = template.find(".//para[@id='colophon']")
249 para.text = COLOPHON.format(count=count,
250 sections=len(groups),
251 pages=len(pages))
252
Zbigniew Jędrzejewski-Szmekd970bd62013-01-26 11:27:35 -0500253def _make_page(template, directive_groups, formatting):
Zbigniew Jędrzejewski-Szmekd9cfd692012-08-09 18:08:14 +0200254 """Create an XML tree from directive_groups.
255
256 directive_groups = {
257 'class': {'variable': [('manpage', 'manvolume'), ...],
258 'variable2': ...},
259 ...
260 }
261 """
Zbigniew Jędrzejewski-Szmekd9cfd692012-08-09 18:08:14 +0200262 for name, directives in directive_groups.items():
Zbigniew Jędrzejewski-Szmekd970bd62013-01-26 11:27:35 -0500263 _make_section(template, name, directives, formatting)
Zbigniew Jędrzejewski-Szmekd9cfd692012-08-09 18:08:14 +0200264
Zbigniew Jędrzejewski-Szmek0acfdd62013-01-14 21:34:19 -0500265 _make_colophon(template, directive_groups.values())
266
Zbigniew Jędrzejewski-Szmekeeb019b2013-01-14 21:02:49 -0500267 return template
Zbigniew Jędrzejewski-Szmekd9cfd692012-08-09 18:08:14 +0200268
Zbigniew Jędrzejewski-Szmekccc9a4f2013-01-26 10:47:16 -0500269def make_page(*xml_files):
Zbigniew Jędrzejewski-Szmekd9cfd692012-08-09 18:08:14 +0200270 "Extract directives from xml_files and return XML index tree."
Zbigniew Jędrzejewski-Szmekeeb019b2013-01-14 21:02:49 -0500271 template = tree.fromstring(TEMPLATE)
272 names = [vl.get('id') for vl in template.iterfind('.//variablelist')]
Zbigniew Jędrzejewski-Szmekd9cfd692012-08-09 18:08:14 +0200273 directive_groups = {name:collections.defaultdict(list)
Zbigniew Jędrzejewski-Szmekeeb019b2013-01-14 21:02:49 -0500274 for name in names}
Zbigniew Jędrzejewski-Szmekd970bd62013-01-26 11:27:35 -0500275 formatting = {}
Zbigniew Jędrzejewski-Szmekd9cfd692012-08-09 18:08:14 +0200276 for page in xml_files:
Zbigniew Jędrzejewski-Szmekccc9a4f2013-01-26 10:47:16 -0500277 try:
Zbigniew Jędrzejewski-Szmekd970bd62013-01-26 11:27:35 -0500278 _extract_directives(directive_groups, formatting, page)
Zbigniew Jędrzejewski-Szmekccc9a4f2013-01-26 10:47:16 -0500279 except Exception:
280 raise ValueError("failed to process " + page)
Zbigniew Jędrzejewski-Szmekd9cfd692012-08-09 18:08:14 +0200281
Zbigniew Jędrzejewski-Szmekd970bd62013-01-26 11:27:35 -0500282 return _make_page(template, directive_groups, formatting)
Zbigniew Jędrzejewski-Szmekd9cfd692012-08-09 18:08:14 +0200283
284if __name__ == '__main__':
Zbigniew Jędrzejewski-Szmek80cb9172013-03-07 20:13:40 -0500285 tree.dump(make_page(*sys.argv[1:]), **PRETTY)