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