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