3 from __future__
import print_function
5 __date__ =
"$2010-07-14 16.48.55$" 7 from os.path
import join, isdir
11 from Utilities.ReleaseScripts.cmsCodeRules.keyFinder
import finds
12 from Utilities.ReleaseScripts.cmsCodeRules.filesFinder
import getFilePathsFromWalk
13 from Utilities.ReleaseScripts.cmsCodeRules.pickleFileCreater
import createPickleFile
14 from Utilities.ReleaseScripts.cmsCodeRules.config
import Configuration, rulesNames, rulesDescription, helpMsg, checkPath, picklePath, txtPath, exceptPaths
15 from Utilities.ReleaseScripts.cmsCodeRules.pathToRegEx
import pathToRegEx
16 from Utilities.ReleaseScripts.cmsCodeRules.showPage
import run
18 configuration = Configuration
21 picklePath = picklePath
26 for i
in range(len(listRule)):
27 path, linesNumbers = listRule[i]
28 listRule[i] = (path.replace(pathHead,
'', 1), linesNumbers)
30 print(
"Error: given wrong type of parameter in function splitPaths.")
38 for rule
in ruleNumberList:
39 if str(rule)
not in RULES:
40 print(
'Error: wrong rule parameter. There is no such rule: "'+rule+
'"\n\n' + rulesDescription)
41 print(
'\nWrite -h for help')
44 for rule
in configuration.keys():
45 configuration[rule][
'filter'] = re.compile(configuration[rule][
'filter'])
47 osWalk.extend(os.walk(directory))
49 exceptPathsForEachRule = []
50 for path
in exceptPaths:
51 exceptPathsForEachRule.append(
join(checkPath, path))
53 for ruleNr
in ruleNumberList:
56 rule = configuration[ruleNr]
58 filesToMatch = rule[
'filesToMatch']
61 for path
in rule[
'exceptPaths']:
63 file, line = path.split(
":",1)
67 xline, xdata = line.split(
":",1)
68 if not xline: xline =
'.*' 71 exceptRuleLines.append((
pathToRegEx(file), xline,xdata))
74 for fileType
in filesToMatch:
77 for path
in exceptRulePaths:
80 xFile = file.replace(
join(checkPath,
""),
"")
81 if not re.match(path, xFile):
82 xFileList.append(file)
85 filesLinesList = fileList
87 for skipper
in rule[
'skip']:
88 filesLinesList = skipper(filesLinesList)
90 for i,xFile
in enumerate(filesLinesList):
91 filesLinesList[i]=((xFile,open(xFile).readlines()))
93 for Nr, fileLine
in enumerate(exceptRuleLines):
94 regEx, line, lineEx = fileLine
95 for index, file
in enumerate(fileList):
96 xFile = file.replace(
join(checkPath,
""),
"")
97 if re.match(regEx, xFile):
98 filesLinesList[index] = (filesLinesList[index][0],
omitLine(filesLinesList[index][1], line,lineEx))
99 files.extend(filesLinesList)
101 listRule =
finds(files, rule[
'filter'], rule[
'exceptFilter'])
102 result.append((ruleNr,
splitPaths(listRule, checkPath)))
107 if re.match(
'^\d+$',line):
110 if (
not regEx)
or (re.search(regEx,fileLines[xline].
strip())):
111 fileLines[xline] =
'' 115 for i,lineData
in enumerate(fileLines):
116 if re.match(line,
str(i)):
117 lineData = lineData.strip()
118 if (
not regEx)
or (re.search(regEx,lineData)):
125 for rule, result
in listOfResult:
127 file = open(
'%s/cmsCodeRule%s.txt'%(filePath, rule),
'w')
128 for path, lineNumbers
in result:
129 file.write(
'%s\n'%path)
130 file.write(
'%s\n'%
str(lineNumbers))
133 if not result
or result == -1:
134 print(
'No results for rule %s'%rule)
136 print(
'Rule %s:' %rule)
137 for path, lineNumbers
in result:
141 print(
"Error: wrong type of parameter in function printOut")
143 if __name__ ==
"__main__":
153 goodParameters =
True 154 argvLen = len(sys.argv)
159 while (i != argvLen):
168 ruleNumbers = sys.argv[i].
split(
',')
170 goodParameters =
False 171 print(
'Error: missing rule parameters. Write -h for help')
176 checkPath = sys.argv[i]
177 if not isdir(checkPath):
178 goodParameters =
False 179 print(
'Error: wrong directory "%s"' %checkPath)
182 goodParameters =
False 183 print(
'Error: missing rule parameters. Write -h for help')
187 if i+1 < argvLen
and sys.argv[i+1][0] !=
'-':
189 picklePath = sys.argv[i]
190 if not isdir(picklePath):
191 goodParameters =
False 192 print(
'Error: wrong directory "%s"' %picklePath)
196 if i+1 < argvLen
and sys.argv[i+1][0] !=
'-':
198 txtPath = sys.argv[i]
199 if not isdir(txtPath):
200 goodParameters =
False 201 print(
'Error: wrong directory "%s"' %txtPath)
203 elif (arg ==
'-html'):
209 goodParameters =
False 210 print(
'Error: there is no parameter like "%s". Write -h for help' %arg)
214 if goodParameters ==
True:
216 if argvLen == 2
and help ==
True:
222 if argvLen == 1
or printResult
or (createPickle ==
False and createTxt ==
False):
226 for rule, ruleResult
in result:
231 run(picklePath, picklePath, picklePath)
def splitPaths(listRule, pathHead)
S & print(S &os, JobReport::InputFile const &f)
def printOut(listOfResult, filePath=None)
def createPickleFile(fileName, listRule, path=os.getcwd())
def omitLine(lines, line, regEx=None)
def getFilePathsFromWalk(osWalkResult, file, exceptPaths=[])
static std::string join(char **cmd)
def finds(fileList, regularExpression, exceptRegEx=[])
def runRules(ruleNumberList, directory)