]> git.ipfire.org Git - thirdparty/systemd.git/blame - tools/make-man-rules.py
build-sys: drop support for generation of Makefile-man.am
[thirdparty/systemd.git] / tools / make-man-rules.py
CommitLineData
3e67e5c9 1#!/usr/bin/env 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
21from __future__ import print_function
56ba3c78
ZJS
22import collections
23import sys
40be878a 24import os.path
e2bb4105 25import pprint
1a13e31d 26from xml_helper import *
56ba3c78 27
56ba3c78 28def man(page, number):
0689f766 29 return '{}.{}'.format(page, number)
56ba3c78 30
40be878a 31def xml(file):
0689f766 32 return os.path.basename(file)
40be878a 33
56ba3c78 34def add_rules(rules, name):
1a13e31d 35 xml = xml_parse(name)
56ba3c78 36 # print('parsing {}'.format(name), file=sys.stderr)
c0652d45
ZJS
37 if xml.getroot().tag != 'refentry':
38 return
56ba3c78
ZJS
39 conditional = xml.getroot().get('conditional') or ''
40 rulegroup = rules[conditional]
41 refmeta = xml.find('./refmeta')
42 title = refmeta.find('./refentrytitle').text
43 number = refmeta.find('./manvolnum').text
44 refnames = xml.findall('./refnamediv/refname')
45 target = man(refnames[0].text, number)
46 if title != refnames[0].text:
47 raise ValueError('refmeta and refnamediv disagree: ' + name)
48 for refname in refnames:
49 assert all(refname not in group
50 for group in rules.values()), "duplicate page name"
51 alias = man(refname.text, number)
52 rulegroup[alias] = target
53 # print('{} => {} [{}]'.format(alias, target, conditional), file=sys.stderr)
54
40be878a 55def create_rules(xml_files):
56ba3c78
ZJS
56 " {conditional => {alias-name => source-name}} "
57 rules = collections.defaultdict(dict)
58 for name in xml_files:
c0652d45
ZJS
59 try:
60 add_rules(rules, name)
61 except Exception:
62 print("Failed to process", name, file=sys.stderr)
63 raise
56ba3c78
ZJS
64 return rules
65
66def mjoin(files):
67 return ' \\\n\t'.join(sorted(files) or '#')
68
e2bb4105
ZJS
69MESON_HEADER = '''\
70# Do not edit. Generated by make-man-rules.py.
71manpages = ['''
72
73MESON_FOOTER = '''\
74]
75# Really, do not edit.'''
76
77def make_mesonfile(rules, dist_files):
78 # reformat rules as
79 # grouped = [ [name, section, [alias...], condition], ...]
80 #
81 # but first create a dictionary like
82 # lists = { (name, condition) => [alias...]
83 grouped = collections.defaultdict(list)
84 for condition, items in rules.items():
85 for alias, name in items.items():
86 group = grouped[(name, condition)]
87 if name != alias:
88 group.append(alias)
89
90 lines = [ [p[0][:-2], p[0][-1], sorted(a[:-2] for a in aliases), p[1]]
91 for p, aliases in sorted(grouped.items()) ]
92 return '\n'.join((MESON_HEADER, pprint.pformat(lines)[1:-1], MESON_FOOTER))
93
56ba3c78 94if __name__ == '__main__':
0689f766 95 pages = sys.argv[1:]
e2bb4105
ZJS
96
97 rules = create_rules(pages)
98 dist_files = (xml(file) for file in pages
e4f42f9d
ZJS
99 if not file.endswith(".directives.xml") and
100 not file.endswith(".index.xml"))
0689f766 101 print(make_mesonfile(rules, dist_files))