summaryrefslogtreecommitdiff
path: root/utilities/clean.py
blob: d68f87c73df3b5fa9884588e5836e86e7ad2a338 (plain)
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
#!/usr/bin/python 

# 201005251608
# simon kirkby
# tigger@interthingy.com
# GPL2+

# Openscad cleaner

import tokenize,os,string

def tokenize_scad(scad_file):
	f = open(scad_file)
	tok = tokenize.generate_tokens(f.readline)
	tokens = []
	for i in tok:
		tokens.append(i)
	f.close
	return tokens


call = {}
scalar = {}
modules = {}
variables = {}
stack = []
def process(tokens,file_name):
	print file_name
	get_mod_name = 0 
	current_call = ''
	for i in t:
#		print i
		sl = len(stack)
		#for j in range(sl):
		#	print '-',
		#print i[1],
		if i[0] == 1:
		#	print 'call ' + str(i[1])
			if get_mod_name:
				print '\t'+i[1]
				get_mod_name = 0
				if modules.has_key(i[1]):
					modules[i[1]].append(file_name + ' line ' + str(i[2][0]))
				else:
					modules[i[1]] = [file_name + ' line ' + str(i[2][0])] 

			if i[1] == 'module':
				#print 'module ->'+i[1]
				get_mod_name = 1
				
			if call.has_key(i[1]):
				call[i[1]] = call[i[1]] + 1
			else:
				call[i[1]] = 1
			current_call = i[1]
		if i[0] == 2:
		#	print 'scalar ' + str(i[1])
			if scalar.has_key(i[1]):
				scalar[i[1]] = scalar[i[1]] + 1
			else:
				scalar[i[1]] = 1
		if i[0] == 51:
		#	if i[1] == '=':
		#		print 'assignment'
			if i[1] == '(':
		#		print 'stack add'
				stack.append(i)
			if i[1] == ')':
				tok = stack.pop()
		#		print 'pop' 
			if i[1] == '=':
				if variables.has_key(current_call):
					variables[current_call].append(file_name + ' line ' + str(i[2][0]))
				else:
					variables[current_call] = [file_name + ' line ' + str(i[2][0])] 
				

sha_path = '../part_sha.txt'
sha_dict =  {}
try:
        f = open(sha_path)
        li = f.readlines()
        f.close()
        for i in li:
                tmp = string.split(string.strip(i),',')
                sha_dict[tmp[0]] = tmp[1]
except:
        print 'no existing sums'

print 'Tokenizing scad files'
print 
print 'Module List'

k = sha_dict.keys()
for i in k:
	if i[-4:] == 'scad':
		t = tokenize_scad('../'+i)
		process(t,i)
	if i[-3:] == 'inc':
		t = tokenize_scad('../'+i)
		process(t,i)
#print call
#print 'Scalar ::\n\n'
#print scalar
print 
print 'Modules ::'
print 
k = modules.keys()
k.sort()
for i in k:
	if len(modules[i]) > 1:
		print 'duplicate module def ' + i 
		for j in modules[i]:
			print '\t' + j
#	else:
#		print 'single module def ' + i 
#		print '\t' + modules[i][0]

# get rid of internal varlables
print 
print 'Variables ::'
print 
del variables['r']
del variables['h']
del variables['v']
del variables['center']
del variables['fn']

v = variables.keys()
v.sort()
for i in v:
	if len(variables[i]) > 1:
		print 'duplicate variable def ' + i 
		for j in variables[i]:
			print '\t' + j

print 
print " Variable instance count ::"
print 
for i in v:
	if call.has_key(i):
		print i + ' : ' + str(call[i])
	else:
		print i + ' unreferenced'

#print scalar
print '\nend\n'