]> git.ipfire.org Git - thirdparty/systemd.git/blame - tools/make-man-rules.py
sd-bus: if we receive an invalid dbus message, ignore and proceeed
[thirdparty/systemd.git] / tools / make-man-rules.py
CommitLineData
3e67e5c9 1#!/usr/bin/env python3
d8a0bcfd 2# SPDX-License-Identifier: LGPL-2.1+
56ba3c78
ZJS
3
4from __future__ import print_function
56ba3c78
ZJS
5import collections
6import sys
40be878a 7import os.path
e2bb4105 8import pprint
1c6c3ef0 9from xml_helper import xml_parse
56ba3c78 10
56ba3c78 11def man(page, number):
0689f766 12 return '{}.{}'.format(page, number)
56ba3c78 13
40be878a 14def xml(file):
0689f766 15 return os.path.basename(file)
40be878a 16
56ba3c78 17def add_rules(rules, name):
1a13e31d 18 xml = xml_parse(name)
56ba3c78 19 # print('parsing {}'.format(name), file=sys.stderr)
c0652d45
ZJS
20 if xml.getroot().tag != 'refentry':
21 return
56ba3c78
ZJS
22 conditional = xml.getroot().get('conditional') or ''
23 rulegroup = rules[conditional]
24 refmeta = xml.find('./refmeta')
25 title = refmeta.find('./refentrytitle').text
26 number = refmeta.find('./manvolnum').text
27 refnames = xml.findall('./refnamediv/refname')
28 target = man(refnames[0].text, number)
29 if title != refnames[0].text:
30 raise ValueError('refmeta and refnamediv disagree: ' + name)
31 for refname in refnames:
32 assert all(refname not in group
33 for group in rules.values()), "duplicate page name"
34 alias = man(refname.text, number)
35 rulegroup[alias] = target
36 # print('{} => {} [{}]'.format(alias, target, conditional), file=sys.stderr)
37
40be878a 38def create_rules(xml_files):
56ba3c78
ZJS
39 " {conditional => {alias-name => source-name}} "
40 rules = collections.defaultdict(dict)
41 for name in xml_files:
c0652d45
ZJS
42 try:
43 add_rules(rules, name)
44 except Exception:
45 print("Failed to process", name, file=sys.stderr)
46 raise
56ba3c78
ZJS
47 return rules
48
49def mjoin(files):
50 return ' \\\n\t'.join(sorted(files) or '#')
51
e2bb4105
ZJS
52MESON_HEADER = '''\
53# Do not edit. Generated by make-man-rules.py.
54manpages = ['''
55
56MESON_FOOTER = '''\
57]
58# Really, do not edit.'''
59
60def make_mesonfile(rules, dist_files):
61 # reformat rules as
62 # grouped = [ [name, section, [alias...], condition], ...]
63 #
64 # but first create a dictionary like
65 # lists = { (name, condition) => [alias...]
66 grouped = collections.defaultdict(list)
67 for condition, items in rules.items():
68 for alias, name in items.items():
69 group = grouped[(name, condition)]
70 if name != alias:
71 group.append(alias)
72
73 lines = [ [p[0][:-2], p[0][-1], sorted(a[:-2] for a in aliases), p[1]]
74 for p, aliases in sorted(grouped.items()) ]
75 return '\n'.join((MESON_HEADER, pprint.pformat(lines)[1:-1], MESON_FOOTER))
76
56ba3c78 77if __name__ == '__main__':
0689f766 78 pages = sys.argv[1:]
e2bb4105
ZJS
79
80 rules = create_rules(pages)
81 dist_files = (xml(file) for file in pages
e4f42f9d
ZJS
82 if not file.endswith(".directives.xml") and
83 not file.endswith(".index.xml"))
0689f766 84 print(make_mesonfile(rules, dist_files))