-
Notifications
You must be signed in to change notification settings - Fork 0
/
find_variables.py
320 lines (256 loc) · 9.05 KB
/
find_variables.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
import os, re, json, argparse, itertools, subprocess
class Source:
def __init__(self, path, filename, line):
self.path = path
self.filename = filename
self.line = line
def __str__(self):
return f"{self.full_path}:{self.line}"
@property
def full_path(self):
return os.path.join(self.path, self.filename)
def as_dict(self):
return dict(path=self.path, filename=self.filename, line=self.line)
class Variable:
def __init__(self, name):
self.name = name
self.sources = []
def __str__(self):
return f"{self.name}: {self.sources}"
def add_source(self, path, filename, line):
self.sources.append(Source(path, filename, line))
def as_dict(self):
return dict(
name=self.name, sources=[source.as_dict() for source in self.sources]
)
def has_ext(name, *args):
return any(map(name.endswith, args))
class PathSpec:
def __init__(self, directories=None, fn=None, cpp_match_fn=lambda x: []):
# define argument dependencies
self.cpp_match_fn = cpp_match_fn
if directories and isinstance(directories, list):
self.directories = directories
else:
raise ValueError("A directories argument must be provided")
if fn:
self.fn = fn
else:
raise ValueError("A fn argument must be provided")
# setup our structures
self.files = []
self._constants = [] # we convert this to a dict when accessed publicly
self._variables = []
self._index = {}
self.needs_review = []
# initialization
self._collect_files()
self._recurse_and_find_constants()
self._recurse_and_find_variables()
self._make_index()
def __iter__(self):
return iter(self._variables)
# Private
# recursively collects a (dirpath, file,) tup for each file
# encountered in each directory
def _collect_files(self):
walk = itertools.chain(*[os.walk(directory) for directory in self.directories])
for dirpath, _, files in walk:
self.files += [
(
dirpath,
file,
)
for file in files
]
def _recurse_and_find_constants(self):
for dirpath, file in self.files:
if has_ext(file, ".c", ".h", ".cpp", ".hpp"):
self._constants += self._find_constants(dirpath, file)
def _recurse_and_find_variables(self):
for dirpath, file in self.files:
if has_ext(file, ".c", ".cpp"):
self._variables += self._find_variables(dirpath, file)
def _find_constants(self, path, filename):
file = os.path.join(path, filename)
with open(file, "r", errors="ignore") as f:
return self.cpp_match_fn(
(
f,
filename,
)
)
def _find_variables(self, path, filename):
file = os.path.join(path, filename)
with open(file, "r", errors="ignore") as f:
return self.fn(
(
f,
path,
filename,
)
)
def _maybe_replace_with_constant(self, variable):
if variable in self.constants.keys():
variable = self.constants[variable]
return variable
def _make_index(self):
for path, filename, line, variable in self._variables:
variable = self._maybe_replace_with_constant(variable)
if '"' in variable:
variable = variable.replace('"', "")
else:
self.needs_review += [
(
path,
filename,
line,
variable,
)
]
continue
self._add_to_index(path, filename, line, variable)
def _add_to_index(self, path, filename, line, variable):
if variable not in self._index.keys():
self._index[variable] = Variable(variable)
self._index[variable].add_source(path, filename, line)
# Public
@property
def index(self):
return self._index
@property
def constants(self):
return dict(self._constants)
@property
def variables(self):
return self._variables
@property
def variables_with_replacements(self):
"Prints out information about how variables were expanded."
for _path, _filename, _line, variable in self._variables:
if '"' not in variable:
try:
const = self.constants[variable]
print(f"\n{variable} -> {const}")
except:
print(f"\nwarning: {variable} has no expansion\n")
else:
print(variable)
def match_variable_line(expression, base):
def matcher(tup):
f, path, filename = tup
found = []
for i, line in enumerate(f):
match = re.search(expression, line)
if match is not None:
variable = match.group("variable")
variable = variable.split(")")[0]
variable = variable.split()[0]
src_path = path.replace(base, "")[1:]
found.append(
(
src_path,
filename,
i + 1,
variable,
)
)
return found
return matcher
def match_preprocessor_define(expression):
def matcher(tup):
f, filename = tup
found = []
for line in f:
match = re.search(expression, line)
if match is not None:
const = match.group("const")
val = match.group("val")
found.append(
(
const,
val,
)
)
return found
return matcher
var_expr = r"(switch_channel_var_(true|false)|switch_channel_(get|set)_variable\w*)(\(\w+[\-*\>*\w*]*\,\s*(?P<variable>\"*\w+\"*))"
cpp_expr = r"#define (?P<const>.+) (?P<val>.+)"
def get_version(p):
process = subprocess.run(
["git", "describe", "--tags"], cwd=p, text=True, stdout=subprocess.PIPE
)
return process.stdout.strip()
def needs_review_report(needs_review):
for path, filename, line, variable in needs_review:
p = os.path.join(path, filename)
print(f"{p}:{line} {variable}")
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="""
Example:
find_variables.py --base /path/to/freeswitch/project
"""
)
parser.add_argument(
"--base",
action="store",
help="The base path to the FreeSWITCH project.",
required=True,
)
parser.add_argument(
"--dir",
action="extend",
nargs="+",
type=str,
help="Specify one or more directories relative to the base path to traverse. Defaults to the 'src' directory if both --dir and --exclude-src are not used.",
default=["src"],
)
parser.add_argument(
"--exclude-src",
action="store_true",
help="Pass this switch if you want to exclude the src directory from traversal.",
)
parser.add_argument(
"--out",
action="store",
default="variables.json",
help="The file in which to save the output. Defaults to 'variables.json'.",
)
parser.add_argument(
"--needs-review",
action="store_true",
help="Prints a report of variables that need manual review.",
)
parser.add_argument(
"--show-replacements",
action="store_true",
help="Prints a report with how "
)
args = parser.parse_args()
base = args.base
dirs = args.dir
if args.exclude_src:
dirs = filter(lambda d: d != "src", dirs)
pathspec = PathSpec(
directories=[os.path.join(base, p) for p in dirs],
fn=match_variable_line(var_expr, base),
cpp_match_fn=match_preprocessor_define(cpp_expr),
)
index = [pathspec.index[key] for key in sorted(pathspec.index, key=str.lower)]
if args.show_replacements:
pathspec.variables_with_replacements()
if args.needs_review:
needs_review_report(pathspec.needs_review)
if pathspec.needs_review:
count = len(pathspec.needs_review)
word = "variable" if count == 1 else "variables"
print(f'{count} {word} must be manually checked. Rerun with --needs-review to check.')
count = len(pathspec.index)
word = "variable" if count == 1 else "variables"
outp = os.path.abspath(args.out)
print(f'{count} {word} processed and output to {outp}')
current_version = get_version(base)
vs = dict(variables=[var.as_dict() for var in index], version=current_version)
with open(args.out, "w") as f:
f.write(json.dumps(vs))