Zbigniew Jędrzejewski-Szmek | 56ba3c7 | 2013-02-02 22:47:47 -0500 | [diff] [blame^] | 1 | # -*- Mode: python; coding: utf-8; indent-tabs-mode: nil -*- */ |
| 2 | # |
| 3 | # This file is part of systemd. |
| 4 | # |
| 5 | # Copyright 2013 Zbigniew Jędrzejewski-Szmek |
| 6 | # |
| 7 | # systemd is free software; you can redistribute it and/or modify it |
| 8 | # under the terms of the GNU Lesser General Public License as published by |
| 9 | # the Free Software Foundation; either version 2.1 of the License, or |
| 10 | # (at your option) any later version. |
| 11 | # |
| 12 | # systemd is distributed in the hope that it will be useful, but |
| 13 | # WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | # Lesser General Public License for more details. |
| 16 | # |
| 17 | # You should have received a copy of the GNU Lesser General Public License |
| 18 | # along with systemd; If not, see <http://www.gnu.org/licenses/>. |
| 19 | |
| 20 | from __future__ import print_function |
| 21 | import xml.etree.ElementTree as tree |
| 22 | import collections |
| 23 | import sys |
| 24 | |
| 25 | SECTION = '''\ |
| 26 | MANPAGES += \\ |
| 27 | {manpages} |
| 28 | MANPAGES_ALIAS += \\ |
| 29 | {aliases} |
| 30 | {rules} |
| 31 | ''' |
| 32 | |
| 33 | CONDITIONAL = '''\ |
| 34 | if {conditional} |
| 35 | ''' \ |
| 36 | + SECTION + \ |
| 37 | '''\ |
| 38 | endif |
| 39 | ''' |
| 40 | |
| 41 | HEADER = '''\ |
| 42 | # Do not edit. Generated by make-man-rules.py. |
| 43 | # Regenerate with 'make update-man-list'. |
| 44 | |
| 45 | ''' |
| 46 | |
| 47 | def man(page, number): |
| 48 | return 'man/{}.{}'.format(page, number) |
| 49 | |
| 50 | def add_rules(rules, name): |
| 51 | xml = tree.parse(name) |
| 52 | # print('parsing {}'.format(name), file=sys.stderr) |
| 53 | conditional = xml.getroot().get('conditional') or '' |
| 54 | rulegroup = rules[conditional] |
| 55 | refmeta = xml.find('./refmeta') |
| 56 | title = refmeta.find('./refentrytitle').text |
| 57 | number = refmeta.find('./manvolnum').text |
| 58 | refnames = xml.findall('./refnamediv/refname') |
| 59 | target = man(refnames[0].text, number) |
| 60 | if title != refnames[0].text: |
| 61 | raise ValueError('refmeta and refnamediv disagree: ' + name) |
| 62 | for refname in refnames: |
| 63 | assert all(refname not in group |
| 64 | for group in rules.values()), "duplicate page name" |
| 65 | alias = man(refname.text, number) |
| 66 | rulegroup[alias] = target |
| 67 | # print('{} => {} [{}]'.format(alias, target, conditional), file=sys.stderr) |
| 68 | |
| 69 | def create_rules(*xml_files): |
| 70 | " {conditional => {alias-name => source-name}} " |
| 71 | rules = collections.defaultdict(dict) |
| 72 | for name in xml_files: |
| 73 | add_rules(rules, name) |
| 74 | return rules |
| 75 | |
| 76 | def mjoin(files): |
| 77 | return ' \\\n\t'.join(sorted(files) or '#') |
| 78 | |
| 79 | def make_makefile(rules): |
| 80 | return HEADER + '\n'.join( |
| 81 | (CONDITIONAL if conditional else SECTION).format( |
| 82 | manpages=mjoin(set(rulegroup.values())), |
| 83 | aliases=mjoin(k for k,v in rulegroup.items() if k != v), |
| 84 | rules='\n'.join('{}: {}'.format(k,v) |
| 85 | for k,v in sorted(rulegroup.items()) |
| 86 | if k != v), |
| 87 | conditional=conditional) |
| 88 | for conditional,rulegroup in sorted(rules.items())) |
| 89 | |
| 90 | if __name__ == '__main__': |
| 91 | rules = create_rules(*sys.argv[1:]) |
| 92 | print(make_makefile(rules), end='') |