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