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