aboutsummaryrefslogtreecommitdiff
path: root/scripts/xml-preprocess.py
blob: 57f1d2891257b6811276354c0506f14af5e9c64c (plain)
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
#!/usr/bin/env python3
#
# Copyright (c) 2017-2019 Tony Su
# Copyright (c) 2023 Red Hat, Inc.
#
# SPDX-License-Identifier: MIT
#
# Adapted from https://github.com/peitaosu/XML-Preprocessor
#
"""This is a XML Preprocessor which can be used to process your XML file before
you use it, to process conditional statements, variables, iteration
statements, error/warning, execute command, etc.

## XML Schema

### Include Files
```
<?include path/to/file ?>
```

### Variables
```
$(env.EnvironmentVariable)

$(sys.SystemVariable)

$(var.CustomVariable)
```

### Conditional Statements
```
<?if ?>

<?ifdef ?>

<?ifndef ?>

<?else?>

<?elseif ?>

<?endif?>
```

### Iteration Statements
```
<?foreach VARNAME in 1;2;3?>
    $(var.VARNAME)
<?endforeach?>
```

### Errors and Warnings
```
<?error "This is error message!" ?>

<?warning "This is warning message!" ?>
```

### Commands
```
<? cmd "echo hello world" ?>
```
"""

import os
import platform
import re
import subprocess
import sys
from typing import Optional
from xml.dom import minidom


