forked from Syndica/sig
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathremove_unused.py
87 lines (68 loc) · 2.48 KB
/
remove_unused.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
# parse arg of file name
import sys
import os
if len(sys.argv) != 2:
print("Usage: python remove_unused.py <dir name>")
sys.exit()
zig_files = []
dirs = [sys.argv[1]]
while 1:
d = dirs.pop()
files = os.listdir(d)
for file in files:
full_path = os.path.join(d, file)
if os.path.isdir(full_path):
dirs.append(full_path)
else:
# if file ends in .zig
if file.endswith('.zig'):
zig_files.append(full_path)
if len(dirs) == 0:
break
total_removes = 0
n_remove_iter = 0
n_removes = 1
while n_removes > 0:
n_removes = 0
print(f"iteration: {n_remove_iter}, lines removed: {n_removes}")
n_remove_iter += 1
for filename in zig_files:
print(filename)
# open and read lines of file
with open(filename, 'r') as f:
full_lines = f.readlines()
# filter lines to start with 'const' or 'pub const'
lines = [line for line in full_lines if line.startswith('const') or line.startswith('pub const')]
# get lines which include '@import'
lines = [line for line in lines if '@import' in line]
# parse the value {VAR} name in 'const {VAR} = @import ...'
import_var_names = []
for (i, line) in enumerate(full_lines):
if not (line.startswith('const') or line.startswith('pub const')):
continue
if '@import' not in line:
continue
start_index = line.index("const ")
end_index = line.index(" = ")
var_name = line[start_index + 6:end_index]
import_var_names.append((var_name, i))
unused_vars = import_var_names.copy()
for i, line in enumerate(full_lines):
for var, line_num in import_var_names:
if (var in line) and (i != line_num):
if (var, line_num) in unused_vars:
unused_vars.remove((var, line_num))
new_lines = []
lines_to_remove = [i for (_, i) in unused_vars]
n_removes += len(lines_to_remove)
total_removes += len(lines_to_remove)
for (i, line) in enumerate(full_lines):
if i in lines_to_remove:
continue
new_lines.append(line)
print(unused_vars)
# write
with open(filename, 'w') as f:
f.writelines(new_lines)
print("total iterations: ", n_remove_iter)
print("total lines removed: ", total_removes)