]> git.ipfire.org Git - thirdparty/systemd.git/blob - tools/make-man-rules.py
Merge pull request #5842 from keszybz/meson-status-and-conditionals
[thirdparty/systemd.git] / tools / make-man-rules.py
1 #!/usr/bin/env python3
2 # -*- Mode: python; coding: utf-8; indent-tabs-mode: nil -*- */
3 #
4 # This file is part of systemd.
5 #
6 # Copyright 2013, 2017 Zbigniew Jędrzejewski-Szmek
7 #
8 # systemd is free software; you can redistribute it and/or modify it
9 # under the terms of the GNU Lesser General Public License as published by
10 # the Free Software Foundation; either version 2.1 of the License, or
11 # (at your option) any later version.
12 #
13 # systemd is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 # Lesser General Public License for more details.
17 #
18 # You should have received a copy of the GNU Lesser General Public License
19 # along with systemd; If not, see <http://www.gnu.org/licenses/>.
20
21 from __future__ import print_function
22 import collections
23 import sys
24 import os.path
25 import pprint
26 from xml_helper import *
27
28 SECTION = '''\
29 MANPAGES += \\
30 {manpages}
31 MANPAGES_ALIAS += \\
32 {aliases}
33 {rules}
34 {htmlrules}
35 '''
36
37 CONDITIONAL = '''\
38 if {conditional}
39 ''' \
40 + SECTION + \
41 '''\
42 endif
43 '''
44
45 HEADER = '''\
46 # Do not edit. Generated by make-man-rules.py.
47 # To regenerate:
48 # 1. Create, update, or remove source .xml files in man/
49 # 2. Run 'make update-man-list'
50 # 3. Run 'make man' to generate manpages
51 #
52 # To make a man page conditional on a configure switch add
53 # attribute conditional="ENABLE_WHAT" or conditional="WITH_WHAT"
54 # to <refentry> element.
55 '''
56
57 HTML_ALIAS_RULE = '''\
58 {}.html: {}.html
59 $(html-alias)
60 '''
61
62 FOOTER = '''\
63
64 # Really, do not edit this file.
65
66 EXTRA_DIST += \\
67 {dist_files}
68 '''
69
70 meson = False
71
72 def man(page, number):
73 return ('man/' if not meson else '') + '{}.{}'.format(page, number)
74
75 def xml(file):
76 return ('man/' if not meson else '') + os.path.basename(file)
77
78 def add_rules(rules, name):
79 xml = xml_parse(name)
80 # print('parsing {}'.format(name), file=sys.stderr)
81 if xml.getroot().tag != 'refentry':
82 return
83 conditional = xml.getroot().get('conditional') or ''
84 rulegroup = rules[conditional]
85 refmeta = xml.find('./refmeta')
86 title = refmeta.find('./refentrytitle').text
87 number = refmeta.find('./manvolnum').text
88 refnames = xml.findall('./refnamediv/refname')
89 target = man(refnames[0].text, number)
90 if title != refnames[0].text:
91 raise ValueError('refmeta and refnamediv disagree: ' + name)
92 for refname in refnames:
93 assert all(refname not in group
94 for group in rules.values()), "duplicate page name"
95 alias = man(refname.text, number)
96 rulegroup[alias] = target
97 # print('{} => {} [{}]'.format(alias, target, conditional), file=sys.stderr)
98
99 def create_rules(xml_files):
100 " {conditional => {alias-name => source-name}} "
101 rules = collections.defaultdict(dict)
102 for name in xml_files:
103 try:
104 add_rules(rules, name)
105 except Exception:
106 print("Failed to process", name, file=sys.stderr)
107 raise
108 return rules
109
110 def mjoin(files):
111 return ' \\\n\t'.join(sorted(files) or '#')
112
113 def make_makefile(rules, dist_files):
114 return HEADER + '\n'.join(
115 (CONDITIONAL if conditional else SECTION).format(
116 manpages=mjoin(set(rulegroup.values())),
117 aliases=mjoin(k for k,v in rulegroup.items() if k != v),
118 rules='\n'.join('{}: {}'.format(k,v)
119 for k,v in sorted(rulegroup.items())
120 if k != v),
121 htmlrules='\n'.join(HTML_ALIAS_RULE.format(k[:-2],v[:-2])
122 for k,v in sorted(rulegroup.items())
123 if k != v),
124 conditional=conditional)
125 for conditional,rulegroup in sorted(rules.items())
126 ) + FOOTER.format(dist_files=mjoin(sorted(dist_files)))
127
128 MESON_HEADER = '''\
129 # Do not edit. Generated by make-man-rules.py.
130 manpages = ['''
131
132 MESON_FOOTER = '''\
133 ]
134 # Really, do not edit.'''
135
136 def make_mesonfile(rules, dist_files):
137 # reformat rules as
138 # grouped = [ [name, section, [alias...], condition], ...]
139 #
140 # but first create a dictionary like
141 # lists = { (name, condition) => [alias...]
142 grouped = collections.defaultdict(list)
143 for condition, items in rules.items():
144 for alias, name in items.items():
145 group = grouped[(name, condition)]
146 if name != alias:
147 group.append(alias)
148
149 lines = [ [p[0][:-2], p[0][-1], sorted(a[:-2] for a in aliases), p[1]]
150 for p, aliases in sorted(grouped.items()) ]
151 return '\n'.join((MESON_HEADER, pprint.pformat(lines)[1:-1], MESON_FOOTER))
152
153 if __name__ == '__main__':
154 meson = sys.argv[1] == '--meson'
155 pages = sys.argv[1+meson:]
156
157 rules = create_rules(pages)
158 dist_files = (xml(file) for file in pages
159 if not file.endswith(".directives.xml") and
160 not file.endswith(".index.xml"))
161 if meson:
162 print(make_mesonfile(rules, dist_files))
163 else:
164 print(make_makefile(rules, dist_files), end='')