File: MakeSingleHeader.py

package info (click to toggle)
cli11 2.4.1%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 2,120 kB
  • sloc: cpp: 23,299; python: 129; sh: 64; makefile: 11; ruby: 7
file content (160 lines) | stat: -rwxr-xr-x 4,895 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#!/usr/bin/env python

from __future__ import print_function, unicode_literals

import os
import re
import argparse
from subprocess import Popen, PIPE
import warnings

tag_str = r"""
^                  # Begin of line
[/\s]+             # Whitespace or comment // chars
\[                 # A literal [
{tag}:             # The tag
(?P<name>[\w_]+)   # name: group name
:                  # Colon
(?P<action>[\w_]+) # action: type of include
\]                 # A literal ]
\s*                # Whitespace
$                  # End of a line

(?P<content>.*)    # All

^                  # Begin of line
[/\s]+             # Whitespace or comment // chars
\[                 # A literal [
{tag}:             # The tag
(?P=name)          # Repeated name
:                  # Colon
end                # Literal "end"
\]                 # A literal ]
\s*                # Whitespace
$                  # End of a line
"""

DIR = os.path.dirname(os.path.abspath(__file__))


class HeaderGroups(dict):
    def __init__(self, tag):
        """
        A dictionary that also can read headers given a tag expression.

        TODO: might have gone overboard on this one, could maybe be two functions.
        """
        self.re_matcher = re.compile(
            tag_str.format(tag=tag), re.MULTILINE | re.DOTALL | re.VERBOSE
        )
        super(HeaderGroups, self).__init__()

    def read_header(self, filename):
        """
        Read a header file in and add items to the dict, based on the item's action.
        """
        with open(filename) as f:
            inner = f.read()

        matches = self.re_matcher.findall(inner)

        if not matches:
            warnings.warn(
                "Failed to find any matches in {filename}".format(filename=filename)
            )

        for name, action, content in matches:
            if action == "verbatim":
                assert (
                    name not in self
                ), "{name} read in more than once! Quitting.".format(name=name)
                self[name] = content
            elif action == "set":
                self[name] = self.get(name, set()) | set(content.strip().splitlines())
            else:
                raise RuntimeError("Action not understood, must be verbatim or set")

    def post_process(self):
        """
        Turn sets into multiple line strings.
        """
        for key in self:
            if isinstance(self[key], set):
                self[key] = "\n".join(sorted(self[key]))


def make_header(output, main_header, files, tag, namespace, macro=None, version=None):
    """
    Makes a single header given a main header template and a list of files.
    """
    groups = HeaderGroups(tag)

    # Set tag if possible to class variable
    try:
        proc = Popen(
            ["git", "describe", "--tags", "--always"], cwd=str(DIR), stdout=PIPE
        )
        out, _ = proc.communicate()
        groups["git"] = out.decode("utf-8").strip() if proc.returncode == 0 else ""
    except OSError:
        groups["git"] = ""

    for f in files:
        if os.path.isdir(f):
            continue
        groups.read_header(f)

    groups["namespace"] = namespace
    groups["version"] = version or groups["git"]

    groups.post_process()

    with open(main_header) as f:
        single_header = f.read().format(**groups)

    if macro is not None:
        before, after = macro
        print("Converting macros", before, "->", after)
        single_header.replace(before, after)

    new_namespace = namespace + "::"
    single_header = re.sub(r"\bCLI::\b", new_namespace, single_header)

    if output is not None:
        with open(output, "w") as f:
            f.write(single_header)

        print("Created", output)
    else:
        print(single_header)


if __name__ == "__main__":
    parser = argparse.ArgumentParser(
        usage="Convert source to single header include. Can optionally add namespace and search-replace replacements (for macros).",
        formatter_class=argparse.ArgumentDefaultsHelpFormatter,
    )
    parser.add_argument("--output", default=None, help="Single header file output")
    parser.add_argument(
        "--main",
        default="CLI11.hpp.in",
        help="The main include file that defines the other files",
    )
    parser.add_argument("files", nargs="+", help="The header files")
    parser.add_argument("--namespace", default="CLI", help="Set the namespace")
    parser.add_argument("--tag", default="CLI11", help="Tag to look up")
    parser.add_argument(
        "--macro", nargs=2, help="Replaces OLD_PREFIX_ with NEW_PREFIX_"
    )
    parser.add_argument("--version", help="Include this version in the generated file")
    args = parser.parse_args()

    make_header(
        args.output,
        args.main,
        args.files,
        args.tag,
        args.namespace,
        args.macro,
        args.version,
    )