Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:31:47

0001 #!/usr/bin/env python3
0002 
0003 from __future__ import print_function
0004 from builtins import range
0005 __author__="Aurelija"
0006 __date__ ="$2010-07-14 16.48.55$"
0007 
0008 from os.path import join, isdir
0009 import os
0010 import re
0011 import sys
0012 from Utilities.ReleaseScripts.cmsCodeRules.keyFinder import finds
0013 from Utilities.ReleaseScripts.cmsCodeRules.filesFinder import getFilePathsFromWalk
0014 from Utilities.ReleaseScripts.cmsCodeRules.pickleFileCreater import createPickleFile
0015 from Utilities.ReleaseScripts.cmsCodeRules.config import Configuration, rulesNames, rulesDescription, helpMsg, checkPath, picklePath, txtPath, exceptPaths
0016 from Utilities.ReleaseScripts.cmsCodeRules.pathToRegEx import pathToRegEx
0017 from Utilities.ReleaseScripts.cmsCodeRules.showPage import run
0018 
0019 configuration = Configuration
0020 RULES = rulesNames
0021 checkPath = checkPath
0022 picklePath = picklePath
0023 txtPath = txtPath
0024 
0025 def splitPaths(listRule, pathHead):
0026     try:
0027         for i in range(len(listRule)):
0028             path, linesNumbers = listRule[i]
0029             listRule[i] = (path.replace(pathHead, '', 1), linesNumbers)
0030     except TypeError:
0031         print("Error: given wrong type of parameter in function splitPaths.")
0032     return listRule
0033 
0034 def runRules(ruleNumberList, directory):
0035 
0036     result = []
0037     osWalk = []
0038     
0039     for rule in ruleNumberList:
0040         if str(rule) not in RULES:
0041             print('Error: wrong rule parameter. There is no such rule: "'+rule+'"\n\n' + rulesDescription)
0042             print('\nWrite -h for help')
0043             return -1
0044 
0045     for rule in configuration.keys():
0046         configuration[rule]['filter'] = re.compile(configuration[rule]['filter'])
0047 
0048     osWalk.extend(os.walk(directory))
0049 
0050     exceptPathsForEachRule = []
0051     for path in exceptPaths:
0052         exceptPathsForEachRule.append(join(checkPath, path))
0053 
0054     for ruleNr in ruleNumberList:
0055         files = []
0056         rule = str(ruleNr)
0057         rule = configuration[ruleNr]
0058 
0059         filesToMatch = rule['filesToMatch']
0060         exceptRuleLines = []
0061         exceptRulePaths = []
0062         for path in rule['exceptPaths']:
0063             try:
0064                 file, line = path.split(":",1)
0065                 xline = line
0066                 xdata = None
0067                 try:
0068                     xline, xdata = line.split(":",1)
0069                     if not xline: xline = '.*'
0070                 except ValueError:
0071                     pass
0072                 exceptRuleLines.append((pathToRegEx(file), xline,xdata))
0073             except ValueError:
0074                 exceptRulePaths.append(pathToRegEx(path))
0075         for fileType in filesToMatch:
0076             fileList = getFilePathsFromWalk(osWalk, fileType, exceptPathsForEachRule)
0077 # ------------------------------------------------------------------------------
0078             for path in exceptRulePaths:
0079                 xFileList = []
0080                 for file in fileList:
0081                     xFile = file.replace(join(checkPath, ""), "")
0082                     if not re.match(path, xFile):
0083                         xFileList.append(file)
0084                 fileList = xFileList
0085 # ------------------------------------------------------------------------------
0086             filesLinesList = fileList
0087             if rule['skip']:
0088                 for skipper in rule['skip']:
0089                     filesLinesList = skipper(filesLinesList)
0090             else:
0091                 for i,xFile in enumerate(filesLinesList):
0092                     filesLinesList[i]=((xFile,open(xFile, errors='replace').readlines()))
0093 # ------------------------------------------------------------------------------
0094             for Nr, fileLine in enumerate(exceptRuleLines):
0095                 regEx, line, lineEx = fileLine
0096                 for index, file in enumerate(fileList):
0097                     xFile = file.replace(join(checkPath, ""), "")
0098                     if re.match(regEx, xFile):
0099                         filesLinesList[index] = (filesLinesList[index][0], omitLine(filesLinesList[index][1], line,lineEx))
0100             files.extend(filesLinesList)
0101 # ------------------------------------------------------------------------------
0102         listRule = finds(files, rule['filter'], rule['exceptFilter'])
0103         result.append((ruleNr, splitPaths(listRule, checkPath)))
0104     return result
0105 
0106 def omitLine(lines, line, regEx=None):
0107     fileLines = lines
0108     if re.match('^\d+$',line):
0109         xline = int(line)-1
0110         try:
0111             if (not regEx) or (re.search(regEx,fileLines[xline].strip())):
0112                 fileLines[xline] = ''
0113         except IndexError:
0114             pass
0115     else:
0116         for i,lineData in enumerate(fileLines):
0117             if re.match(line,str(i)):
0118                 lineData = lineData.strip()
0119                 if (not regEx) or (re.search(regEx,lineData)):
0120                     fileLines[i] = ''
0121     return fileLines
0122 
0123 def printOut(listOfResult, filePath = None):
0124     file = None
0125     try:
0126         for rule, result in listOfResult:
0127             if filePath:
0128                 file = open('%s/cmsCodeRule%s.txt'%(filePath, rule), 'w')
0129                 for path, lineNumbers in result:
0130                     file.write('%s\n'%path)
0131                     file.write('%s\n'%str(lineNumbers))
0132                 file.close()
0133             else:
0134                 if not result or result == -1:
0135                     print('No results for rule %s'%rule)
0136                 else:
0137                     print('Rule %s:' %rule)
0138                     for path, lineNumbers in result:
0139                         print(path)
0140                         print(lineNumbers)
0141     except TypeError:
0142         print("Error: wrong type of parameter in function printOut")
0143 
0144 if __name__ == "__main__":
0145 
0146     cwd = os.getcwd()
0147     
0148     createPickle = False
0149     createTxt = False
0150     html = False
0151     help = False
0152     rules = False
0153     
0154     goodParameters = True
0155     argvLen = len(sys.argv)
0156     printResult = False
0157     ruleNumbers = RULES
0158 
0159     i = 1
0160     while (i != argvLen):
0161         arg = sys.argv[i]
0162 
0163         if   (arg == '-h'):
0164             help = True
0165         elif (arg == '-r'):
0166             rules = True
0167             i+=1
0168             if i < argvLen:
0169                 ruleNumbers = sys.argv[i].split(',')
0170             else:
0171                 goodParameters = False
0172                 print('Error: missing rule parameters. Write -h for help')
0173                 break
0174         elif (arg == '-d'):
0175             i+=1
0176             if i < argvLen:
0177                 checkPath = sys.argv[i]
0178                 if not isdir(checkPath):
0179                     goodParameters = False
0180                     print('Error: wrong directory "%s"' %checkPath)
0181                     break
0182             else:
0183                 goodParameters = False
0184                 print('Error: missing rule parameters. Write -h for help')
0185                 break
0186         elif (arg == '-S'):
0187             createPickle = True
0188             if i+1 < argvLen and sys.argv[i+1][0] != '-':
0189                 i+=1
0190                 picklePath = sys.argv[i]
0191                 if not isdir(picklePath):
0192                     goodParameters = False
0193                     print('Error: wrong directory "%s"' %picklePath)
0194                     break
0195         elif (arg == '-s'):
0196             createTxt = True
0197             if i+1 < argvLen and sys.argv[i+1][0] != '-':
0198                 i+=1
0199                 txtPath = sys.argv[i]
0200                 if not isdir(txtPath):
0201                     goodParameters = False
0202                     print('Error: wrong directory "%s"' %txtPath)
0203                     break
0204         elif (arg == '-html'):
0205             html = True
0206             createPickle = True
0207         elif (arg == '-p'):
0208             printResult = True
0209         else:
0210             goodParameters = False
0211             print('Error: there is no parameter like "%s". Write -h for help' %arg)
0212             break
0213         i+=1
0214 
0215     if goodParameters == True:
0216 
0217         if argvLen == 2 and help == True:
0218             print(helpMsg)
0219         else:
0220             result = runRules(ruleNumbers, checkPath)
0221                     
0222             if result != -1:
0223                 if argvLen == 1 or printResult or (createPickle == False and createTxt == False):
0224                     printOut(result)
0225                 else:
0226                     if createPickle:
0227                         for rule, ruleResult in result:
0228                             createPickleFile('cmsCodeRule%s.dat'%rule, ruleResult, picklePath)
0229                     if createTxt:
0230                         printOut(result, txtPath)
0231             if html:
0232                 run(picklePath, picklePath, picklePath)
0233             if help:
0234                 print(helpMsg)