-
Notifications
You must be signed in to change notification settings - Fork 0
/
hwaf-spy-env.py
184 lines (167 loc) · 6.48 KB
/
hwaf-spy-env.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
178
179
180
181
182
183
184
# -*- python -*-
# stdlib imports
import os
import os.path as osp
import sys
# waf imports ---
import waflib.Options
import waflib.Utils
import waflib.Logs as msg
from waflib.Configure import conf
_heptooldir = osp.dirname(osp.abspath(__file__))
# add this directory to sys.path to ease the loading of other hepwaf tools
if not _heptooldir in sys.path: sys.path.append(_heptooldir)
### ---------------------------------------------------------------------------
# monkey patch waflib.ConfigSet.ConfigSet to log environment mods
@conf
def hwaf_setup_spy_env(ctx):
import traceback
import waflib.ConfigSet
_orig_ConfigSet_setitem = waflib.ConfigSet.ConfigSet.__setitem__
_orig_ConfigSet_setattr = waflib.ConfigSet.ConfigSet.__setattr__
_orig_ConfigSet_append_value = waflib.ConfigSet.ConfigSet.append_value
_orig_ConfigSet_prepend_value = waflib.ConfigSet.ConfigSet.prepend_value
_orig_ConfigSet_append_unique = waflib.ConfigSet.ConfigSet.append_unique
def _tb_stack_filter(stack):
'''filter function to only keep interesting stacks
(discarding noise from waf/hwaf internals)
'''
who = stack[-1]
fname = who[0]
if fname.endswith(('waflib/Configure.py',
'waflib/ConfigSet.py',
'share/hwaf/tools/hwaf-base.py',
'tools/hwaf-base.py',
'share/hwaf/tools/hwaf-spy-env.py',
'tools/hwaf-spy-env.py',
)):
return True
return False
#mylog = open('spy.log.txt', 'w')
def _new_ConfigSet_setitem(self, key, value):
if key == 'HWAF_ENV_SPY':
return _orig_ConfigSet_setitem(self, key, value)
stack = traceback.extract_stack()[:-1]
if _tb_stack_filter(stack):
return _orig_ConfigSet_setitem(self, key, value)
#print(">>> __setitem__(%s, %s)..." % (key, stack), file=mylog)
#mylog.flush()
old_value = self[key]
ret = _orig_ConfigSet_setitem(self, key, value)
new_value = self[key]
if old_value != new_value or 1:
_orig_ConfigSet_append_value(
self,
'HWAF_ENV_SPY',
[{'action': 'set',
'key': key,
'old': old_value,
'new': new_value,
'val': value,
'who': stack[-1],
}]
)
return ret
waflib.ConfigSet.ConfigSet.__setitem__ = _new_ConfigSet_setitem
def _new_ConfigSet_setattr(self, key, value):
if key == 'HWAF_ENV_SPY':
return _orig_ConfigSet_setattr(self, key, value)
stack = traceback.extract_stack()[:-1]
if _tb_stack_filter(stack):
return _orig_ConfigSet_setattr(self, key, value)
#print(">>> __setattr__(%s, %s)..." % (key, stack), file=mylog)
#mylog.flush()
old_value = self[key]
ret = _orig_ConfigSet_setattr(self, key, value)
new_value = self[key]
if old_value != new_value or 1:
_orig_ConfigSet_append_value(
self,
'HWAF_ENV_SPY',
[{'action': 'set',
'key': key,
'old': old_value,
'new': new_value,
'val': value,
'who': stack[-1],
}]
)
return ret
waflib.ConfigSet.ConfigSet.__setattr__ = _new_ConfigSet_setattr
def _new_ConfigSet_append_value(self, var, val):
if var == 'HWAF_ENV_SPY':
return _orig_ConfigSet_append_value(self, var, val)
stack = traceback.extract_stack()[:-1]
if _tb_stack_filter(stack):
return _orig_ConfigSet_append_value(self, var, val)
#print(">>> append_value(%s, %s)..." % (var, stack), file=mylog)
#mylog.flush()
old_value = self[var]
ret = _orig_ConfigSet_append_value(self, var, val)
new_value = self[var]
if old_value != new_value or 1:
_orig_ConfigSet_append_value(
self,
'HWAF_ENV_SPY',
[{'action': 'append_value',
'key': var,
'old': old_value,
'new': new_value,
'val': val,
'who': stack[-1],
}]
)
return ret
waflib.ConfigSet.ConfigSet.append_value = _new_ConfigSet_append_value
def _new_ConfigSet_prepend_value(self, var, val):
if var == 'HWAF_ENV_SPY':
return _orig_ConfigSet_prepend_value(self, var, val)
stack = traceback.extract_stack()[:-1]
if _tb_stack_filter(stack):
return _orig_ConfigSet_prepend_value(self, var, val)
#print(">>> prepend_value(%s, %s)..." % (var, stack), file=mylog)
#mylog.flush()
old_value = self[var]
ret = _orig_ConfigSet_prepend_value(self, var, val)
new_value = self[var]
if old_value != new_value or 1:
_orig_ConfigSet_append_value(
self,
'HWAF_ENV_SPY',
[{'action': 'prepend_value',
'key': var,
'old': old_value,
'new': new_value,
'val': val,
'who': stack[-1],
}]
)
return ret
waflib.ConfigSet.ConfigSet.prepend_value = _new_ConfigSet_prepend_value
def _new_ConfigSet_append_unique(self, var, val):
if var == 'HWAF_ENV_SPY':
return _orig_ConfigSet_append_unique(self, var, val)
stack = traceback.extract_stack()[:-1]
if _tb_stack_filter(stack):
return _orig_ConfigSet_append_unique(self, var, val)
#print(">>> append_unique(%s, %s)..." % (var, stack), file=mylog)
#mylog.flush()
old_value = self[var]
ret = _orig_ConfigSet_append_unique(self, var, val)
new_value = self[var]
if old_value != new_value or 1:
_orig_ConfigSet_append_value(
self,
'HWAF_ENV_SPY',
[{'action': 'append_unique',
'key': var,
'old': old_value,
'new': new_value,
'val': val,
'who': stack[-1],
}]
)
return ret
waflib.ConfigSet.ConfigSet.append_unique = _new_ConfigSet_append_unique
pass
## EOF ##