Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

61 lines
1.6 KiB

  1. from __future__ import generators
  2. import os
  3. import re
  4. import stat
  5. # This takes a DOS filename wildcard like *abc.t?t and returns a regex string that will match it.
  6. def GetRegExForDOSWildcard( wildcard ):
  7. # First find the base directory name.
  8. iLast = wildcard.rfind( "/" )
  9. if iLast == -1:
  10. iLast = wildcard.rfind( "\\" )
  11. if iLast == -1:
  12. dirName = "."
  13. dosStyleWildcard = wildcard
  14. else:
  15. dirName = wildcard[0:iLast]
  16. dosStyleWildcard = wildcard[iLast+1:]
  17. # Now generate a regular expression for the search.
  18. # DOS -> RE
  19. # * -> .*
  20. # . -> \.
  21. # ? -> .
  22. reString = dosStyleWildcard.replace( ".", r"\." ).replace( "*", ".*" ).replace( "?", "." ) + r'\Z'
  23. return reString
  24. #
  25. # Useful function to return a list of files in a directory based on a dos-style wildcard like "*.txt"
  26. #
  27. # for name in WildcardSearch( "d:/hl2/src4/dt*.cpp", 1 ):
  28. # print name
  29. #
  30. def WildcardSearch( wildcard, bRecurse=0 ):
  31. reString = GetRegExForDOSWildcard( wildcard )
  32. matcher = re.compile( reString, re.IGNORECASE )
  33. return __GetFiles_R( matcher, dirName, bRecurse )
  34. def __GetFiles_R( matcher, dirName, bRecurse ):
  35. fileList = []
  36. # For each file, see if we can find the regular expression.
  37. files = os.listdir( dirName )
  38. for baseName in files:
  39. filename = dirName + "/" + baseName
  40. mode = os.stat( filename )[stat.ST_MODE]
  41. if stat.S_ISREG( mode ):
  42. # Make sure the file matches the search string.
  43. if matcher.match( baseName ):
  44. fileList.append( filename )
  45. elif bRecurse and stat.S_ISDIR( mode ):
  46. fileList += __GetFiles_R( matcher, filename, bRecurse )
  47. return fileList