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 |
Zbigniew Jędrzejewski-Szmek | 185c3be | 2013-02-27 20:16:38 -0500 | [diff] [blame^] | 24 | import os |
Zbigniew Jędrzejewski-Szmek | 56ba3c7 | 2013-02-02 22:47:47 -0500 | [diff] [blame] | 25 | |
| 26 | SECTION = '''\ |
| 27 | MANPAGES += \\ |
| 28 | {manpages} |
| 29 | MANPAGES_ALIAS += \\ |
| 30 | {aliases} |
| 31 | {rules} |
| 32 | ''' |
| 33 | |
| 34 | CONDITIONAL = '''\ |
| 35 | if {conditional} |
| 36 | ''' \ |
| 37 | + SECTION + \ |
| 38 | '''\ |
| 39 | endif |
| 40 | ''' |
| 41 | |
| 42 | HEADER = '''\ |
| 43 | # Do not edit. Generated by make-man-rules.py. |
Zbigniew Jędrzejewski-Szmek | 185c3be | 2013-02-27 20:16:38 -0500 | [diff] [blame^] | 44 | # Regenerate with 'make all update-man-list'. |
Zbigniew Jędrzejewski-Szmek | 56ba3c7 | 2013-02-02 22:47:47 -0500 | [diff] [blame] | 45 | |
| 46 | ''' |
| 47 | |
Zbigniew Jędrzejewski-Szmek | c78ab91 | 2013-02-05 10:31:20 -0500 | [diff] [blame] | 48 | CLEANFILES = '''\ |
| 49 | |
| 50 | CLEANFILES += \\ |
| 51 | {cleanfiles} |
| 52 | ''' |
| 53 | |
Zbigniew Jędrzejewski-Szmek | 56ba3c7 | 2013-02-02 22:47:47 -0500 | [diff] [blame] | 54 | def man(page, number): |
| 55 | return 'man/{}.{}'.format(page, number) |
| 56 | |
| 57 | def add_rules(rules, name): |
| 58 | xml = tree.parse(name) |
| 59 | # print('parsing {}'.format(name), file=sys.stderr) |
| 60 | conditional = xml.getroot().get('conditional') or '' |
| 61 | rulegroup = rules[conditional] |
| 62 | refmeta = xml.find('./refmeta') |
| 63 | title = refmeta.find('./refentrytitle').text |
| 64 | number = refmeta.find('./manvolnum').text |
| 65 | refnames = xml.findall('./refnamediv/refname') |
| 66 | target = man(refnames[0].text, number) |
| 67 | if title != refnames[0].text: |
| 68 | raise ValueError('refmeta and refnamediv disagree: ' + name) |
| 69 | for refname in refnames: |
| 70 | assert all(refname not in group |
| 71 | for group in rules.values()), "duplicate page name" |
| 72 | alias = man(refname.text, number) |
| 73 | rulegroup[alias] = target |
| 74 | # print('{} => {} [{}]'.format(alias, target, conditional), file=sys.stderr) |
| 75 | |
| 76 | def create_rules(*xml_files): |
| 77 | " {conditional => {alias-name => source-name}} " |
| 78 | rules = collections.defaultdict(dict) |
| 79 | for name in xml_files: |
| 80 | add_rules(rules, name) |
| 81 | return rules |
| 82 | |
| 83 | def mjoin(files): |
| 84 | return ' \\\n\t'.join(sorted(files) or '#') |
| 85 | |
Zbigniew Jędrzejewski-Szmek | c78ab91 | 2013-02-05 10:31:20 -0500 | [diff] [blame] | 86 | def make_makefile(rules, cleanfiles): |
Zbigniew Jędrzejewski-Szmek | 56ba3c7 | 2013-02-02 22:47:47 -0500 | [diff] [blame] | 87 | return HEADER + '\n'.join( |
| 88 | (CONDITIONAL if conditional else SECTION).format( |
| 89 | manpages=mjoin(set(rulegroup.values())), |
| 90 | aliases=mjoin(k for k,v in rulegroup.items() if k != v), |
| 91 | rules='\n'.join('{}: {}'.format(k,v) |
| 92 | for k,v in sorted(rulegroup.items()) |
| 93 | if k != v), |
| 94 | conditional=conditional) |
Zbigniew Jędrzejewski-Szmek | c78ab91 | 2013-02-05 10:31:20 -0500 | [diff] [blame] | 95 | for conditional,rulegroup in sorted(rules.items())) + \ |
| 96 | CLEANFILES.format(cleanfiles=mjoin(cleanfiles)) |
Zbigniew Jędrzejewski-Szmek | 56ba3c7 | 2013-02-02 22:47:47 -0500 | [diff] [blame] | 97 | |
| 98 | if __name__ == '__main__': |
Zbigniew Jędrzejewski-Szmek | c78ab91 | 2013-02-05 10:31:20 -0500 | [diff] [blame] | 99 | sources = set(sys.argv[1:]) |
Zbigniew Jędrzejewski-Szmek | 185c3be | 2013-02-27 20:16:38 -0500 | [diff] [blame^] | 100 | basenames = [os.path.basename(source) for source in sources] |
Zbigniew Jędrzejewski-Szmek | c78ab91 | 2013-02-05 10:31:20 -0500 | [diff] [blame] | 101 | spares = set([source for source in sources |
Zbigniew Jędrzejewski-Szmek | 185c3be | 2013-02-27 20:16:38 -0500 | [diff] [blame^] | 102 | if os.path.basename(source) + '.in' in basenames]) |
Zbigniew Jędrzejewski-Szmek | c78ab91 | 2013-02-05 10:31:20 -0500 | [diff] [blame] | 103 | rules = create_rules(*(sources - spares)) |
| 104 | print(make_makefile(rules, spares), end='') |