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