-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinject-config.py
55 lines (47 loc) · 1.39 KB
/
inject-config.py
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
"""
This script parses values from config.json for use in the build environment as value macros.
The order of values in the source json file is expected to be maintained.
"""
import json
import re
import sys
from collections import OrderedDict
# Python 2/3 compatibility
STR_TYPE = bytes if (sys.version_info > (3, 0)) else unicode
def parseConfig(d, prefix=''):
if prefix != '':
prefix += '_'
result = []
for key in d:
t = type(d[key])
if t in (dict, OrderedDict):
result.extend(parseConfig(d[key], key))
continue
elif t in (str, STR_TYPE):
if d[key] == "":
result.append(str(prefix+key))
continue
elif d[key][0] == '`' and d[key][:1] == '`':
val = re.escape(d[key][1:-1])
else:
val = "\\\"%s\\\"" % d[key]
elif t is bool:
val = str(d[key]).lower()
else:
val = d[key]
result.append((str(prefix+key), val))
return result
cfg = []
with open('config.json') as f:
cfg.extend(parseConfig(json.load(f, object_pairs_hook=OrderedDict)))
if cfg:
output = []
for item in cfg:
t = type(item)
if t is tuple:
output.append("-D%s=%s" % item)
elif t is str:
output.append("-D%s" % item)
else:
pass
print(' '.join(output))