]> git.ipfire.org Git - thirdparty/systemd.git/blob - tools/make-man-rules.py
final v236 update (#7649)
[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 # systemd is free software; you can redistribute it and/or modify it
10 # under the terms of the GNU Lesser General Public License as published by
11 # the Free Software Foundation; either version 2.1 of the License, or
12 # (at your option) any later version.
13 #
14 # systemd is distributed in the hope that it will be useful, but
15 # WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 # Lesser General Public License for more details.
18 #
19 # You should have received a copy of the GNU Lesser General Public License
20 # along with systemd; If not, see <http://www.gnu.org/licenses/>.
21
22 from __future__ import print_function
23 import collections
24 import sys
25 import os.path
26 import pprint
27 from xml_helper import xml_parse
28
29 def man(page, number):
30 return '{}.{}'.format(page, number)
31
32 def xml(file):
33 return os.path.basename(file)
34
35 def add_rules(rules, name):
36 xml = xml_parse(name)
37 # print('parsing {}'.format(name), file=sys.stderr)
38 if xml.getroot().tag != 'refentry':
39 return
40 conditional = xml.getroot().get('conditional') or ''
41 rulegroup = rules[conditional]
42 refmeta = xml.find('./refmeta')
43 title = refmeta.find('./refentrytitle').text
44 number = refmeta.find('./manvolnum').text
45 refnames = xml.findall('./refnamediv/refname')
46 target = man(refnames[0].text, number)
47 if title != refnames[0].text:
48 raise ValueError('refmeta and refnamediv disagree: ' + name)
49 for refname in refnames:
50 assert all(refname not in group
51 for group in rules.values()), "duplicate page name"
52 alias = man(refname.text, number)
53 rulegroup[alias] = target
54 # print('{} => {} [{}]'.format(alias, target, conditional), file=sys.stderr)
55
56 def create_rules(xml_files):
57 " {conditional => {alias-name => source-name}} "
58 rules = collections.defaultdict(dict)
59 for name in xml_files:
60 try:
61 add_rules(rules, name)
62 except Exception:
63 print("Failed to process", name, file=sys.stderr)
64 raise
65 return rules
66
67 def mjoin(files):
68 return ' \\\n\t'.join(sorted(files) or '#')
69
70 MESON_HEADER = '''\
71 # Do not edit. Generated by make-man-rules.py.
72 manpages = ['''
73
74 MESON_FOOTER = '''\
75 ]
76 # Really, do not edit.'''
77
78 def make_mesonfile(rules, dist_files):
79 # reformat rules as
80 # grouped = [ [name, section, [alias...], condition], ...]
81 #
82 # but first create a dictionary like
83 # lists = { (name, condition) => [alias...]
84 grouped = collections.defaultdict(list)
85 for condition, items in rules.items():
86 for alias, name in items.items():
87 group = grouped[(name, condition)]
88 if name != alias:
89 group.append(alias)
90
91 lines = [ [p[0][:-2], p[0][-1], sorted(a[:-2] for a in aliases), p[1]]
92 for p, aliases in sorted(grouped.items()) ]
93 return '\n'.join((MESON_HEADER, pprint.pformat(lines)[1:-1], MESON_FOOTER))
94
95 if __name__ == '__main__':
96 pages = sys.argv[1:]
97
98 rules = create_rules(pages)
99 dist_files = (xml(file) for file in pages
100 if not file.endswith(".directives.xml") and
101 not file.endswith(".index.xml"))
102 print(make_mesonfile(rules, dist_files))