-
Notifications
You must be signed in to change notification settings - Fork 12
/
config.py
177 lines (153 loc) · 6.49 KB
/
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
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
# MIT License
#
# Copyright (c) 2017 Gaetan Guidet
#
# This file is part of excons.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import os
import re
import excons
import SCons.Script # pylint: disable=import-error
# pylint: disable=bad-indentation
def GetPath(name):
return excons.joinpath(excons.out_dir, "%s.status" % name)
def HasChanged(name, opts):
if not os.path.isfile(GetPath(name)):
return True
else:
with open(GetPath(name), "r") as f:
for line in f.readlines():
spl = line.strip().split(" ")
if spl[0] in opts:
if str(opts[spl[0]]) != " ".join(spl[1:]):
return True
return False
def Write(name, opts):
with open(GetPath(name), "w") as f:
for k, v in opts.iteritems():
f.write("%s %s\n" % (k, v))
f.write("\n")
def GenerateFile(outpath, inpath, opts, pattern=None, optgroup=None, converters=None, replacefuncs=None):
# Converters must convert opts value to strings
# When no defined, str() is used
if pattern is not None:
if optgroup is None:
raise Exception("Please specify 'optgroup' when using a custom pattern")
phexp = re.compile(pattern)
qualifiergrp = None
else:
# @VAR_NAME@
# @VAR_NAME.defined@
# @VAR_NAME.undefined@
# @VAR_NAME.equal(otherval)@
# @VAR_NAME.not_equal(otherval)@
# @VAR_NAME.match(expr)@
# @VAR_NAME.not_match(expr)@
# @VAR_NAME.greater(numeric)@
# @VAR_NAME.greater_or_equal(numeric)@
# @VAR_NAME.lesser(numeric)@
# @VAR_NAME.lesser_or_equal(numeric)@
phexp = re.compile(r"@([^@.]+)(?:\.([^@.]+))?@")
qlexp = re.compile(r"defined|undefined|(?:(equal|greater|lesser|greater_or_equal|lesser_or_equal|match|not_equal|not_match)\(([^)]+)\))")
optgroup = 1
qualifiergrp = 2
if converters is None:
converters = {}
# value -> string
def _convertvalue(v):
vt = type(v)
if vt in converters:
return converters[vt](v)
elif not isinstance(v, basestring):
return str(v)
else:
return v
with open(outpath, "wb") as outf:
with open(inpath, "rb") as inf:
for line in inf.readlines():
if replacefuncs is not None:
for replacefunc in replacefuncs:
line = replacefunc(line, opts)
remain = line[:]
outline = ""
m = phexp.search(remain)
while m is not None:
outline += remain[:m.start()]
matched = m.group()
remain = remain[m.end():]
key = m.group(optgroup)
if not key in opts:
val = None
else:
val = opts[key]
if qualifiergrp is not None and m.group(qualifiergrp):
qm = qlexp.match(m.group(qualifiergrp))
if qm is None:
excons.WarnOnce("Invalid qualifier for '%s': %s" % (key, m.group(qualifiergrp)))
else:
qualifier = qm.group(0)
if qualifier == "defined":
val = (val is not None)
elif qualifier == "undefined":
val = (val is None)
else:
qualifier = qm.group(1)
qval = qm.group(2)
if qualifier == "equal":
val = (_convertvalue(val) == qval)
elif qualifier == "not_equal":
val = (_convertvalue(val) != qval)
elif qualifier == "greater":
val = (float(val) > float(qval))
elif qualifier == "greater_or_equal":
val = (float(val) >= float(qval))
elif qualifier == "lesser":
val = (float(val) < float(qval))
elif qualifier == "lesser_or_equal":
val = (float(val) <= float(qval))
elif qualifier == "match":
val = (re.match(qval, val) is not None)
elif qualifier == "not_match":
val = (re.match(qval, val) is None)
else:
excons.WarnOnce("Unexpected qualifier for %s: %s" % (key, qualifier))
val = None
if val is None:
excons.WarnOnce("No value for placeholder '%s' in %s" % (key, inpath))
else:
matched = matched.replace(m.group(0), _convertvalue(val))
outline += matched
m = phexp.search(remain)
outline += remain
outf.write(outline)
def AddGenerator(env, name, opts, pattern=None, optgroup=None, converters=None, replacefuncs=None):
if converters is None:
converters = {}
def _ActionFunc(target, source, env): # pylint: disable=unused-argument
GenerateFile(str(target[0]), str(source[0]), opts, pattern=pattern, optgroup=optgroup, converters=converters, replacefuncs=replacefuncs)
return None
funcname = "%sGenerateFile" % name
env["BUILDERS"][funcname] = SCons.Script.Builder(action=SCons.Script.Action(_ActionFunc, "Generating $TARGET ..."))
func = getattr(env, funcname)
def _WrapFunc(target, source):
if HasChanged(name, opts):
Write(name, opts)
return func(target, [source] + [GetPath(name)])
return _WrapFunc