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