chiark / gitweb /
General: Update build system to upstream support of meson+ninja.
[elogind.git] / tools / make-man-rules.py
1 #!/usr/bin/env python3
2 #  -*- Mode: python; coding: utf-8; indent-tabs-mode: nil -*- */
3 #
4 #  This file is part of systemd.
5 #
6 #  Copyright 2013, 2017 Zbigniew JÄ™drzejewski-Szmek
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
22 import collections
23 import sys
24 import os.path
25 import pprint
26 from xml_helper import xml_parse
27
28 def man(page, number):
29     return '{}.{}'.format(page, number)
30
31 def xml(file):
32     return os.path.basename(file)
33
34 def add_rules(rules, name):
35     xml = xml_parse(name)
36     # print('parsing {}'.format(name), file=sys.stderr)
37     if xml.getroot().tag != 'refentry':
38         return
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
55 def create_rules(xml_files):
56     " {conditional => {alias-name => source-name}} "
57     rules = collections.defaultdict(dict)
58     for name in xml_files:
59         try:
60             add_rules(rules, name)
61         except Exception:
62             print("Failed to process", name, file=sys.stderr)
63             raise
64     return rules
65
66 def mjoin(files):
67     return ' \\\n\t'.join(sorted(files) or '#')
68
69 MESON_HEADER = '''\
70 # Do not edit. Generated by make-man-rules.py.
71 manpages = ['''
72
73 MESON_FOOTER = '''\
74 ]
75 # Really, do not edit.'''
76
77 def 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
94 if __name__ == '__main__':
95     pages = sys.argv[1:]
96
97     rules = create_rules(pages)
98     dist_files = (xml(file) for file in pages
99                   if not file.endswith(".directives.xml") and
100                      not file.endswith(".index.xml"))
101     print(make_mesonfile(rules, dist_files))