-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathsubst_file.py
38 lines (30 loc) · 844 Bytes
/
subst_file.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
#!/usr/bin/env python2
import os
import subprocess
import sys
import re
import json
if len(sys.argv) != 4:
print(sys.argv[0] + ': in out json_subs')
sys.exit(1)
with open(sys.argv[1]) as f:
contents = f.read()
out_file_name = sys.argv[2]
with open(sys.argv[3]) as f:
vars = json.load(f)
subs = vars['subs']
do_chmod = vars['do_chmod']
if isinstance(subs, dict):
subs = subs.items()
for k,v in subs:
# scons Substfile just defers to re.sub
contents = re.sub(k, str(v), contents)
# Don't write to the file if it isn't changing
if os.path.exists(out_file_name) and not do_chmod:
with open(out_file_name) as f:
if f.read() == contents:
sys.exit(0)
with open(out_file_name, 'w') as f:
f.write(contents)
if do_chmod:
subprocess.check_call(['chmod', 'oug+x', out_file_name])