-
Notifications
You must be signed in to change notification settings - Fork 0
/
formic.py
executable file
·142 lines (114 loc) · 3.73 KB
/
formic.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
import os
import glob
import xml.dom
import collections
from xml.dom.minidom import parse, parseString, Node
from xml.dom import pulldom
currentFile = None
currentTarget = None
targets = []
properties = []
## tags is a dictonary the structure of which is a key=tagName value=list of (tuple { filename, parent target, attributes}
tags = dict()
def resetCurrent():
global currentFile
global currentTarget
currentFile = None
currentTarget = None
## scan for all potential build files (eg. build.xml, deploy.xml, vis_build.xml, test.xml)
def allAntProjectFiles() :
antProjectFiles = []
for root, dirs, files in os.walk('.'):
for filename in glob.glob(root + '/build.xml'):
if len(filename) < 40 and filename.find('test.xml') == -1:
try :
dom = parse(filename)
project = dom.getElementsByTagName("project")[0]
if(project is not None):
antProjectFiles.append(filename)
except:
continue
return antProjectFiles
def processAntFile(filename):
global currentFile
handle = open(filename)
doc = pulldom.parse(handle)
resetCurrent()
currentFile = filename
for event, node in doc:
if event == pulldom.START_ELEMENT:
printNode(node)
processNode(node)
def printNode(node):
if node.nodeType == Node.ELEMENT_NODE:
if node.tagName != 'property':
print(node.tagName + ' ' + printAttributes(node))
def printAttributes(node):
attributes = '';
for i in range(node.attributes.length):
attributes = attributes + printAttribute(node.attributes.item(i)) + ' '
return attributes
def printAttribute(attr):
if attr.name != 'None':
return attr.name + "='" + attr.value +"'"
def processNode(node):
global currentTarget
if node.nodeType == Node.ELEMENT_NODE:
props = ''
attrMap = processAttributes(node)
if node.tagName == 'target' :
targets.append(attrMap['name'])
currentTarget = attrMap['name']
elif node.tagName == 'property':
if 'name' in attrMap:
props = props + ' ' + "name='"+ attrMap.pop('name') +"'"
for key in attrMap.keys():
props = props + ' ' + key+"='"+ attrMap[key] +"'"
properties.append(props)
else:
for key in attrMap.keys():
props = props + ' ' + key+"='"+ attrMap[key] +"'"
if node.tagName not in tags:
tags[node.tagName] = []
tags[node.tagName].append([currentFile, currentTarget, props])
def processAttributes(node):
attrMap = dict()
for i in range(node.attributes.length):
processAttribute(attrMap, node.attributes.item(i))
return attrMap
def processAttribute(map, attr):
if attr.name != 'None':
map[attr.name] = attr.value
def count(p):
cnt = dict()
for word in p:
try:
cnt[word]
cnt[word] += 1
except:
cnt[word] = 1
return cnt
def printDictionarySortedByValue(aDict):
for key, value in sorted(aDict.items(), key=lambda t: t[1], reverse=True):
print("%s: %s" % (value, key))
def printTagsSortedByBuildFile(aDict):
for key, value in sorted(aDict.items(), key=lambda t: t[0]):
print("\n%s:\n" % (key))
for i in value:
fileName, target, attr = i
print("\t%s %s %s" % (fileName, target, attr))
for projectFiles in allAntProjectFiles():
print('---------- ' + projectFiles + ' ----------------------------------------------')
processAntFile(projectFiles)
print("\n---------------targets ")
printDictionarySortedByValue(count(targets))
print("\n---------------properties ")
printDictionarySortedByValue(count(properties))
printTagsSortedByBuildFile(tags)
## scan for all potential build files (eg. build.xml, deploy.xml, vis_build.xml, test.xml)
## for each file
## parse it
## count each target name
## count each task used
## count each property defined
## count each