-
Notifications
You must be signed in to change notification settings - Fork 0
/
compile
executable file
·53 lines (44 loc) · 1.19 KB
/
compile
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
#! python3
from re import compile as regcomp
from keyword import kwlist
from sys import argv
invars = {}
outvars = {}
with open('scriptconfig') as fh:
for line in fh:
key, sep, val = line.rstrip().partition('=')
invars[key] = val
is_wellformed = regcomp(r'(?:[^\\\$\`]|\$(?:\{\w+}|\w+)|\\.)+').fullmatch
expand_vars = regcomp(r'\\(.)|\$(?:\{(\w+)\}|(\w+))').sub
escape_quotes = regcomp(r'[\\"]').sub
def expansion(match):
subexp = match.group(1)
if subexp is not None:
return subexp
subexp = match.group(2)
if subexp is not None:
return outvars.get(subexp, '')
subexp = match.group(3)
if subexp is not None:
return outvars.get(subexp, '')
return ''
changed = True
while changed:
changed = False
for key, val in invars.items():
if is_wellformed(val):
val = expand_vars(expansion, val)
if outvars.get(key) != val:
outvars[key] = val
changed = True
print(f"#! {outvars['PYTHON3']} -O\n\nclass autoconf:")
for key in sorted(outvars):
val = outvars[key]
escaped_val = escape_quotes(r'\\\g<0>', val)
while key in kwlist:
key = 'ac_' + key
print(f"\t{key} = \"{escaped_val}\"")
for filename in argv[1:]:
print()
with open(filename) as fh:
print(fh.read(), end = '')