diff options
| author | FluorescentCIAAfricanAmerican <[email protected]> | 2020-04-22 12:56:21 -0400 |
|---|---|---|
| committer | FluorescentCIAAfricanAmerican <[email protected]> | 2020-04-22 12:56:21 -0400 |
| commit | 3bf9df6b2785fa6d951086978a3e66f49427166a (patch) | |
| tree | 2c0f1f0c63c4832882bc93814ebd2c2b1c6224e5 /devtools/WildcardSearch.py | |
| download | archived-source-engine-2018-hl2-src-master.tar.xz archived-source-engine-2018-hl2-src-master.zip | |
Diffstat (limited to 'devtools/WildcardSearch.py')
| -rw-r--r-- | devtools/WildcardSearch.py | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/devtools/WildcardSearch.py b/devtools/WildcardSearch.py new file mode 100644 index 0000000..7477644 --- /dev/null +++ b/devtools/WildcardSearch.py @@ -0,0 +1,61 @@ + +from __future__ import generators +import os +import re +import stat + + +# This takes a DOS filename wildcard like *abc.t?t and returns a regex string that will match it. +def GetRegExForDOSWildcard( wildcard ): + # First find the base directory name. + iLast = wildcard.rfind( "/" ) + if iLast == -1: + iLast = wildcard.rfind( "\\" ) + + if iLast == -1: + dirName = "." + dosStyleWildcard = wildcard + else: + dirName = wildcard[0:iLast] + dosStyleWildcard = wildcard[iLast+1:] + + # Now generate a regular expression for the search. + # DOS -> RE + # * -> .* + # . -> \. + # ? -> . + reString = dosStyleWildcard.replace( ".", r"\." ).replace( "*", ".*" ).replace( "?", "." ) + return reString + + +# +# Useful function to return a list of files in a directory based on a dos-style wildcard like "*.txt" +# +# for name in WildcardSearch( "d:/hl2/src4/dt*.cpp", 1 ): +# print name +# +def WildcardSearch( wildcard, bRecurse=0 ): + reString = GetRegExForDOSWildcard( wildcard ) + matcher = re.compile( reString, re.IGNORECASE ) + + return __GetFiles_R( matcher, dirName, bRecurse ) + +def __GetFiles_R( matcher, dirName, bRecurse ): + fileList = [] + # For each file, see if we can find the regular expression. + files = os.listdir( dirName ) + for baseName in files: + filename = dirName + "/" + baseName + + mode = os.stat( filename )[stat.ST_MODE] + if stat.S_ISREG( mode ): + # Make sure the file matches the search string. + if matcher.match( baseName ): + fileList.append( filename ) + + elif bRecurse and stat.S_ISDIR( mode ): + fileList += __GetFiles_R( matcher, filename, bRecurse ) + + return fileList + + |