CMS 3D CMS Logo

cmsCodeRulesChecker.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 from __future__ import print_function
4 from builtins import range
5 __author__="Aurelija"
6 __date__ ="$2010-07-14 16.48.55$"
7 
8 from os.path import join, isdir
9 import os
10 import re
11 import sys
12 from Utilities.ReleaseScripts.cmsCodeRules.keyFinder import finds
13 from Utilities.ReleaseScripts.cmsCodeRules.filesFinder import getFilePathsFromWalk
14 from Utilities.ReleaseScripts.cmsCodeRules.pickleFileCreater import createPickleFile
15 from Utilities.ReleaseScripts.cmsCodeRules.config import Configuration, rulesNames, rulesDescription, helpMsg, checkPath, picklePath, txtPath, exceptPaths
16 from Utilities.ReleaseScripts.cmsCodeRules.pathToRegEx import pathToRegEx
17 from Utilities.ReleaseScripts.cmsCodeRules.showPage import run
18 
19 configuration = Configuration
20 RULES = rulesNames
21 checkPath = checkPath
22 picklePath = picklePath
23 txtPath = txtPath
24 
25 def splitPaths(listRule, pathHead):
26  try:
27  for i in range(len(listRule)):
28  path, linesNumbers = listRule[i]
29  listRule[i] = (path.replace(pathHead, '', 1), linesNumbers)
30  except TypeError:
31  print("Error: given wrong type of parameter in function splitPaths.")
32  return listRule
33 
34 def runRules(ruleNumberList, directory):
35 
36  result = []
37  osWalk = []
38 
39  for rule in ruleNumberList:
40  if str(rule) not in RULES:
41  print('Error: wrong rule parameter. There is no such rule: "'+rule+'"\n\n' + rulesDescription)
42  print('\nWrite -h for help')
43  return -1
44 
45  for rule in configuration.keys():
46  configuration[rule]['filter'] = re.compile(configuration[rule]['filter'])
47 
48  osWalk.extend(os.walk(directory))
49 
50  exceptPathsForEachRule = []
51  for path in exceptPaths:
52  exceptPathsForEachRule.append(join(checkPath, path))
53 
54  for ruleNr in ruleNumberList:
55  files = []
56  rule = str(ruleNr)
57  rule = configuration[ruleNr]
58 
59  filesToMatch = rule['filesToMatch']
60  exceptRuleLines = []
61  exceptRulePaths = []
62  for path in rule['exceptPaths']:
63  try:
64  file, line = path.split(":",1)
65  xline = line
66  xdata = None
67  try:
68  xline, xdata = line.split(":",1)
69  if not xline: xline = '.*'
70  except ValueError:
71  pass
72  exceptRuleLines.append((pathToRegEx(file), xline,xdata))
73  except ValueError:
74  exceptRulePaths.append(pathToRegEx(path))
75  for fileType in filesToMatch:
76  fileList = getFilePathsFromWalk(osWalk, fileType, exceptPathsForEachRule)
77 # ------------------------------------------------------------------------------
78  for path in exceptRulePaths:
79  xFileList = []
80  for file in fileList:
81  xFile = file.replace(join(checkPath, ""), "")
82  if not re.match(path, xFile):
83  xFileList.append(file)
84  fileList = xFileList
85 # ------------------------------------------------------------------------------
86  filesLinesList = fileList
87  if rule['skip']:
88  for skipper in rule['skip']:
89  filesLinesList = skipper(filesLinesList)
90  else:
91  for i,xFile in enumerate(filesLinesList):
92  filesLinesList[i]=((xFile,open(xFile).readlines()))
93 # ------------------------------------------------------------------------------
94  for Nr, fileLine in enumerate(exceptRuleLines):
95  regEx, line, lineEx = fileLine
96  for index, file in enumerate(fileList):
97  xFile = file.replace(join(checkPath, ""), "")
98  if re.match(regEx, xFile):
99  filesLinesList[index] = (filesLinesList[index][0], omitLine(filesLinesList[index][1], line,lineEx))
100  files.extend(filesLinesList)
101 # ------------------------------------------------------------------------------
102  listRule = finds(files, rule['filter'], rule['exceptFilter'])
103  result.append((ruleNr, splitPaths(listRule, checkPath)))
104  return result
105 
106 def omitLine(lines, line, regEx=None):
107  fileLines = lines
108  if re.match('^\d+$',line):
109  xline = int(line)-1
110  try:
111  if (not regEx) or (re.search(regEx,fileLines[xline].strip())):
112  fileLines[xline] = ''
113  except IndexError:
114  pass
115  else:
116  for i,lineData in enumerate(fileLines):
117  if re.match(line,str(i)):
118  lineData = lineData.strip()
119  if (not regEx) or (re.search(regEx,lineData)):
120  fileLines[i] = ''
121  return fileLines
122 
123 def printOut(listOfResult, filePath = None):
124  file = None
125  try:
126  for rule, result in listOfResult:
127  if filePath:
128  file = open('%s/cmsCodeRule%s.txt'%(filePath, rule), 'w')
129  for path, lineNumbers in result:
130  file.write('%s\n'%path)
131  file.write('%s\n'%str(lineNumbers))
132  file.close()
133  else:
134  if not result or result == -1:
135  print('No results for rule %s'%rule)
136  else:
137  print('Rule %s:' %rule)
138  for path, lineNumbers in result:
139  print(path)
140  print(lineNumbers)
141  except TypeError:
142  print("Error: wrong type of parameter in function printOut")
143 
144 if __name__ == "__main__":
145 
146  cwd = os.getcwd()
147 
148  createPickle = False
149  createTxt = False
150  html = False
151  help = False
152  rules = False
153 
154  goodParameters = True
155  argvLen = len(sys.argv)
156  printResult = False
157  ruleNumbers = RULES
158 
159  i = 1
160  while (i != argvLen):
161  arg = sys.argv[i]
162 
163  if (arg == '-h'):
164  help = True
165  elif (arg == '-r'):
166  rules = True
167  i+=1
168  if i < argvLen:
169  ruleNumbers = sys.argv[i].split(',')
170  else:
171  goodParameters = False
172  print('Error: missing rule parameters. Write -h for help')
173  break
174  elif (arg == '-d'):
175  i+=1
176  if i < argvLen:
177  checkPath = sys.argv[i]
178  if not isdir(checkPath):
179  goodParameters = False
180  print('Error: wrong directory "%s"' %checkPath)
181  break
182  else:
183  goodParameters = False
184  print('Error: missing rule parameters. Write -h for help')
185  break
186  elif (arg == '-S'):
187  createPickle = True
188  if i+1 < argvLen and sys.argv[i+1][0] != '-':
189  i+=1
190  picklePath = sys.argv[i]
191  if not isdir(picklePath):
192  goodParameters = False
193  print('Error: wrong directory "%s"' %picklePath)
194  break
195  elif (arg == '-s'):
196  createTxt = True
197  if i+1 < argvLen and sys.argv[i+1][0] != '-':
198  i+=1
199  txtPath = sys.argv[i]
200  if not isdir(txtPath):
201  goodParameters = False
202  print('Error: wrong directory "%s"' %txtPath)
203  break
204  elif (arg == '-html'):
205  html = True
206  createPickle = True
207  elif (arg == '-p'):
208  printResult = True
209  else:
210  goodParameters = False
211  print('Error: there is no parameter like "%s". Write -h for help' %arg)
212  break
213  i+=1
214 
215  if goodParameters == True:
216 
217  if argvLen == 2 and help == True:
218  print(helpMsg)
219  else:
220  result = runRules(ruleNumbers, checkPath)
221 
222  if result != -1:
223  if argvLen == 1 or printResult or (createPickle == False and createTxt == False):
224  printOut(result)
225  else:
226  if createPickle:
227  for rule, ruleResult in result:
228  createPickleFile('cmsCodeRule%s.dat'%rule, ruleResult, picklePath)
229  if createTxt:
230  printOut(result, txtPath)
231  if html:
232  run(picklePath, picklePath, picklePath)
233  if help:
234  print(helpMsg)
pickleFileCreater.createPickleFile
def createPickleFile(fileName, listRule, path=os.getcwd())
Definition: pickleFileCreater.py:9
FastTimerService_cff.range
range
Definition: FastTimerService_cff.py:34
cmsCodeRulesChecker.runRules
def runRules(ruleNumberList, directory)
Definition: cmsCodeRulesChecker.py:34
cmsCodeRulesChecker.splitPaths
def splitPaths(listRule, pathHead)
Definition: cmsCodeRulesChecker.py:25
digitizers_cfi.strip
strip
Definition: digitizers_cfi.py:19
filesFinder.getFilePathsFromWalk
def getFilePathsFromWalk(osWalkResult, file, exceptPaths=[])
Definition: filesFinder.py:8
join
static std::string join(char **cmd)
Definition: RemoteFile.cc:17
cms::dd::split
std::vector< std::string_view > split(std::string_view, const char *)
cmsCodeRulesChecker.omitLine
def omitLine(lines, line, regEx=None)
Definition: cmsCodeRulesChecker.py:106
str
#define str(s)
Definition: TestProcessor.cc:48
cmsCodeRulesChecker.printOut
def printOut(listOfResult, filePath=None)
Definition: cmsCodeRulesChecker.py:123
createfilelist.int
int
Definition: createfilelist.py:10
edm::print
S & print(S &os, JobReport::InputFile const &f)
Definition: JobReport.cc:66
keyFinder.finds
def finds(fileList, regularExpression, exceptRegEx=[])
Definition: keyFinder.py:8
writedatasetfile.run
run
Definition: writedatasetfile.py:27
pathToRegEx
Definition: pathToRegEx.py:1