-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathargvars.py
61 lines (50 loc) · 2.45 KB
/
argvars.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
import re, os
import log, cfg
class ArgVars:
def __init__(self):
self.last_read = None
def read(self, ws, exp):
exp.set_ws(ws)
argvars_filename = exp.expand(cfg.argvars_path)
log.debug('reading argvars:' + argvars_filename)
try:
argvars_file = open(argvars_filename, 'r', encoding='utf-8')
except IOError:
log.debug('Failed to open argvars file: ' + argvars_filename)
return None
argvars = argvars_file.read()
m = re.search(cfg.argvars_version_re, argvars, re.M)
if not m:
log.debug('No match in argvars')
return None
self.last_read = m.group(2)
return self.last_read
def save(self, version, exp):
argvars_filename = exp.expand(cfg.argvars_path)
log.debug('saving argvars:' + argvars_filename)
if version == self.last_read:
log.debug('version not changed, skipping save')
return
if not os.path.isfile(argvars_filename):
log.debug('Argvars file does not exist, creating it')
with open(argvars_filename, 'w', encoding='utf-8') as argvars_file:
argvars_file.write(exp.expand(cfg.template_header))
argvars_file.write(exp.expand(cfg.template))
argvars_file.write(exp.expand(cfg.template_footer))
return
with open(argvars_filename, 'r', encoding='utf-8') as argvars_file:
argvars = argvars_file.read()
m = re.search(cfg.argvars_version_re, argvars, re.M)
if not m:
log.debug('Argvars file exists, but does not contain EW_VERSION, adding it')
with open(argvars_filename, 'w', encoding='utf-8') as argvars_file:
argvars = re.sub('<iarUserArgVars */>', '<iarUserArgVars>\n</iarUserArgVars>', argvars)
argvars_file.write(argvars.replace('<iarUserArgVars>', '<iarUserArgVars>\n' + exp.expand(cfg.template)))
return
replaced_argvars = re.sub(cfg.argvars_version_re, m.group(1) + version, argvars, flags=re.M)
if replaced_argvars == argvars:
log.debug('No change to argvars, skip writing')
return
log.debug('Argvars file exists, and contains EW_VERSION, replacing it')
with open(argvars_filename, 'w', encoding='utf-8') as argvars_file:
argvars_file.write(replaced_argvars)