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 xml_parse
28 def man(page, number):
29 return '{}.{}'.format(page, number)
32 return os.path.basename(file)
34 def add_rules(rules, name):
36 # print('parsing {}'.format(name), file=sys.stderr)
37 if xml.getroot().tag != 'refentry':
39 conditional = xml.getroot().get('conditional') or ''
40 rulegroup = rules[conditional]
41 refmeta = xml.find('./refmeta')
42 title = refmeta.find('./refentrytitle').text
43 number = refmeta.find('./manvolnum').text
44 refnames = xml.findall('./refnamediv/refname')
45 target = man(refnames[0].text, number)
46 if title != refnames[0].text:
47 raise ValueError('refmeta and refnamediv disagree: ' + name)
48 for refname in refnames:
49 assert all(refname not in group
50 for group in rules.values()), "duplicate page name"
51 alias = man(refname.text, number)
52 rulegroup[alias] = target
53 # print('{} => {} [{}]'.format(alias, target, conditional), file=sys.stderr)
55 def create_rules(xml_files):
56 " {conditional => {alias-name => source-name}} "
57 rules = collections.defaultdict(dict)
58 for name in xml_files:
60 add_rules(rules, name)
62 print("Failed to process", name, file=sys.stderr)
67 return ' \\\n\t'.join(sorted(files) or '#')
70 # Do not edit. Generated by make-man-rules.py.
75 # Really, do not edit.'''
77 def make_mesonfile(rules, dist_files):
79 # grouped = [ [name, section, [alias...], condition], ...]
81 # but first create a dictionary like
82 # lists = { (name, condition) => [alias...]
83 grouped = collections.defaultdict(list)
84 for condition, items in rules.items():
85 for alias, name in items.items():
86 group = grouped[(name, condition)]
90 lines = [ [p[0][:-2], p[0][-1], sorted(a[:-2] for a in aliases), p[1]]
91 for p, aliases in sorted(grouped.items()) ]
92 return '\n'.join((MESON_HEADER, pprint.pformat(lines)[1:-1], MESON_FOOTER))
94 if __name__ == '__main__':
97 rules = create_rules(pages)
98 dist_files = (xml(file) for file in pages
99 if not file.endswith(".directives.xml") and
100 not file.endswith(".index.xml"))
101 print(make_mesonfile(rules, dist_files))