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