chiark / gitweb /
man: add man page for localectl
[elogind.git] / make-directive-index.py
1 # -*- coding: utf-8 -*-
2 import sys
3 import collections
4 import xml.etree.ElementTree as tree
5
6 TEMPLATE = '''\
7 <refentry id="systemd.directives">
8
9         <refentryinfo>
10                 <title>systemd.directives</title>
11                 <productname>systemd</productname>
12
13                 <authorgroup>
14                         <author>
15                                 <contrib>Developer</contrib>
16                                 <firstname>Zbigniew</firstname>
17                                 <surname>JÄ™drzejewski-Szmek</surname>
18                                 <email>zbyszek@in.waw.pl</email>
19                         </author>
20                 </authorgroup>
21         </refentryinfo>
22
23         <refmeta>
24                 <refentrytitle>systemd.directives</refentrytitle>
25                 <manvolnum>5</manvolnum>
26         </refmeta>
27
28         <refnamediv>
29                 <refname>systemd.directives</refname>
30                 <refpurpose>Index of configuration directives</refpurpose>
31         </refnamediv>
32
33         <refsect1>
34                 <title>Unit directives</title>
35
36                 <para>Directives for configuring units, used in unit
37                 files.</para>
38
39                 <variablelist id='unit-directives' />
40         </refsect1>
41
42         <refsect1>
43                 <title>System manager directives</title>
44
45                 <para>Directives for configuring the behaviour of the
46                 systemd process.</para>
47
48                 <variablelist id='systemd-directives' />
49         </refsect1>
50
51         <refsect1>
52                 <title>UDEV directives</title>
53
54                 <para>Directives for configuring systemd units through the
55                 udev database.</para>
56
57                 <variablelist id='udev-directives' />
58         </refsect1>
59
60         <refsect1>
61                 <title>Journal directives</title>
62
63                 <para>Directives for configuring the behaviour of the
64                 journald process.</para>
65
66                 <variablelist id='journal-directives' />
67         </refsect1>
68 </refentry>
69 '''
70
71 def _extract_directives(directive_groups, page):
72     t = tree.parse(page)
73     section = t.find('./refmeta/manvolnum').text
74     pagename = t.find('./refmeta/refentrytitle').text
75     for variablelist in t.iterfind('.//variablelist'):
76         klass = variablelist.attrib.get('class') or 'unit-directives'
77         stor = directive_groups[klass]
78         for varname in variablelist.iterfind('./varlistentry/term/varname'):
79             text = ''.join(varname.text.partition('=')[:2])
80             stor[text].append((pagename, section))
81
82 def _make_section(refentry, name, directives):
83     varlist = refentry.find(".//*[@id='{}']".format(name))
84     for varname, manpages in sorted(directives.items()):
85         entry = tree.SubElement(varlist, 'varlistentry')
86         a = tree.SubElement(tree.SubElement(entry, 'term'), 'varname')
87         a.text = varname
88         para = tree.SubElement(tree.SubElement(entry, 'listitem'), 'para')
89
90         b = None
91         for manpage, manvolume in sorted(manpages):
92                 if b is not None:
93                         b.tail = ', '
94                 b = tree.SubElement(para, 'citerefentry')
95                 c = tree.SubElement(b, 'refentrytitle')
96                 c.text = manpage
97                 d = tree.SubElement(b, 'manvolnum')
98                 d.text = manvolume
99         entry.tail = '\n\n'
100
101 def _make_page(directive_groups):
102     """Create an XML tree from directive_groups.
103
104     directive_groups = {
105        'class': {'variable': [('manpage', 'manvolume'), ...],
106                  'variable2': ...},
107        ...
108     }
109     """
110     refentry = tree.fromstring(TEMPLATE)
111
112     for name, directives in directive_groups.items():
113             _make_section(refentry, name, directives)
114
115     return refentry
116
117 def make_page(xml_files):
118     "Extract directives from xml_files and return XML index tree."
119     directive_groups = {name:collections.defaultdict(list)
120                         for name in ['unit-directives',
121                                      'udev-directives',
122                                      'systemd-directives',
123                                      'journal-directives',
124                                      ]}
125     for page in xml_files:
126         _extract_directives(directive_groups, page)
127
128     return _make_page(directive_groups)
129
130 if __name__ == '__main__':
131     tree.dump(make_page(sys.argv[1:]))