CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_8_patch3/src/Utilities/ReleaseScripts/scripts/cmsCodeRulesChecker.py

Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 
00003 __author__="Aurelija"
00004 __date__ ="$2010-07-14 16.48.55$"
00005 
00006 from os.path import join, isdir
00007 import os
00008 import re
00009 import sys
00010 from Utilities.ReleaseScripts.cmsCodeRules.keyFinder import finds
00011 from Utilities.ReleaseScripts.cmsCodeRules.filesFinder import getFilePathsFromWalk
00012 from Utilities.ReleaseScripts.cmsCodeRules.pickleFileCreater import createPickleFile
00013 from Utilities.ReleaseScripts.cmsCodeRules.config import Configuration, rulesNames, rulesDescription, helpMsg, checkPath, picklePath, txtPath, exceptPaths
00014 from Utilities.ReleaseScripts.cmsCodeRules.pathToRegEx import pathToRegEx
00015 from Utilities.ReleaseScripts.cmsCodeRules.showPage import run
00016 
00017 configuration = Configuration
00018 RULES = rulesNames
00019 checkPath = checkPath
00020 picklePath = picklePath
00021 txtPath = txtPath
00022 
00023 def splitPaths(listRule, pathHead):
00024     try:
00025         for i in range(len(listRule)):
00026             path, linesNumbers = listRule[i]
00027             listRule[i] = (path.replace(pathHead, '', 1), linesNumbers)
00028     except TypeError:
00029         print "Error: given wrong type of parameter in function splitPaths."
00030     return listRule
00031 
00032 def runRules(ruleNumberList, directory):
00033 
00034     result = []
00035     osWalk = []
00036     
00037     for rule in ruleNumberList:
00038         if str(rule) not in RULES:
00039             print 'Error: wrong rule parameter. There is no such rule: "'+rule+'"\n\n' + rulesDescription
00040             print '\nWrite -h for help'
00041             return -1
00042 
00043     for rule in configuration.keys():
00044         configuration[rule]['filter'] = re.compile(configuration[rule]['filter'])
00045 
00046     osWalk.extend(os.walk(directory))
00047 
00048     exceptPathsForEachRule = []
00049     for path in exceptPaths:
00050         exceptPathsForEachRule.append(join(checkPath, path))
00051 
00052     for ruleNr in ruleNumberList:
00053         files = []
00054         rule = str(ruleNr)
00055         rule = configuration[ruleNr]
00056 
00057         filesToMatch = rule['filesToMatch']
00058         exceptRuleLines = []
00059         exceptRulePaths = []
00060         for path in rule['exceptPaths']:
00061             try:
00062                 file, line = path.split(":",1)
00063                 xline = line
00064                 xdata = None
00065                 try:
00066                     xline, xdata = line.split(":",1)
00067                     if not xline: xline = '.*'
00068                 except ValueError:
00069                     pass
00070                 exceptRuleLines.append((pathToRegEx(file), xline,xdata))
00071             except ValueError:
00072                 exceptRulePaths.append(pathToRegEx(path))
00073         for fileType in filesToMatch:
00074             fileList = getFilePathsFromWalk(osWalk, fileType, exceptPathsForEachRule)
00075 # ------------------------------------------------------------------------------
00076             for path in exceptRulePaths:
00077                 xFileList = []
00078                 for file in fileList:
00079                     xFile = file.replace(join(checkPath, ""), "")
00080                     if not re.match(path, xFile):
00081                         xFileList.append(file)
00082                 fileList = xFileList
00083 # ------------------------------------------------------------------------------
00084             filesLinesList = fileList
00085             if rule['skip']:
00086                 for skipper in rule['skip']:
00087                     filesLinesList = skipper(filesLinesList)
00088             else:
00089                 for i,xFile in enumerate(filesLinesList):
00090                     filesLinesList[i]=((xFile,open(xFile).readlines()))
00091 # ------------------------------------------------------------------------------
00092             for Nr, fileLine in enumerate(exceptRuleLines):
00093                 regEx, line, lineEx = fileLine
00094                 for index, file in enumerate(fileList):
00095                     xFile = file.replace(join(checkPath, ""), "")
00096                     if re.match(regEx, xFile):
00097                         filesLinesList[index] = (filesLinesList[index][0], omitLine(filesLinesList[index][1], line,lineEx))
00098             files.extend(filesLinesList)
00099 # ------------------------------------------------------------------------------
00100         listRule = finds(files, rule['filter'], rule['exceptFilter'])
00101         result.append((ruleNr, splitPaths(listRule, checkPath)))
00102     return result
00103 
00104 def omitLine(lines, line, regEx=None):
00105     fileLines = lines
00106     if re.match('^\d+$',line):
00107         xline = int(line)-1
00108         try:
00109             if (not regEx) or (re.search(regEx,fileLines[xline].strip())):
00110                 fileLines[xline] = ''
00111         except IndexError:
00112             pass
00113     else:
00114         for i,lineData in enumerate(fileLines):
00115             if re.match(line,str(i)):
00116                 lineData = lineData.strip()
00117                 if (not regEx) or (re.search(regEx,lineData)):
00118                     fileLines[i] = ''
00119     return fileLines
00120 
00121 def printOut(listOfResult, filePath = None):
00122     file = None
00123     try:
00124         for rule, result in listOfResult:
00125             if filePath:
00126                 file = open('%s/cmsCodeRule%s.txt'%(filePath, rule), 'w')
00127                 for path, lineNumbers in result:
00128                     file.write('%s\n'%path)
00129                     file.write('%s\n'%str(lineNumbers))
00130                 file.close()
00131             else:
00132                 if not result or result == -1:
00133                     print 'No results for rule %s'%rule
00134                 else:
00135                     print 'Rule %s:' %rule
00136                     for path, lineNumbers in result:
00137                         print path
00138                         print lineNumbers
00139     except TypeError:
00140         print "Error: wrong type of parameter in function printOut"
00141 
00142 if __name__ == "__main__":
00143 
00144     cwd = os.getcwd()
00145     
00146     createPickle = False
00147     createTxt = False
00148     html = False
00149     help = False
00150     rules = False
00151     
00152     goodParameters = True
00153     argvLen = len(sys.argv)
00154     printResult = False
00155     ruleNumbers = RULES
00156 
00157     i = 1
00158     while (i != argvLen):
00159         arg = sys.argv[i]
00160 
00161         if   (arg == '-h'):
00162             help = True
00163         elif (arg == '-r'):
00164             rules = True
00165             i+=1
00166             if i < argvLen:
00167                 ruleNumbers = sys.argv[i].split(',')
00168             else:
00169                 goodParameters = False
00170                 print 'Error: missing rule parameters. Write -h for help'
00171                 break
00172         elif (arg == '-d'):
00173             i+=1
00174             if i < argvLen:
00175                 checkPath = sys.argv[i]
00176                 if not isdir(checkPath):
00177                     goodParameters = False
00178                     print 'Error: wrong directory "%s"' %checkPath
00179                     break
00180             else:
00181                 goodParameters = False
00182                 print 'Error: missing rule parameters. Write -h for help'
00183                 break
00184         elif (arg == '-S'):
00185             createPickle = True
00186             if i+1 < argvLen and sys.argv[i+1][0] != '-':
00187                 i+=1
00188                 picklePath = sys.argv[i]
00189                 if not isdir(picklePath):
00190                     goodParameters = False
00191                     print 'Error: wrong directory "%s"' %picklePath
00192                     break
00193         elif (arg == '-s'):
00194             createTxt = True
00195             if i+1 < argvLen and sys.argv[i+1][0] != '-':
00196                 i+=1
00197                 txtPath = sys.argv[i]
00198                 if not isdir(txtPath):
00199                     goodParameters = False
00200                     print 'Error: wrong directory "%s"' %txtPath
00201                     break
00202         elif (arg == '-html'):
00203             html = True
00204             createPickle = True
00205         elif (arg == '-p'):
00206             printResult = True
00207         else:
00208             goodParameters = False
00209             print 'Error: there is no parameter like "%s". Write -h for help' %arg
00210             break
00211         i+=1
00212 
00213     if goodParameters == True:
00214 
00215         if argvLen == 2 and help == True:
00216             print helpMsg
00217         else:
00218             result = runRules(ruleNumbers, checkPath)
00219                     
00220             if result != -1:
00221                 if argvLen == 1 or printResult or (createPickle == False and createTxt == False):
00222                     printOut(result)
00223                 else:
00224                     if createPickle:
00225                         for rule, ruleResult in result:
00226                             createPickleFile('cmsCodeRule%s.dat'%rule, ruleResult, picklePath)
00227                     if createTxt:
00228                         printOut(result, txtPath)
00229             if html:
00230                 run(picklePath, picklePath, picklePath)
00231             if help:
00232                 print helpMsg