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