Jörg Thalheim | 3e67e5c | 2017-05-01 02:26:56 +0200 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Zbigniew Jędrzejewski-Szmek | 56ba3c7 | 2013-02-02 22:47:47 -0500 | [diff] [blame] | 2 | # -*- Mode: python; coding: utf-8; indent-tabs-mode: nil -*- */ |
| 3 | # |
| 4 | # This file is part of systemd. |
| 5 | # |
Zbigniew Jędrzejewski-Szmek | e2bb410 | 2017-04-04 23:21:04 -0400 | [diff] [blame] | 6 | # Copyright 2013, 2017 Zbigniew Jędrzejewski-Szmek |
Zbigniew Jędrzejewski-Szmek | 56ba3c7 | 2013-02-02 22:47:47 -0500 | [diff] [blame] | 7 | # |
| 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. |
| 12 | # |
| 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. |
| 17 | # |
| 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/>. |
| 20 | |
| 21 | from __future__ import print_function |
Zbigniew Jędrzejewski-Szmek | 56ba3c7 | 2013-02-02 22:47:47 -0500 | [diff] [blame] | 22 | import collections |
| 23 | import sys |
Zbigniew Jędrzejewski-Szmek | 40be878 | 2013-12-21 18:11:35 -0500 | [diff] [blame] | 24 | import os.path |
Zbigniew Jędrzejewski-Szmek | e2bb410 | 2017-04-04 23:21:04 -0400 | [diff] [blame] | 25 | import pprint |
Zbigniew Jędrzejewski-Szmek | 1a13e31 | 2013-03-29 14:22:27 -0400 | [diff] [blame] | 26 | from xml_helper import * |
Zbigniew Jędrzejewski-Szmek | 56ba3c7 | 2013-02-02 22:47:47 -0500 | [diff] [blame] | 27 | |
Zbigniew Jędrzejewski-Szmek | 56ba3c7 | 2013-02-02 22:47:47 -0500 | [diff] [blame] | 28 | def man(page, number): |
Zbigniew Jędrzejewski-Szmek | 0689f76 | 2017-07-02 20:22:35 -0400 | [diff] [blame^] | 29 | return '{}.{}'.format(page, number) |
Zbigniew Jędrzejewski-Szmek | 56ba3c7 | 2013-02-02 22:47:47 -0500 | [diff] [blame] | 30 | |
Zbigniew Jędrzejewski-Szmek | 40be878 | 2013-12-21 18:11:35 -0500 | [diff] [blame] | 31 | def xml(file): |
Zbigniew Jędrzejewski-Szmek | 0689f76 | 2017-07-02 20:22:35 -0400 | [diff] [blame^] | 32 | return os.path.basename(file) |
Zbigniew Jędrzejewski-Szmek | 40be878 | 2013-12-21 18:11:35 -0500 | [diff] [blame] | 33 | |
Zbigniew Jędrzejewski-Szmek | 56ba3c7 | 2013-02-02 22:47:47 -0500 | [diff] [blame] | 34 | def add_rules(rules, name): |
Zbigniew Jędrzejewski-Szmek | 1a13e31 | 2013-03-29 14:22:27 -0400 | [diff] [blame] | 35 | xml = xml_parse(name) |
Zbigniew Jędrzejewski-Szmek | 56ba3c7 | 2013-02-02 22:47:47 -0500 | [diff] [blame] | 36 | # print('parsing {}'.format(name), file=sys.stderr) |
Zbigniew Jędrzejewski-Szmek | c0652d4 | 2014-02-19 16:06:10 -0500 | [diff] [blame] | 37 | if xml.getroot().tag != 'refentry': |
| 38 | return |
Zbigniew Jędrzejewski-Szmek | 56ba3c7 | 2013-02-02 22:47:47 -0500 | [diff] [blame] | 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) |
| 54 | |
Zbigniew Jędrzejewski-Szmek | 40be878 | 2013-12-21 18:11:35 -0500 | [diff] [blame] | 55 | def create_rules(xml_files): |
Zbigniew Jędrzejewski-Szmek | 56ba3c7 | 2013-02-02 22:47:47 -0500 | [diff] [blame] | 56 | " {conditional => {alias-name => source-name}} " |
| 57 | rules = collections.defaultdict(dict) |
| 58 | for name in xml_files: |
Zbigniew Jędrzejewski-Szmek | c0652d4 | 2014-02-19 16:06:10 -0500 | [diff] [blame] | 59 | try: |
| 60 | add_rules(rules, name) |
| 61 | except Exception: |
| 62 | print("Failed to process", name, file=sys.stderr) |
| 63 | raise |
Zbigniew Jędrzejewski-Szmek | 56ba3c7 | 2013-02-02 22:47:47 -0500 | [diff] [blame] | 64 | return rules |
| 65 | |
| 66 | def mjoin(files): |
| 67 | return ' \\\n\t'.join(sorted(files) or '#') |
| 68 | |
Zbigniew Jędrzejewski-Szmek | e2bb410 | 2017-04-04 23:21:04 -0400 | [diff] [blame] | 69 | MESON_HEADER = '''\ |
| 70 | # Do not edit. Generated by make-man-rules.py. |
| 71 | manpages = [''' |
| 72 | |
| 73 | MESON_FOOTER = '''\ |
| 74 | ] |
| 75 | # Really, do not edit.''' |
| 76 | |
| 77 | def make_mesonfile(rules, dist_files): |
| 78 | # reformat rules as |
| 79 | # grouped = [ [name, section, [alias...], condition], ...] |
| 80 | # |
| 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)] |
| 87 | if name != alias: |
| 88 | group.append(alias) |
| 89 | |
| 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)) |
| 93 | |
Zbigniew Jędrzejewski-Szmek | 56ba3c7 | 2013-02-02 22:47:47 -0500 | [diff] [blame] | 94 | if __name__ == '__main__': |
Zbigniew Jędrzejewski-Szmek | 0689f76 | 2017-07-02 20:22:35 -0400 | [diff] [blame^] | 95 | pages = sys.argv[1:] |
Zbigniew Jędrzejewski-Szmek | e2bb410 | 2017-04-04 23:21:04 -0400 | [diff] [blame] | 96 | |
| 97 | rules = create_rules(pages) |
| 98 | dist_files = (xml(file) for file in pages |
Zbigniew Jędrzejewski-Szmek | e4f42f9 | 2015-06-30 09:56:44 -0400 | [diff] [blame] | 99 | if not file.endswith(".directives.xml") and |
| 100 | not file.endswith(".index.xml")) |
Zbigniew Jędrzejewski-Szmek | 0689f76 | 2017-07-02 20:22:35 -0400 | [diff] [blame^] | 101 | print(make_mesonfile(rules, dist_files)) |