chiark / gitweb /
bus-benchmark: add performance data output mode
[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>Miscellaneous options and directives</title>
143
144                 <para>Other configuration elements which don't fit in
145                 any of the above groups.</para>
146
147                 <variablelist id='miscellaneous' />
148         </refsect1>
149
150         <refsect1>
151                 <title>Files and directories</title>
152
153                 <para>Paths and file names referred to in the
154                 documentation.</para>
155
156                 <variablelist id='filenames' />
157         </refsect1>
158
159         <refsect1>
160                 <title>Colophon</title>
161                 <para id='colophon' />
162         </refsect1>
163 </refentry>
164 '''
165
166 COLOPHON = '''\
167 This index contains {count} entries in {sections} sections,
168 referring to {pages} individual manual pages.
169 '''
170
171 def _extract_directives(directive_groups, formatting, page):
172     t = xml_parse(page)
173     section = t.find('./refmeta/manvolnum').text
174     pagename = t.find('./refmeta/refentrytitle').text
175
176     storopt = directive_groups['options']
177     for variablelist in t.iterfind('.//variablelist'):
178         klass = variablelist.attrib.get('class')
179         storvar = directive_groups[klass or 'miscellaneous']
180         # <option>s go in OPTIONS, unless class is specified
181         for xpath, stor in (('./varlistentry/term/varname', storvar),
182                             ('./varlistentry/term/option',
183                              storvar if klass else storopt)):
184             for name in variablelist.iterfind(xpath):
185                 text = re.sub(r'([= ]).*', r'\1', name.text).rstrip()
186                 stor[text].append((pagename, section))
187                 if text not in formatting:
188                     # use element as formatted display
189                     if name.text[-1] in '= ':
190                         name.clear()
191                     else:
192                         name.tail = ''
193                     name.text = text
194                     formatting[text] = name
195
196     storfile = directive_groups['filenames']
197     for xpath, absolute_only in (('.//refsynopsisdiv//filename', False),
198                                  ('.//refsynopsisdiv//command', False),
199                                  ('.//filename', True)):
200         for name in t.iterfind(xpath):
201             if absolute_only and not (name.text and name.text.startswith('/')):
202                 continue
203             if name.attrib.get('noindex'):
204                 continue
205             name.tail = ''
206             if name.text:
207                 if name.text.endswith('*'):
208                     name.text = name.text[:-1]
209                 if not name.text.startswith('.'):
210                     text = name.text.partition(' ')[0]
211                     if text != name.text:
212                         name.clear()
213                         name.text = text
214                     if text.endswith('/'):
215                         text = text[:-1]
216                     storfile[text].append((pagename, section))
217                     if text not in formatting:
218                         # use element as formatted display
219                         formatting[text] = name
220             else:
221                 text = ' '.join(name.itertext())
222                 storfile[text].append((pagename, section))
223                 formatting[text] = name
224
225 def _make_section(template, name, directives, formatting):
226     varlist = template.find(".//*[@id='{}']".format(name))
227     for varname, manpages in sorted(directives.items()):
228         entry = tree.SubElement(varlist, 'varlistentry')
229         term = tree.SubElement(entry, 'term')
230         display = deepcopy(formatting[varname])
231         term.append(display)
232
233         para = tree.SubElement(tree.SubElement(entry, 'listitem'), 'para')
234
235         b = None
236         for manpage, manvolume in sorted(set(manpages)):
237             if b is not None:
238                 b.tail = ', '
239             b = tree.SubElement(para, 'citerefentry')
240             c = tree.SubElement(b, 'refentrytitle')
241             c.text = manpage
242             d = tree.SubElement(b, 'manvolnum')
243             d.text = manvolume
244         entry.tail = '\n\n'
245
246 def _make_colophon(template, groups):
247     count = 0
248     pages = set()
249     for group in groups:
250         count += len(group)
251         for pagelist in group.values():
252             pages |= set(pagelist)
253
254     para = template.find(".//para[@id='colophon']")
255     para.text = COLOPHON.format(count=count,
256                                 sections=len(groups),
257                                 pages=len(pages))
258
259 def _make_page(template, directive_groups, formatting):
260     """Create an XML tree from directive_groups.
261
262     directive_groups = {
263        'class': {'variable': [('manpage', 'manvolume'), ...],
264                  'variable2': ...},
265        ...
266     }
267     """
268     for name, directives in directive_groups.items():
269         _make_section(template, name, directives, formatting)
270
271     _make_colophon(template, directive_groups.values())
272
273     return template
274
275 def make_page(*xml_files):
276     "Extract directives from xml_files and return XML index tree."
277     template = tree.fromstring(TEMPLATE)
278     names = [vl.get('id') for vl in template.iterfind('.//variablelist')]
279     directive_groups = {name:collections.defaultdict(list)
280                         for name in names}
281     formatting = {}
282     for page in xml_files:
283         try:
284             _extract_directives(directive_groups, formatting, page)
285         except Exception:
286             raise ValueError("failed to process " + page)
287
288     return _make_page(template, directive_groups, formatting)
289
290 if __name__ == '__main__':
291     with open(sys.argv[1], 'wb') as f:
292         f.write(xml_print(make_page(*sys.argv[2:])))