2 # -*- Mode: python; coding: utf-8; indent-tabs-mode: nil -*- */
4 # This file is part of systemd.
6 # Copyright 2013, 2017 Zbigniew Jędrzejewski-Szmek
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.
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.
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/>.
21 from __future__ import print_function
26 from xml_helper import *
46 # Do not edit. Generated by make-man-rules.py.
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
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.
57 HTML_ALIAS_RULE = '''\
64 # Really, do not edit this file.
72 def man(page, number):
73 return ('man/' if not meson else '') + '{}.{}'.format(page, number)
76 return ('man/' if not meson else '') + os.path.basename(file)
78 def add_rules(rules, name):
80 # print('parsing {}'.format(name), file=sys.stderr)
81 if xml.getroot().tag != 'refentry':
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)
99 def create_rules(xml_files):
100 " {conditional => {alias-name => source-name}} "
101 rules = collections.defaultdict(dict)
102 for name in xml_files:
104 add_rules(rules, name)
106 print("Failed to process", name, file=sys.stderr)
111 return ' \\\n\t'.join(sorted(files) or '#')
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())
121 htmlrules='\n'.join(HTML_ALIAS_RULE.format(k[:-2],v[:-2])
122 for k,v in sorted(rulegroup.items())
124 conditional=conditional)
125 for conditional,rulegroup in sorted(rules.items())
126 ) + FOOTER.format(dist_files=mjoin(sorted(dist_files)))
129 # Do not edit. Generated by make-man-rules.py.
134 # Really, do not edit.'''
136 def make_mesonfile(rules, dist_files):
138 # grouped = [ [name, section, [alias...], condition], ...]
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)]
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))
153 if __name__ == '__main__':
154 meson = sys.argv[1] == '--meson'
155 pages = sys.argv[1+meson:]
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"))
162 print(make_mesonfile(rules, dist_files))
164 print(make_makefile(rules, dist_files), end='')