forked from cms-sw/cmssw
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmsCodeRulesChecker.py
executable file
·233 lines (210 loc) · 8.23 KB
/
cmsCodeRulesChecker.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
#!/usr/bin/env python3
from builtins import range
__author__="Aurelija"
__date__ ="$2010-07-14 16.48.55$"
from os.path import join, isdir
import os
import re
import sys
from Utilities.ReleaseScripts.cmsCodeRules.keyFinder import finds
from Utilities.ReleaseScripts.cmsCodeRules.filesFinder import getFilePathsFromWalk
from Utilities.ReleaseScripts.cmsCodeRules.pickleFileCreater import createPickleFile
from Utilities.ReleaseScripts.cmsCodeRules.config import Configuration, rulesNames, rulesDescription, helpMsg, checkPath, picklePath, txtPath, exceptPaths
from Utilities.ReleaseScripts.cmsCodeRules.pathToRegEx import pathToRegEx
from Utilities.ReleaseScripts.cmsCodeRules.showPage import run
configuration = Configuration
RULES = rulesNames
checkPath = checkPath
picklePath = picklePath
txtPath = txtPath
def splitPaths(listRule, pathHead):
try:
for i in range(len(listRule)):
path, linesNumbers = listRule[i]
listRule[i] = (path.replace(pathHead, '', 1), linesNumbers)
except TypeError:
print("Error: given wrong type of parameter in function splitPaths.")
return listRule
def runRules(ruleNumberList, directory):
result = []
osWalk = []
for rule in ruleNumberList:
if str(rule) not in RULES:
print('Error: wrong rule parameter. There is no such rule: "'+rule+'"\n\n' + rulesDescription)
print('\nWrite -h for help')
return -1
for rule in configuration.keys():
configuration[rule]['filter'] = re.compile(configuration[rule]['filter'])
osWalk.extend(os.walk(directory))
exceptPathsForEachRule = []
for path in exceptPaths:
exceptPathsForEachRule.append(join(checkPath, path))
for ruleNr in ruleNumberList:
files = []
rule = str(ruleNr)
rule = configuration[ruleNr]
filesToMatch = rule['filesToMatch']
exceptRuleLines = []
exceptRulePaths = []
for path in rule['exceptPaths']:
try:
file, line = path.split(":",1)
xline = line
xdata = None
try:
xline, xdata = line.split(":",1)
if not xline: xline = '.*'
except ValueError:
pass
exceptRuleLines.append((pathToRegEx(file), xline,xdata))
except ValueError:
exceptRulePaths.append(pathToRegEx(path))
for fileType in filesToMatch:
fileList = getFilePathsFromWalk(osWalk, fileType, exceptPathsForEachRule)
# ------------------------------------------------------------------------------
for path in exceptRulePaths:
xFileList = []
for file in fileList:
xFile = file.replace(join(checkPath, ""), "")
if not re.match(path, xFile):
xFileList.append(file)
fileList = xFileList
# ------------------------------------------------------------------------------
filesLinesList = fileList
if rule['skip']:
for skipper in rule['skip']:
filesLinesList = skipper(filesLinesList)
else:
for i,xFile in enumerate(filesLinesList):
filesLinesList[i]=((xFile,open(xFile, errors='replace').readlines()))
# ------------------------------------------------------------------------------
for Nr, fileLine in enumerate(exceptRuleLines):
regEx, line, lineEx = fileLine
for index, file in enumerate(fileList):
xFile = file.replace(join(checkPath, ""), "")
if re.match(regEx, xFile):
filesLinesList[index] = (filesLinesList[index][0], omitLine(filesLinesList[index][1], line,lineEx))
files.extend(filesLinesList)
# ------------------------------------------------------------------------------
listRule = finds(files, rule['filter'], rule['exceptFilter'])
result.append((ruleNr, splitPaths(listRule, checkPath)))
return result
def omitLine(lines, line, regEx=None):
fileLines = lines
if re.match('^\d+$',line):
xline = int(line)-1
try:
if (not regEx) or (re.search(regEx,fileLines[xline].strip())):
fileLines[xline] = ''
except IndexError:
pass
else:
for i,lineData in enumerate(fileLines):
if re.match(line,str(i)):
lineData = lineData.strip()
if (not regEx) or (re.search(regEx,lineData)):
fileLines[i] = ''
return fileLines
def printOut(listOfResult, filePath = None):
file = None
try:
for rule, result in listOfResult:
if filePath:
file = open('%s/cmsCodeRule%s.txt'%(filePath, rule), 'w')
for path, lineNumbers in result:
file.write('%s\n'%path)
file.write('%s\n'%str(lineNumbers))
file.close()
else:
if not result or result == -1:
print('No results for rule %s'%rule)
else:
print('Rule %s:' %rule)
for path, lineNumbers in result:
print(path)
print(lineNumbers)
except TypeError:
print("Error: wrong type of parameter in function printOut")
if __name__ == "__main__":
cwd = os.getcwd()
createPickle = False
createTxt = False
html = False
help = False
rules = False
goodParameters = True
argvLen = len(sys.argv)
printResult = False
ruleNumbers = RULES
i = 1
while (i != argvLen):
arg = sys.argv[i]
if (arg == '-h'):
help = True
elif (arg == '-r'):
rules = True
i+=1
if i < argvLen:
ruleNumbers = sys.argv[i].split(',')
else:
goodParameters = False
print('Error: missing rule parameters. Write -h for help')
break
elif (arg == '-d'):
i+=1
if i < argvLen:
checkPath = sys.argv[i]
if not isdir(checkPath):
goodParameters = False
print('Error: wrong directory "%s"' %checkPath)
break
else:
goodParameters = False
print('Error: missing rule parameters. Write -h for help')
break
elif (arg == '-S'):
createPickle = True
if i+1 < argvLen and sys.argv[i+1][0] != '-':
i+=1
picklePath = sys.argv[i]
if not isdir(picklePath):
goodParameters = False
print('Error: wrong directory "%s"' %picklePath)
break
elif (arg == '-s'):
createTxt = True
if i+1 < argvLen and sys.argv[i+1][0] != '-':
i+=1
txtPath = sys.argv[i]
if not isdir(txtPath):
goodParameters = False
print('Error: wrong directory "%s"' %txtPath)
break
elif (arg == '-html'):
html = True
createPickle = True
elif (arg == '-p'):
printResult = True
else:
goodParameters = False
print('Error: there is no parameter like "%s". Write -h for help' %arg)
break
i+=1
if goodParameters == True:
if argvLen == 2 and help == True:
print(helpMsg)
else:
result = runRules(ruleNumbers, checkPath)
if result != -1:
if argvLen == 1 or printResult or (createPickle == False and createTxt == False):
printOut(result)
else:
if createPickle:
for rule, ruleResult in result:
createPickleFile('cmsCodeRule%s.dat'%rule, ruleResult, picklePath)
if createTxt:
printOut(result, txtPath)
if html:
run(picklePath, picklePath, picklePath)
if help:
print(helpMsg)