]>
Commit | Line | Data |
---|---|---|
b95f5528 | 1 | #!/usr/bin/python3 |
56ba3c78 ZJS |
2 | # -*- Mode: python; coding: utf-8; indent-tabs-mode: nil -*- */ |
3 | # | |
4 | # This file is part of systemd. | |
5 | # | |
e2bb4105 | 6 | # Copyright 2013, 2017 Zbigniew Jędrzejewski-Szmek |
56ba3c78 ZJS |
7 | # |
8 | # systemd is free software; you can redistribute it and/or modify it | |
9 | # under the terms of the GNU Lesser General Public License as published by | |
10 | # the Free Software Foundation; either version 2.1 of the License, or | |
11 | # (at your option) any later version. | |
12 | # | |
13 | # systemd is distributed in the hope that it will be useful, but | |
14 | # WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
16 | # Lesser General Public License for more details. | |
17 | # | |
18 | # You should have received a copy of the GNU Lesser General Public License | |
19 | # along with systemd; If not, see <http://www.gnu.org/licenses/>. | |
20 | ||
21 | from __future__ import print_function | |
56ba3c78 ZJS |
22 | import collections |
23 | import sys | |
40be878a | 24 | import os.path |
e2bb4105 | 25 | import pprint |
1a13e31d | 26 | from xml_helper import * |
56ba3c78 ZJS |
27 | |
28 | SECTION = '''\ | |
29 | MANPAGES += \\ | |
30 | {manpages} | |
31 | MANPAGES_ALIAS += \\ | |
32 | {aliases} | |
33 | {rules} | |
87cfe600 | 34 | {htmlrules} |
56ba3c78 ZJS |
35 | ''' |
36 | ||
37 | CONDITIONAL = '''\ | |
38 | if {conditional} | |
39 | ''' \ | |
40 | + SECTION + \ | |
41 | '''\ | |
42 | endif | |
43 | ''' | |
44 | ||
45 | HEADER = '''\ | |
46 | # Do not edit. Generated by make-man-rules.py. | |
d86dd07d ZJS |
47 | # To regenerate: |
48 | # 1. Create, update, or remove source .xml files in man/ | |
49 | # 2. Run 'make update-man-list' | |
50 | # 3. Run 'make man' to generate manpages | |
51 | # | |
52 | # To make a man page conditional on a configure switch add | |
53 | # attribute conditional="ENABLE_WHAT" or conditional="WITH_WHAT" | |
54 | # to <refentry> element. | |
56ba3c78 ZJS |
55 | ''' |
56 | ||
87cfe600 ZJS |
57 | HTML_ALIAS_RULE = '''\ |
58 | {}.html: {}.html | |
59 | $(html-alias) | |
60 | ''' | |
61 | ||
40be878a ZJS |
62 | FOOTER = '''\ |
63 | ||
d86dd07d ZJS |
64 | # Really, do not edit this file. |
65 | ||
40be878a | 66 | EXTRA_DIST += \\ |
e4f42f9d | 67 | {dist_files} |
40be878a ZJS |
68 | ''' |
69 | ||
e2bb4105 ZJS |
70 | meson = False |
71 | ||
56ba3c78 | 72 | def man(page, number): |
e2bb4105 | 73 | return ('man/' if not meson else '') + '{}.{}'.format(page, number) |
56ba3c78 | 74 | |
40be878a | 75 | def xml(file): |
e2bb4105 | 76 | return ('man/' if not meson else '') + os.path.basename(file) |
40be878a | 77 | |
56ba3c78 | 78 | def add_rules(rules, name): |
1a13e31d | 79 | xml = xml_parse(name) |
56ba3c78 | 80 | # print('parsing {}'.format(name), file=sys.stderr) |
c0652d45 ZJS |
81 | if xml.getroot().tag != 'refentry': |
82 | return | |
56ba3c78 ZJS |
83 | conditional = xml.getroot().get('conditional') or '' |
84 | rulegroup = rules[conditional] | |
85 | refmeta = xml.find('./refmeta') | |
86 | title = refmeta.find('./refentrytitle').text | |
87 | number = refmeta.find('./manvolnum').text | |
88 | refnames = xml.findall('./refnamediv/refname') | |
89 | target = man(refnames[0].text, number) | |
90 | if title != refnames[0].text: | |
91 | raise ValueError('refmeta and refnamediv disagree: ' + name) | |
92 | for refname in refnames: | |
93 | assert all(refname not in group | |
94 | for group in rules.values()), "duplicate page name" | |
95 | alias = man(refname.text, number) | |
96 | rulegroup[alias] = target | |
97 | # print('{} => {} [{}]'.format(alias, target, conditional), file=sys.stderr) | |
98 | ||
40be878a | 99 | def create_rules(xml_files): |
56ba3c78 ZJS |
100 | " {conditional => {alias-name => source-name}} " |
101 | rules = collections.defaultdict(dict) | |
102 | for name in xml_files: | |
c0652d45 ZJS |
103 | try: |
104 | add_rules(rules, name) | |
105 | except Exception: | |
106 | print("Failed to process", name, file=sys.stderr) | |
107 | raise | |
56ba3c78 ZJS |
108 | return rules |
109 | ||
110 | def mjoin(files): | |
111 | return ' \\\n\t'.join(sorted(files) or '#') | |
112 | ||
e4f42f9d | 113 | def make_makefile(rules, dist_files): |
56ba3c78 ZJS |
114 | return HEADER + '\n'.join( |
115 | (CONDITIONAL if conditional else SECTION).format( | |
116 | manpages=mjoin(set(rulegroup.values())), | |
117 | aliases=mjoin(k for k,v in rulegroup.items() if k != v), | |
118 | rules='\n'.join('{}: {}'.format(k,v) | |
119 | for k,v in sorted(rulegroup.items()) | |
120 | if k != v), | |
87cfe600 ZJS |
121 | htmlrules='\n'.join(HTML_ALIAS_RULE.format(k[:-2],v[:-2]) |
122 | for k,v in sorted(rulegroup.items()) | |
123 | if k != v), | |
56ba3c78 | 124 | conditional=conditional) |
40be878a | 125 | for conditional,rulegroup in sorted(rules.items()) |
e4f42f9d | 126 | ) + FOOTER.format(dist_files=mjoin(sorted(dist_files))) |
56ba3c78 | 127 | |
e2bb4105 ZJS |
128 | MESON_HEADER = '''\ |
129 | # Do not edit. Generated by make-man-rules.py. | |
130 | manpages = [''' | |
131 | ||
132 | MESON_FOOTER = '''\ | |
133 | ] | |
134 | # Really, do not edit.''' | |
135 | ||
136 | def make_mesonfile(rules, dist_files): | |
137 | # reformat rules as | |
138 | # grouped = [ [name, section, [alias...], condition], ...] | |
139 | # | |
140 | # but first create a dictionary like | |
141 | # lists = { (name, condition) => [alias...] | |
142 | grouped = collections.defaultdict(list) | |
143 | for condition, items in rules.items(): | |
144 | for alias, name in items.items(): | |
145 | group = grouped[(name, condition)] | |
146 | if name != alias: | |
147 | group.append(alias) | |
148 | ||
149 | lines = [ [p[0][:-2], p[0][-1], sorted(a[:-2] for a in aliases), p[1]] | |
150 | for p, aliases in sorted(grouped.items()) ] | |
151 | return '\n'.join((MESON_HEADER, pprint.pformat(lines)[1:-1], MESON_FOOTER)) | |
152 | ||
56ba3c78 | 153 | if __name__ == '__main__': |
e2bb4105 ZJS |
154 | meson = sys.argv[1] == '--meson' |
155 | pages = sys.argv[1+meson:] | |
156 | ||
157 | rules = create_rules(pages) | |
158 | dist_files = (xml(file) for file in pages | |
e4f42f9d ZJS |
159 | if not file.endswith(".directives.xml") and |
160 | not file.endswith(".index.xml")) | |
e2bb4105 ZJS |
161 | if meson: |
162 | print(make_mesonfile(rules, dist_files)) | |
163 | else: | |
164 | print(make_makefile(rules, dist_files), end='') |