pathSpecifiers.py (2815B)
1 #Embedded file name: C:\BuildAgent\work\20d73f1b24eed06e\eve\release\V24.01\packages\fsd\common\pathSpecifiers.py 2 import re 3 import itertools 4 import logging 5 log = logging.getLogger(__name__) 6 log.addHandler(logging.NullHandler()) 7 8 def GetPathSpecifierRegex(specifier): 9 10 def regexMatch(match): 11 if match.group(0) == '*': 12 return '[A-Za-z0-9_\\-\\.]*' 13 if match.group(0) == '...': 14 return '(?:[A-Za-z0-9_\\-\\.]+/)*[A-Za-z0-9_\\-\\.]*' 15 if match.group(0) == '.': 16 return '\\.' 17 return match.group(0) 18 19 return '^' + re.sub('(\\.\\.\\.|\\*|\\.)', regexMatch, specifier) + '$' 20 21 22 def CompilePathSpecifier(specifier): 23 return re.compile(GetPathSpecifierRegex(specifier)) 24 25 26 def ComposeFilenameAndDirectoriesForMatching(filename, directories): 27 filepathForMatching = '/'.join(directories) 28 if filepathForMatching != '': 29 filepathForMatching += '/' + filename 30 else: 31 filepathForMatching = filename 32 return filepathForMatching 33 34 35 def PathMatchesSpecifier(path, specifier): 36 return specifier.match(path) is not None 37 38 39 slashSeparator = re.compile('[/\\\\]') 40 41 def GetMaxRecursionDepth(specifier): 42 if '...' in specifier: 43 return -1 44 return len(re.split(slashSeparator, specifier)) - 1 45 46 47 def MatchSpecifierList(filename, directoriesFromRoot, specifierList): 48 path = ComposeFilenameAndDirectoriesForMatching(filename, directoriesFromRoot) 49 return MatchSpecifierListWithRelativePath(path, specifierList) 50 51 52 def MatchSpecifierListWithRelativePath(path, specifierList): 53 return any(itertools.imap(lambda p_s: PathMatchesSpecifier(p_s[0], p_s[1]), itertools.imap(lambda i: (path, i), specifierList))) 54 55 56 def CompilerSpecifierToList(specifier): 57 if isinstance(specifier, basestring): 58 return [CompilePathSpecifier(specifier)] 59 else: 60 return map(CompilePathSpecifier, specifier) 61 62 63 class PathConditional(object): 64 65 def __init__(self, includes = None, excludes = None): 66 if includes is not None: 67 self.includes = CompilerSpecifierToList(includes) 68 else: 69 self.includes = None 70 if excludes is not None: 71 self.excludes = CompilerSpecifierToList(excludes) 72 else: 73 self.excludes = None 74 75 def Matches(self, path): 76 if self.includes is not None: 77 if not MatchSpecifierListWithRelativePath(path, self.includes): 78 return False 79 if self.excludes is not None: 80 if MatchSpecifierListWithRelativePath(path, self.excludes): 81 log.debug('Path %s excluded' % path) 82 return False 83 log.debug('Path %s was not excluded by provided excludes' % path) 84 else: 85 log.debug('No exclude modifiers to test on %s' % path) 86 return True