test
CMS 3D CMS Logo

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