forked from se-sic/cppstats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathascope.py
executable file
·206 lines (170 loc) · 5.64 KB
/
ascope.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
# modules from the std-library
import os
import re
import sys
from optparse import OptionParser
# external libs
# python-lxml module
try:
from lxml import etree
except ImportError:
print("python-lxml module not found! (python-lxml)")
print("see http://codespeak.net/lxml/")
print("programm terminating ...!")
sys.exit(-1)
def returnFileNames(folder, extfilt = ['.xml']):
'''This function returns all files of the input folder <folder>
and its subfolders.'''
filesfound = list()
if os.path.isdir(folder):
wqueue = [os.path.abspath(folder)]
while wqueue:
currentfolder = wqueue[0]
wqueue = wqueue[1:]
foldercontent = os.listdir(currentfolder)
tmpfiles = filter(lambda n: os.path.isfile(
os.path.join(currentfolder, n)), foldercontent)
tmpfiles = filter(lambda n: os.path.splitext(n)[1] in extfilt,
tmpfiles)
tmpfiles = map(lambda n: os.path.join(currentfolder, n),
tmpfiles)
filesfound += tmpfiles
tmpfolders = filter(lambda n: os.path.isdir(
os.path.join(currentfolder, n)), foldercontent)
tmpfolders = map(lambda n: os.path.join(currentfolder, n),
tmpfolders)
wqueue += tmpfolders
return filesfound
class Ascope:
##################################################
# constants:
__cppnscpp = 'http://www.sdml.info/srcML/cpp'
__cppnsdef = 'http://www.sdml.info/srcML/src'
__cpprens = re.compile('{(.+)}(.+)')
__conditionals = ['if', 'ifdef', 'ifndef', 'else', 'elif', 'endif']
__conditions = ['if', 'ifdef', 'ifndef']
__screensize = 50
__depthannotation = 60
##################################################
def __init__(self):
oparser = OptionParser()
oparser.add_option('-d', '--dir', dest='dir',
help='input directory (mandatory)')
(self.opts, self.args) = oparser.parse_args()
if not self.opts.dir:
oparser.print_help()
sys.exit(-1)
self.loc=0
self.checkFiles()
def __getIfdefAnnotations__(self, root):
'''This method returns all nodes of the xml which are ifdef
annotations in the source code.'''
treeifdefs = list()
for _, elem in etree.iterwalk(root):
ns, tag = Ascope.__cpprens.match(elem.tag).\
groups()
if ns == Ascope.__cppnscpp \
and tag in Ascope.__conditionals:
treeifdefs.append(elem)
return treeifdefs
def __createListFromTreeifdefs__(self, treeifdefs):
'''This method returns a list representation for the input treeifdefs
(xml-objects). Corresponding #ifdef elements are in one sublist.'''
try:
if not treeifdefs: return []
listifdefs = list()
workerlist = list()
for nifdef in treeifdefs:
tag = nifdef.tag.split('}')[1]
if tag in ['if', 'ifdef', 'ifndef']:
workerlist.append(list())
workerlist[-1].append(nifdef)
elif tag in ['elif', 'else']:
workerlist[-1].append(nifdef)
elif tag in ['endif']:
workerlist[-1].append(nifdef)
listifdefs.append(workerlist[-1])
workerlist = workerlist[:-1]
else:
print('ERROR: tag (%s) unknown!' % tag)
return listifdefs
except IndexError:
return []
def __getParentTag__(self, tag):
parent = tag.getparent()
return parent.tag.split('}')[1]
def __checkDiscipline__(self, treeifdefs, loc, stats, statsU):
listundisciplined = self.__createListFromTreeifdefs__(treeifdefs)
# print('INFO: %s annotations to check' % len(listundisciplined))
allannotations=[]
for ifdef in listundisciplined:
for i in range(len(ifdef)-1):
allannotations.append([ifdef[i].sourceline,ifdef[i+1].sourceline,self.__findFeatures__(ifdef,i)]);
for screen in range(0, max(1,min(65000,loc-Ascope.__screensize/2)), Ascope.__screensize/2):
screenend=min(loc, screen+Ascope.__screensize)
annotationsOnScreen=set()
annotationsOnScreenCount=0
for annotation in allannotations:
if annotation[0]<=screenend:
if annotation[1]>screen:
annotationsOnScreen.add(annotation[2])
annotationsOnScreenCount=annotationsOnScreenCount+1
try:
stats[annotationsOnScreenCount]=stats[annotationsOnScreenCount]+1
statsU[len(annotationsOnScreen)]=statsU[len(annotationsOnScreen)]+1
except IndexError:
print(annotationsOnScreenCount)
sys.exit(-1)
# print(stats)
# print(statsU)
def __findFeatures__(self, ifdef, idx):
result=""
if ifdef[idx].tag.split('}')[1]=='else':
idx=0
result="!"
if ifdef[idx].tag.split('}')[1]=='ifndef':
if (result=="!"):
result=""
else:
result="!"
context = etree.iterwalk(ifdef[idx])
for action, elem in context:
if action=="end":
if elem.tag.split('}')[1]=="name":
result=result+elem.text
# print result;
return result
def checkFile(self, file, stats, statsU):
# print('INFO: processing (%s)' % file)
try:
tree = etree.parse(file)
f = open(file, 'r')
except etree.XMLSyntaxError:
print('ERROR: file (%s) is not valid. Skipping it.' % file)
return
#get LOC
thisloc=len(f.readlines())-2
if (thisloc > 65000):
print('INFO: file (%s) not fully processed!' % file)
# get root of the xml and iterate over it
root = tree.getroot()
treeifdefs = self.__getIfdefAnnotations__(root)
self.__checkDiscipline__(treeifdefs, thisloc, stats, statsU)
def checkFiles(self):
xmlfiles = returnFileNames(self.opts.dir, ['.xml'])
stats=[0]*Ascope.__depthannotation
statsU=[0]*Ascope.__depthannotation
for xmlfile in xmlfiles:
self.checkFile(xmlfile, stats, statsU)
f = open("count.csv","a")
f.write(self.opts.dir+";"+str(Ascope.__screensize)+";")
for i in stats:
f.write(str(i)+";")
for i in statsU:
f.write(str(i)+";")
f.write("\n")
##################################################
if __name__ == '__main__':
Ascope()