CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_1_8_patch12/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(":")
00063                 exceptRuleLines.append((pathToRegEx(file), line))
00064             except ValueError:
00065                 exceptRulePaths.append(pathToRegEx(path))
00066         for fileType in filesToMatch:
00067             fileList = getFilePathsFromWalk(osWalk, fileType, exceptPathsForEachRule)
00068 # ------------------------------------------------------------------------------
00069             for path in exceptRulePaths:
00070                 FileList = []
00071                 for file in fileList:
00072                     File = file.replace(join(checkPath, ""), "")
00073                     if not re.match(path, File):
00074                         FileList.append(file)
00075                 fileList = FileList
00076 # ------------------------------------------------------------------------------
00077             filesLinesList = []
00078             skipLines = fileList
00079             for skipper in rule['skip']:
00080                 filesLinesList = skipper(skipLines)
00081                 skipLines = filesLinesList
00082             if not exceptRuleLines and not rule['skip']:
00083                 filesLinesList = fileList
00084 # ------------------------------------------------------------------------------
00085             for Nr, fileLine in enumerate(exceptRuleLines):
00086                 regEx, line = fileLine
00087                 for index, file in enumerate(fileList):
00088                     File = file.replace(join(checkPath, ""), "")
00089                     if re.match(regEx, File):
00090                         if rule['skip'] != None or Nr > 0:
00091                             filesLinesList[index] = (filesLinesList[index][0], omitLine(filesLinesList[index][1], line))
00092                         else:
00093                             filesLinesList.append([file, omitLine(file, line)])
00094                     elif rule['skip'] == None:
00095                         filesLinesList.append((file, open(file).readlines()))
00096             files.extend(filesLinesList)
00097 # ------------------------------------------------------------------------------
00098         listRule = finds(files, rule['filter'], rule['exceptFilter'])
00099         result.append((ruleNr, splitPaths(listRule, checkPath)))
00100     return result
00101 
00102 def omitLine(file, line):
00103     try:
00104         if type(file).__name__ != 'list':
00105             fileLines = open(file).readlines()
00106         else: fileLines = file
00107         fileLines[int(line)-1] = ''
00108     except IndexError:
00109         print 'File = "' + file +'" has only ' + str(len(fileLines)) + ' lines. Wrong given line number: ' + str(line)
00110     return fileLines
00111 
00112 def printOut(listOfResult, filePath = None):
00113     file = None
00114     try:
00115         for rule, result in listOfResult:
00116             if filePath:
00117                 file = open('%s/cmsCodeRule%s.txt'%(filePath, rule), 'w')
00118                 for path, lineNumbers in result:
00119                     file.write('%s\n'%path)
00120                     file.write('%s\n'%str(lineNumbers))
00121                 file.close()
00122             else:
00123                 if not result or result == -1:
00124                     print 'No results for rule %s'%rule
00125                 else:
00126                     print 'Rule %s:' %rule
00127                     for path, lineNumbers in result:
00128                         print path
00129                         print lineNumbers
00130     except TypeError:
00131         print "Error: wrong type of parameter in function printOut"
00132 
00133 if __name__ == "__main__":
00134 
00135     cwd = os.getcwd()
00136     
00137     createPickle = False
00138     createTxt = False
00139     html = False
00140     help = False
00141     rules = False
00142     
00143     goodParameters = True
00144     argvLen = len(sys.argv)
00145     printResult = False
00146     ruleNumbers = RULES
00147 
00148     i = 1
00149     while (i != argvLen):
00150         arg = sys.argv[i]
00151 
00152         if   (arg == '-h'):
00153             help = True
00154         elif (arg == '-r'):
00155             rules = True
00156             i+=1
00157             if i < argvLen:
00158                 ruleNumbers = sys.argv[i].split(',')
00159             else:
00160                 goodParameters = False
00161                 print 'Error: missing rule parameters. Write -h for help'
00162                 break
00163         elif (arg == '-d'):
00164             i+=1
00165             if i < argvLen:
00166                 checkPath = sys.argv[i]
00167                 if not isdir(checkPath):
00168                     goodParameters = False
00169                     print 'Error: wrong directory "%s"' %checkPath
00170                     break
00171             else:
00172                 goodParameters = False
00173                 print 'Error: missing rule parameters. Write -h for help'
00174                 break
00175         elif (arg == '-S'):
00176             createPickle = True
00177             if i+1 < argvLen and sys.argv[i+1][0] != '-':
00178                 i+=1
00179                 picklePath = sys.argv[i]
00180                 if not isdir(picklePath):
00181                     goodParameters = False
00182                     print 'Error: wrong directory "%s"' %picklePath
00183                     break
00184         elif (arg == '-s'):
00185             createTxt = True
00186             if i+1 < argvLen and sys.argv[i+1][0] != '-':
00187                 i+=1
00188                 txtPath = sys.argv[i]
00189                 if not isdir(txtPath):
00190                     goodParameters = False
00191                     print 'Error: wrong directory "%s"' %txtPath
00192                     break
00193         elif (arg == '-html'):
00194             html = True
00195             createPickle = True
00196         elif (arg == '-p'):
00197             printResult = True
00198         else:
00199             goodParameters = False
00200             print 'Error: there is no parameter like "%s". Write -h for help' %arg
00201             break
00202         i+=1
00203 
00204     if goodParameters == True:
00205 
00206         if argvLen == 2 and help == True:
00207             print helpMsg
00208         else:
00209             result = runRules(ruleNumbers, checkPath)
00210                     
00211             if result != -1:
00212                 if argvLen == 1 or printResult or (createPickle == False and createTxt == False):
00213                     printOut(result)
00214                 else:
00215                     if createPickle:
00216                         for rule, ruleResult in result:
00217                             createPickleFile('cmsCodeRule%s.dat'%rule, ruleResult, picklePath)
00218                     if createTxt:
00219                         printOut(result, txtPath)
00220             if html:
00221                 run(picklePath, picklePath, picklePath)
00222             if help:
00223                 print helpMsg