]> git.ipfire.org Git - thirdparty/systemd.git/blame - tools/make-man-rules.py
core: s/reexection/reexecution/ typo fix
[thirdparty/systemd.git] / tools / make-man-rules.py
CommitLineData
56ba3c78
ZJS
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
20from __future__ import print_function
56ba3c78
ZJS
21import collections
22import sys
40be878a 23import os.path
1a13e31d 24from xml_helper import *
56ba3c78
ZJS
25
26SECTION = '''\
27MANPAGES += \\
28 {manpages}
29MANPAGES_ALIAS += \\
30 {aliases}
31{rules}
87cfe600 32{htmlrules}
56ba3c78
ZJS
33'''
34
35CONDITIONAL = '''\
36if {conditional}
37''' \
38+ SECTION + \
39'''\
40endif
41'''
42
43HEADER = '''\
44# Do not edit. Generated by make-man-rules.py.
d86dd07d
ZJS
45# To regenerate:
46# 1. Create, update, or remove source .xml files in man/
47# 2. Run 'make update-man-list'
48# 3. Run 'make man' to generate manpages
49#
50# To make a man page conditional on a configure switch add
51# attribute conditional="ENABLE_WHAT" or conditional="WITH_WHAT"
52# to <refentry> element.
56ba3c78
ZJS
53'''
54
87cfe600
ZJS
55HTML_ALIAS_RULE = '''\
56{}.html: {}.html
57 $(html-alias)
58'''
59
40be878a
ZJS
60FOOTER = '''\
61
d86dd07d
ZJS
62# Really, do not edit this file.
63
40be878a 64EXTRA_DIST += \\
e4f42f9d 65 {dist_files}
40be878a
ZJS
66'''
67
56ba3c78
ZJS
68def man(page, number):
69 return 'man/{}.{}'.format(page, number)
70
40be878a
ZJS
71def xml(file):
72 return 'man/{}'.format(os.path.basename(file))
73
56ba3c78 74def add_rules(rules, name):
1a13e31d 75 xml = xml_parse(name)
56ba3c78 76 # print('parsing {}'.format(name), file=sys.stderr)
c0652d45
ZJS
77 if xml.getroot().tag != 'refentry':
78 return
56ba3c78
ZJS
79 conditional = xml.getroot().get('conditional') or ''
80 rulegroup = rules[conditional]
81 refmeta = xml.find('./refmeta')
82 title = refmeta.find('./refentrytitle').text
83 number = refmeta.find('./manvolnum').text
84 refnames = xml.findall('./refnamediv/refname')
85 target = man(refnames[0].text, number)
86 if title != refnames[0].text:
87 raise ValueError('refmeta and refnamediv disagree: ' + name)
88 for refname in refnames:
89 assert all(refname not in group
90 for group in rules.values()), "duplicate page name"
91 alias = man(refname.text, number)
92 rulegroup[alias] = target
93 # print('{} => {} [{}]'.format(alias, target, conditional), file=sys.stderr)
94
40be878a 95def create_rules(xml_files):
56ba3c78
ZJS
96 " {conditional => {alias-name => source-name}} "
97 rules = collections.defaultdict(dict)
98 for name in xml_files:
c0652d45
ZJS
99 try:
100 add_rules(rules, name)
101 except Exception:
102 print("Failed to process", name, file=sys.stderr)
103 raise
56ba3c78
ZJS
104 return rules
105
106def mjoin(files):
107 return ' \\\n\t'.join(sorted(files) or '#')
108
e4f42f9d 109def make_makefile(rules, dist_files):
56ba3c78
ZJS
110 return HEADER + '\n'.join(
111 (CONDITIONAL if conditional else SECTION).format(
112 manpages=mjoin(set(rulegroup.values())),
113 aliases=mjoin(k for k,v in rulegroup.items() if k != v),
114 rules='\n'.join('{}: {}'.format(k,v)
115 for k,v in sorted(rulegroup.items())
116 if k != v),
87cfe600
ZJS
117 htmlrules='\n'.join(HTML_ALIAS_RULE.format(k[:-2],v[:-2])
118 for k,v in sorted(rulegroup.items())
119 if k != v),
56ba3c78 120 conditional=conditional)
40be878a 121 for conditional,rulegroup in sorted(rules.items())
e4f42f9d 122 ) + FOOTER.format(dist_files=mjoin(sorted(dist_files)))
56ba3c78
ZJS
123
124if __name__ == '__main__':
40be878a 125 rules = create_rules(sys.argv[1:])
e4f42f9d
ZJS
126 dist_files = (xml(file) for file in sys.argv[1:]
127 if not file.endswith(".directives.xml") and
128 not file.endswith(".index.xml"))
129 print(make_makefile(rules, dist_files), end='')