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