chiark / gitweb /
udev: use initialization instead of zeroing in one place
[elogind.git] / make-directive-index.py
1 #  -*- Mode: python; coding: utf-8; indent-tabs-mode: nil -*- */
2 #
3 #  This file is part of systemd.
4 #
5 #  Copyright 2012-2013 Zbigniew JÄ™drzejewski-Szmek
6 #
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
20 import sys
21 import collections
22 import re
23 from xml_helper import *
24 from copy import deepcopy
25
26 TEMPLATE = '''\
27 <refentry id="systemd.directives" conditional="HAVE_PYTHON">
28
29         <refentryinfo>
30                 <title>systemd.directives</title>
31                 <productname>systemd</productname>
32
33                 <authorgroup>
34                         <author>
35                                 <contrib>Developer</contrib>
36                                 <firstname>Zbigniew</firstname>
37                                 <surname>JÄ™drzejewski-Szmek</surname>
38                                 <email>zbyszek@in.waw.pl</email>
39                         </author>
40                 </authorgroup>
41         </refentryinfo>
42
43         <refmeta>
44                 <refentrytitle>systemd.directives</refentrytitle>
45                 <manvolnum>7</manvolnum>
46         </refmeta>
47
48         <refnamediv>
49                 <refname>systemd.directives</refname>
50                 <refpurpose>Index of configuration directives</refpurpose>
51         </refnamediv>
52
53         <refsect1>
54                 <title>Unit directives</title>
55
56                 <para>Directives for configuring units, used in unit
57                 files.</para>
58
59                 <variablelist id='unit-directives' />
60         </refsect1>
61
62         <refsect1>
63                 <title>Options on the kernel command line</title>
64
65                 <para>Kernel boot options for configuring the behaviour of the
66                 systemd process.</para>
67
68                 <variablelist id='kernel-commandline-options' />
69         </refsect1>
70
71         <refsect1>
72                 <title>Environment variables</title>
73
74                 <para>Environment variables understood by the systemd
75                 manager and other programs.</para>
76
77                 <variablelist id='environment-variables' />
78         </refsect1>
79
80         <refsect1>
81                 <title>UDEV directives</title>
82
83                 <para>Directives for configuring systemd units through the
84                 udev database.</para>
85
86                 <variablelist id='udev-directives' />
87         </refsect1>
88
89         <refsect1>
90                 <title>Journal fields</title>
91
92                 <para>Fields in the journal events with a well known meaning.</para>
93
94                 <variablelist id='journal-directives' />
95         </refsect1>
96
97         <refsect1>
98                 <title>PAM configuration directives</title>
99
100                 <para>Directives for configuring PAM behaviour.</para>
101
102                 <variablelist id='pam-directives' />
103         </refsect1>
104
105         <refsect1>
106                 <title>crypttab options</title>
107
108                 <para>Options which influence mounted filesystems and
109                 encrypted volumes.</para>
110
111                 <variablelist id='crypttab-options' />
112         </refsect1>
113
114         <refsect1>
115                 <title>System manager directives</title>
116
117                 <para>Directives for configuring the behaviour of the
118                 systemd process.</para>
119
120                 <variablelist id='systemd-directives' />
121         </refsect1>
122
123         <refsect1>
124                 <title>bootchart.conf directives</title>
125
126                 <para>Directives for configuring the behaviour of the
127                 systemd-bootchart process.</para>
128
129                 <variablelist id='bootchart-directives' />
130         </refsect1>
131
132         <refsect1>
133                 <title>command-line options</title>
134
135                 <para>Command-line options accepted by programs in the
136                 systemd suite.</para>
137
138                 <variablelist id='options' />
139         </refsect1>
140
141         <refsect1>
142                 <title>Constants</title>
143
144                 <para>Various constant used and/or defined by systemd.</para>
145
146                 <variablelist id='constants' />
147         </refsect1>
148
149         <refsect1>
150                 <title>Miscellaneous options and directives</title>
151
152                 <para>Other configuration elements which don't fit in
153                 any of the above groups.</para>
154
155                 <variablelist id='miscellaneous' />
156         </refsect1>
157
158         <refsect1>
159                 <title>Files and directories</title>
160
161                 <para>Paths and file names referred to in the
162                 documentation.</para>
163
164                 <variablelist id='filenames' />
165         </refsect1>
166
167         <refsect1>
168                 <title>Colophon</title>
169                 <para id='colophon' />
170         </refsect1>
171 </refentry>
172 '''
173
174 COLOPHON = '''\
175 This index contains {count} entries in {sections} sections,
176 referring to {pages} individual manual pages.
177 '''
178
179 def _extract_directives(directive_groups, formatting, page):
180     t = xml_parse(page)
181     section = t.find('./refmeta/manvolnum').text
182     pagename = t.find('./refmeta/refentrytitle').text
183
184     storopt = directive_groups['options']
185     for variablelist in t.iterfind('.//variablelist'):
186         klass = variablelist.attrib.get('class')
187         storvar = directive_groups[klass or 'miscellaneous']
188         # <option>s go in OPTIONS, unless class is specified
189         for xpath, stor in (('./varlistentry/term/varname', storvar),
190                             ('./varlistentry/term/option',
191                              storvar if klass else storopt)):
192             for name in variablelist.iterfind(xpath):
193                 text = re.sub(r'([= ]).*', r'\1', name.text).rstrip()
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
204     storfile = directive_groups['filenames']
205     for xpath, absolute_only in (('.//refsynopsisdiv//filename', False),
206                                  ('.//refsynopsisdiv//command', False),
207                                  ('.//filename', True)):
208         for name in t.iterfind(xpath):
209             if absolute_only and not (name.text and name.text.startswith('/')):
210                 continue
211             if name.attrib.get('noindex'):
212                 continue
213             name.tail = ''
214             if name.text:
215                 if name.text.endswith('*'):
216                     name.text = name.text[:-1]
217                 if not name.text.startswith('.'):
218                     text = name.text.partition(' ')[0]
219                     if text != name.text:
220                         name.clear()
221                         name.text = text
222                     if text.endswith('/'):
223                         text = text[:-1]
224                     storfile[text].append((pagename, section))
225                     if text not in formatting:
226                         # use element as formatted display
227                         formatting[text] = name
228             else:
229                 text = ' '.join(name.itertext())
230                 storfile[text].append((pagename, section))
231                 formatting[text] = name
232
233     storfile = directive_groups['constants']
234     for name in t.iterfind('.//constant'):
235         if name.attrib.get('noindex'):
236             continue
237         name.tail = ''
238         if name.text.startswith('('): # a cast, strip it
239             name.text = name.text.partition(' ')[2]
240         storfile[name.text].append((pagename, section))
241         formatting[name.text] = name
242
243 def _make_section(template, name, directives, formatting):
244     varlist = template.find(".//*[@id='{}']".format(name))
245     for varname, manpages in sorted(directives.items()):
246         entry = tree.SubElement(varlist, 'varlistentry')
247         term = tree.SubElement(entry, 'term')
248         display = deepcopy(formatting[varname])
249         term.append(display)
250
251         para = tree.SubElement(tree.SubElement(entry, 'listitem'), 'para')
252
253         b = None
254         for manpage, manvolume in sorted(set(manpages)):
255             if b is not None:
256                 b.tail = ', '
257             b = tree.SubElement(para, 'citerefentry')
258             c = tree.SubElement(b, 'refentrytitle')
259             c.text = manpage
260             d = tree.SubElement(b, 'manvolnum')
261             d.text = manvolume
262         entry.tail = '\n\n'
263
264 def _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
277 def _make_page(template, directive_groups, formatting):
278     """Create an XML tree from directive_groups.
279
280     directive_groups = {
281        'class': {'variable': [('manpage', 'manvolume'), ...],
282                  'variable2': ...},
283        ...
284     }
285     """
286     for name, directives in directive_groups.items():
287         _make_section(template, name, directives, formatting)
288
289     _make_colophon(template, directive_groups.values())
290
291     return template
292
293 def make_page(*xml_files):
294     "Extract directives from xml_files and return XML index tree."
295     template = tree.fromstring(TEMPLATE)
296     names = [vl.get('id') for vl in template.iterfind('.//variablelist')]
297     directive_groups = {name:collections.defaultdict(list)
298                         for name in names}
299     formatting = {}
300     for page in xml_files:
301         try:
302             _extract_directives(directive_groups, formatting, page)
303         except Exception:
304             raise ValueError("failed to process " + page)
305
306     return _make_page(template, directive_groups, formatting)
307
308 if __name__ == '__main__':
309     with open(sys.argv[1], 'wb') as f:
310         f.write(xml_print(make_page(*sys.argv[2:])))