Beispiel #1
0
def getFilePathsFromWalk(osWalkResult, file, exceptPaths=[]):

    listOfFiles = []

    file = pathToRegEx(file)

    for root, dirs, files in osWalkResult:
        for name in files:
            excepted = False
            fullPath = join(root, name)
            for path in pathsToRegEx(exceptPaths):
                if re.match(path, fullPath):
                    excepted = True
                    break
            if not excepted and re.match(file, name):
                listOfFiles.append(fullPath)
    return listOfFiles
Beispiel #2
0
def runRules(ruleNumberList, directory):

    result = []
    osWalk = []
    
    for rule in ruleNumberList:
        if str(rule) not in RULES:
            print('Error: wrong rule parameter. There is no such rule: "'+rule+'"\n\n' + rulesDescription)
            print('\nWrite -h for help')
            return -1

    for rule in configuration.keys():
        configuration[rule]['filter'] = re.compile(configuration[rule]['filter'])

    osWalk.extend(os.walk(directory))

    exceptPathsForEachRule = []
    for path in exceptPaths:
        exceptPathsForEachRule.append(join(checkPath, path))

    for ruleNr in ruleNumberList:
        files = []
        rule = str(ruleNr)
        rule = configuration[ruleNr]

        filesToMatch = rule['filesToMatch']
        exceptRuleLines = []
        exceptRulePaths = []
        for path in rule['exceptPaths']:
            try:
                file, line = path.split(":",1)
                xline = line
                xdata = None
                try:
                    xline, xdata = line.split(":",1)
                    if not xline: xline = '.*'
                except ValueError:
                    pass
                exceptRuleLines.append((pathToRegEx(file), xline,xdata))
            except ValueError:
                exceptRulePaths.append(pathToRegEx(path))
        for fileType in filesToMatch:
            fileList = getFilePathsFromWalk(osWalk, fileType, exceptPathsForEachRule)
# ------------------------------------------------------------------------------
            for path in exceptRulePaths:
                xFileList = []
                for file in fileList:
                    xFile = file.replace(join(checkPath, ""), "")
                    if not re.match(path, xFile):
                        xFileList.append(file)
                fileList = xFileList
# ------------------------------------------------------------------------------
            filesLinesList = fileList
            if rule['skip']:
                for skipper in rule['skip']:
                    filesLinesList = skipper(filesLinesList)
            else:
                for i,xFile in enumerate(filesLinesList):
                    filesLinesList[i]=((xFile,open(xFile).readlines()))
# ------------------------------------------------------------------------------
            for Nr, fileLine in enumerate(exceptRuleLines):
                regEx, line, lineEx = fileLine
                for index, file in enumerate(fileList):
                    xFile = file.replace(join(checkPath, ""), "")
                    if re.match(regEx, xFile):
                        filesLinesList[index] = (filesLinesList[index][0], omitLine(filesLinesList[index][1], line,lineEx))
            files.extend(filesLinesList)
# ------------------------------------------------------------------------------
        listRule = finds(files, rule['filter'], rule['exceptFilter'])
        result.append((ruleNr, splitPaths(listRule, checkPath)))
    return result
Beispiel #3
0
def runRules(ruleNumberList, directory):

    result = []
    osWalk = []

    for rule in ruleNumberList:
        if str(rule) not in RULES:
            print 'Error: wrong rule parameter. There is no such rule: "' + rule + '"\n\n' + rulesDescription
            print '\nWrite -h for help'
            return -1

    for rule in configuration.keys():
        configuration[rule]['filter'] = re.compile(
            configuration[rule]['filter'])

    osWalk.extend(os.walk(directory))

    exceptPathsForEachRule = []
    for path in exceptPaths:
        exceptPathsForEachRule.append(join(checkPath, path))

    for ruleNr in ruleNumberList:
        files = []
        rule = str(ruleNr)
        rule = configuration[ruleNr]

        filesToMatch = rule['filesToMatch']
        exceptRuleLines = []
        exceptRulePaths = []
        for path in rule['exceptPaths']:
            try:
                file, line = path.split(":", 1)
                xline = line
                xdata = None
                try:
                    xline, xdata = line.split(":", 1)
                    if not xline: xline = '.*'
                except ValueError:
                    pass
                exceptRuleLines.append((pathToRegEx(file), xline, xdata))
            except ValueError:
                exceptRulePaths.append(pathToRegEx(path))
        for fileType in filesToMatch:
            fileList = getFilePathsFromWalk(osWalk, fileType,
                                            exceptPathsForEachRule)
            # ------------------------------------------------------------------------------
            for path in exceptRulePaths:
                xFileList = []
                for file in fileList:
                    xFile = file.replace(join(checkPath, ""), "")
                    if not re.match(path, xFile):
                        xFileList.append(file)
                fileList = xFileList
# ------------------------------------------------------------------------------
            filesLinesList = fileList
            if rule['skip']:
                for skipper in rule['skip']:
                    filesLinesList = skipper(filesLinesList)
            else:
                for i, xFile in enumerate(filesLinesList):
                    filesLinesList[i] = ((xFile, open(xFile).readlines()))
# ------------------------------------------------------------------------------
            for Nr, fileLine in enumerate(exceptRuleLines):
                regEx, line, lineEx = fileLine
                for index, file in enumerate(fileList):
                    xFile = file.replace(join(checkPath, ""), "")
                    if re.match(regEx, xFile):
                        filesLinesList[index] = (filesLinesList[index][0],
                                                 omitLine(
                                                     filesLinesList[index][1],
                                                     line, lineEx))
            files.extend(filesLinesList)


# ------------------------------------------------------------------------------
        listRule = finds(files, rule['filter'], rule['exceptFilter'])
        result.append((ruleNr, splitPaths(listRule, checkPath)))
    return result