]>
Commit | Line | Data |
---|---|---|
3e67e5c9 | 1 | #!/usr/bin/env python3 |
56ba3c78 | 2 | # -*- Mode: python; coding: utf-8; indent-tabs-mode: nil -*- */ |
35df7443 | 3 | # SPDX-License-Identifier: LGPL-2.1+ |
56ba3c78 ZJS |
4 | # |
5 | # This file is part of systemd. | |
6 | # | |
e2bb4105 | 7 | # Copyright 2013, 2017 Zbigniew Jędrzejewski-Szmek |
56ba3c78 ZJS |
8 | |
9 | from __future__ import print_function | |
56ba3c78 ZJS |
10 | import collections |
11 | import sys | |
40be878a | 12 | import os.path |
e2bb4105 | 13 | import pprint |
1c6c3ef0 | 14 | from xml_helper import xml_parse |
56ba3c78 | 15 | |
56ba3c78 | 16 | def man(page, number): |
0689f766 | 17 | return '{}.{}'.format(page, number) |
56ba3c78 | 18 | |
40be878a | 19 | def xml(file): |
0689f766 | 20 | return os.path.basename(file) |
40be878a | 21 | |
56ba3c78 | 22 | def add_rules(rules, name): |
1a13e31d | 23 | xml = xml_parse(name) |
56ba3c78 | 24 | # print('parsing {}'.format(name), file=sys.stderr) |
c0652d45 ZJS |
25 | if xml.getroot().tag != 'refentry': |
26 | return | |
56ba3c78 ZJS |
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 | ||
40be878a | 43 | def create_rules(xml_files): |
56ba3c78 ZJS |
44 | " {conditional => {alias-name => source-name}} " |
45 | rules = collections.defaultdict(dict) | |
46 | for name in xml_files: | |
c0652d45 ZJS |
47 | try: |
48 | add_rules(rules, name) | |
49 | except Exception: | |
50 | print("Failed to process", name, file=sys.stderr) | |
51 | raise | |
56ba3c78 ZJS |
52 | return rules |
53 | ||
54 | def mjoin(files): | |
55 | return ' \\\n\t'.join(sorted(files) or '#') | |
56 | ||
e2bb4105 ZJS |
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 | ||
56ba3c78 | 82 | if __name__ == '__main__': |
0689f766 | 83 | pages = sys.argv[1:] |
e2bb4105 ZJS |
84 | |
85 | rules = create_rules(pages) | |
86 | dist_files = (xml(file) for file in pages | |
e4f42f9d ZJS |
87 | if not file.endswith(".directives.xml") and |
88 | not file.endswith(".index.xml")) | |
0689f766 | 89 | print(make_mesonfile(rules, dist_files)) |