class Preprocessor():
    """This class holds the XML preprocessing state"""

    def __init__(self):
        self.sys_vars = {
            "ARCH": platform.architecture()[0],
            "SOURCE": os.path.abspath(__file__),
            "CURRENT": os.getcwd(),
        }
        self.cus_vars = {}

    def _pp_include(self, xml_str: str) -> str:
        include_regex = r"(<\?include([\w\s\\/.:_-]+)\s*\?>)"
        matches = re.findall(include_regex, xml_str)
        for group_inc, group_xml in matches:
            inc_file_path = group_xml.strip()
            with open(inc_file_path, "r", encoding="utf-8") as inc_file:
                inc_file_content = inc_file.read()
                xml_str = xml_str.replace(group_inc, inc_file_content)
        return xml_str

    def _pp_env_var(self, xml_str: str) -> str:
        envvar_regex = r"(\$\(env\.(\w+)\))"
        matches = re.findall(envvar_regex, xml_str)
        for group_env, group_var in matches:
            xml_str = xml_str.replace(group_env, os.environ[group_var])
        return xml_str

    def _pp_sys_var(self, xml_str: str) -> str:
        sysvar_regex = r"(\$\(sys\.(\w+)\))"
        matches = re.findall(sysvar_regex, xml_str)
        for group_sys, group_var in matches:
            xml_str = xml_str.replace(group_sys, self.sys_vars[group_var])
        return xml_str

    def _pp_cus_var(self, xml_str: str) -> str:
        define_regex = r"(<\?define\s*(\w+)\s*=\s*([\w\s\"]+)\s*\?>)"
        matches = re.findall(define_regex, xml_str)
        for group_def, group_name, group_var in matches:
            group_name = group_name.strip()
            group_var = group_var.strip().strip("\"")
            self.cus_vars[group_name] = group_var
            xml_str = xml_str.replace(group_def, "")
        cusvar_regex = r"(\$\(var\.(\w+)\))"
        matches = re.findall(cusvar_regex, xml_str)
        for group_cus, group_var in matches:
            xml_str = xml_str.replace(
                group_cus,
                self.cus_vars.get(group_var, "")
            )
        return xml_str

    def _pp_foreach(self, xml_str: str) -> str:
        foreach_regex = r"(<\?foreach\s+(\w+)\s+in\s+([\w;]+)\s*\?>(.*)<\?endforeach\?>)"
        matches = re.findall(foreach_regex, xml_str)
        for group_for, group_name, group_vars, group_text in matches:
            group_texts = ""
            for var in group_vars.split(";"):
                self.cus_vars[group_name] = var
                group_texts += self._pp_cus_var(group_text)
            xml_str = xml_str.replace(group_for, group_texts)
        return xml_str

    def _pp_error_warning(self, xml_str: str) -> str:
        error_regex = r"<\?error\s*\"([^\"]+)\"\s*\?>"
        matches = re.findall(error_regex, xml_str)
        for group_var in matches:
            raise RuntimeError("[Error]: " + group_var)
        warning_regex = r"(<\?warning\s*\"([^\"]+)\"\s*\?>)"
        matches = re.findall(warning_regex, xml_str)
        for group_wrn, group_var in matches:
            print("[Warning]: " + group_var)
            xml_str = xml_str.replace(group_wrn, "")
        return xml_str

    def _pp_if_eval(self, xml_str: str) -> str:
        ifelif_regex = (
            r"(<\?(if|elseif)\s*([^\"\s=<>!]+)\s*([!=<>]+)\s*\"*([^\"=<>!]+)\"*\s*\?>)"
        )
        matches = re.findall(ifelif_regex, xml_str)
        for ifelif, tag, left, operator, right in matches:
            if "<" in operator or ">" in operator:
                result = eval(f"{left} {operator} {right}")
            else:
                result = eval(f'"{left}" {operator} "{right}"')
            xml_str = xml_str.replace(ifelif, f"<?{tag} {result}?>")
        return xml_str

    def _pp_ifdef_ifndef(self, xml_str: str) -> str:
        ifndef_regex = r"(<\?(ifdef|ifndef)\s*([\w]+)\s*\?>)"
        matches = re.findall(ifndef_regex, xml_str)
        for group_ifndef, group_tag, group_var in matches:
            if group_tag == "ifdef":
                result = group_var in self.cus_vars
            else:
                result = group_var not in self.cus_vars
            xml_str = xml_str.replace(group_ifndef, f"<?if {result}?>")
        return xml_str

    def _pp_if_elseif(self, xml_str: str) -> str:
        if_elif_else_regex = (
            r"(<\?if\s(True|False)\?>"
            r"(.*?)"
            r"<\?elseif\s(True|False)\?>"
            r"(.*?)"
            r"<\?else\?>"
            r"(.*?)"
            r"<\?endif\?>)"
        )
        if_else_regex = (
            r"(<\?if\s(True|False)\?>"
            r"(.*?)"
            r"<\?else\?>"
            r"(.*?)"
            r"<\?endif\?>)"
        )
        if_regex = r"(<\?if\s(True|False)\?>(.*?)<\?endif\?>)"
        matches = re.findall(if_elif_else_regex, xml_str, re.DOTALL)
        for (group_full, group_if, group_if_elif, group_elif,
             group_elif_else, group_else) in matches:
            result = ""
            if group_if == "True":
                result = group_if_elif
            elif group_elif == "True":
                result = group_elif_else
            else:
                result = group_else
            xml_str = xml_str.replace(group_full, result)
        matches = re.findall(if_else_regex, xml_str, re.DOTALL)
        for group_full, group_if, group_if_else, group_else in matches:
            result = ""
            if group_if == "True":
                result = group_if_else
            else:
                result = group_else
            xml_str = xml_str.replace(group_full, result)
        matches = re.findall(if_regex, xml_str, re.DOTALL)
        for group_full, group_if, group_text in matches:
            result = ""
            if group_if == "True":
                result = group_text
            xml_str = xml_str.replace(group_full, result)
        return xml_str

    def _pp_command(self, xml_str: str) -> str:
        cmd_regex = r"(<\?cmd\s*\"([^\"]+)\"\s*\?>)"
        matches = re.findall(cmd_regex, xml_str)
        for group_cmd, group_exec in matches:
            output = subprocess.check_output(
                group_exec, shell=True,
                text=True, stderr=subprocess.STDOUT
            )
            xml_str = xml_str.replace(group_cmd, output)
        return xml_str

    def _pp_blanks(self, xml_str: str) -> str:
        right_blank_regex = r">[\n\s\t\r]*"
        left_blank_regex = r"[\n\s\t\r]*<"
        xml_str = re.sub(right_blank_regex, ">", xml_str)
        xml_str = re.sub(left_blank_regex, "<", xml_str)
        return xml_str

    def preprocess(self, xml_str: str) -> str:
        fns = [
            self._pp_blanks,
            self._pp_include,
            self._pp_foreach,
            self._pp_env_var,
            self._pp_sys_var,
            self._pp_cus_var,
            self._pp_if_eval,
            self._pp_ifdef_ifndef,
            self._pp_if_elseif,
            self._pp_command,
            self._pp_error_warning,
        ]

        while True:
            changed = False
            for func in fns:
                out_xml = func(xml_str)
                if not changed and out_xml != xml_str:
                    changed = True
                xml_str = out_xml
            if not changed:
                break

        return xml_str


def preprocess_xml(path: str) -> str:
    with open(path, "r", encoding="utf-8") as original_file:
        input_xml = original_file.read()

        proc = Preprocessor()
        return proc.preprocess(input_xml)


def save_xml(xml_str: str, path: Optional[str]):
    xml = minidom.parseString(xml_str)
    with open(path, "w", encoding="utf-8") if path else sys.stdout as output_file:
        output_file.write(xml.toprettyxml())


def main():
    if len(sys.argv) < 2:
        print("Usage: xml-preprocessor input.xml [output.xml]")
        sys.exit(1)

    output_file = None
    if len(sys.argv) == 3:
        output_file = sys.argv[2]

    input_file = sys.argv[1]
    output_xml = preprocess_xml(input_file)
    save_xml(output_xml, output_file)


if __name__ == "__main__":
    main()