]> git.ipfire.org Git - thirdparty/systemd.git/blame - tools/make-man-rules.py
verbs: add a new VERB_MUSTBEROOT flag
[thirdparty/systemd.git] / tools / make-man-rules.py
CommitLineData
3e67e5c9 1#!/usr/bin/env python3
56ba3c78 2# -*- Mode: python; coding: utf-8; indent-tabs-mode: nil -*- */
35df7443 3# SPDX-License-Identifier: LGPL-2.1+
56ba3c78
ZJS
4#
5# This file is part of systemd.
6#
e2bb4105 7# Copyright 2013, 2017 Zbigniew Jędrzejewski-Szmek
56ba3c78
ZJS
8#
9# systemd is free software; you can redistribute it and/or modify it
10# under the terms of the GNU Lesser General Public License as published by
11# the Free Software Foundation; either version 2.1 of the License, or
12# (at your option) any later version.
13#
14# systemd is distributed in the hope that it will be useful, but
15# WITHOUT ANY WARRANTY; without even the implied warranty of
16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17# Lesser General Public License for more details.
18#
19# You should have received a copy of the GNU Lesser General Public License
20# along with systemd; If not, see <http://www.gnu.org/licenses/>.
21
22from __future__ import print_function
56ba3c78
ZJS
23import collections
24import sys
40be878a 25import os.path
e2bb4105 26import pprint
1c6c3ef0 27from xml_helper import xml_parse
56ba3c78 28
56ba3c78 29def man(page, number):
0689f766 30 return '{}.{}'.format(page, number)
56ba3c78 31
40be878a 32def xml(file):
0689f766 33 return os.path.basename(file)
40be878a 34
56ba3c78 35def add_rules(rules, name):
1a13e31d 36 xml = xml_parse(name)
56ba3c78 37 # print('parsing {}'.format(name), file=sys.stderr)
c0652d45
ZJS
38 if xml.getroot().tag != 'refentry':
39 return
56ba3c78
ZJS
40 conditional = xml.getroot().get('conditional') or ''
41 rulegroup = rules[conditional]
42 refmeta = xml.find('./refmeta')
43 title = refmeta.find('./refentrytitle').text
44 number = refmeta.find('./manvolnum').text
45 refnames = xml.findall('./refnamediv/refname')
46 target = man(refnames[0].text, number)
47 if title != refnames[0].text:
48 raise ValueError('refmeta and refnamediv disagree: ' + name)
49 for refname in refnames:
50 assert all(refname not in group
51 for group in rules.values()), "duplicate page name"
52 alias = man(refname.text, number)
53 rulegroup[alias] = target
54 # print('{} => {} [{}]'.format(alias, target, conditional), file=sys.stderr)
55
40be878a 56def create_rules(xml_files):
56ba3c78
ZJS
57 " {conditional => {alias-name => source-name}} "
58 rules = collections.defaultdict(dict)
59 for name in xml_files:
c0652d45
ZJS
60 try:
61 add_rules(rules, name)
62 except Exception:
63 print("Failed to process", name, file=sys.stderr)
64 raise
56ba3c78
ZJS
65 return rules
66
67def mjoin(files):
68 return ' \\\n\t'.join(sorted(files) or '#')
69
e2bb4105
ZJS
70MESON_HEADER = '''\
71# Do not edit. Generated by make-man-rules.py.
72manpages = ['''
73
74MESON_FOOTER = '''\
75]
76# Really, do not edit.'''
77
78def make_mesonfile(rules, dist_files):
79 # reformat rules as
80 # grouped = [ [name, section, [alias...], condition], ...]
81 #
82 # but first create a dictionary like
83 # lists = { (name, condition) => [alias...]
84 grouped = collections.defaultdict(list)
85 for condition, items in rules.items():
86 for alias, name in items.items():
87 group = grouped[(name, condition)]
88 if name != alias:
89 group.append(alias)
90
91 lines = [ [p[0][:-2], p[0][-1], sorted(a[:-2] for a in aliases), p[1]]
92 for p, aliases in sorted(grouped.items()) ]
93 return '\n'.join((MESON_HEADER, pprint.pformat(lines)[1:-1], MESON_FOOTER))
94
56ba3c78 95if __name__ == '__main__':
0689f766 96 pages = sys.argv[1:]
e2bb4105
ZJS
97
98 rules = create_rules(pages)
99 dist_files = (xml(file) for file in pages
e4f42f9d
ZJS
100 if not file.endswith(".directives.xml") and
101 not file.endswith(".index.xml"))
0689f766 102 print(make_mesonfile(rules, dist_files))