treescan.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #!/usr/bin/env python
  2. #
  3. # Copyright 2010 The Closure Library Authors. All Rights Reserved.
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS-IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. """Shared utility functions for scanning directory trees."""
  17. import os
  18. import re
  19. __author__ = 'nnaze@google.com (Nathan Naze)'
  20. # Matches a .js file path.
  21. _JS_FILE_REGEX = re.compile(r'^.+\.js$')
  22. def ScanTreeForJsFiles(root):
  23. """Scans a directory tree for JavaScript files.
  24. Args:
  25. root: str, Path to a root directory.
  26. Returns:
  27. An iterable of paths to JS files, relative to cwd.
  28. """
  29. return ScanTree(root, path_filter=_JS_FILE_REGEX)
  30. def ScanTree(root, path_filter=None, ignore_hidden=True):
  31. """Scans a directory tree for files.
  32. Args:
  33. root: str, Path to a root directory.
  34. path_filter: A regular expression filter. If set, only paths matching
  35. the path_filter are returned.
  36. ignore_hidden: If True, do not follow or return hidden directories or files
  37. (those starting with a '.' character).
  38. Yields:
  39. A string path to files, relative to cwd.
  40. """
  41. def OnError(os_error):
  42. raise os_error
  43. for dirpath, dirnames, filenames in os.walk(root, onerror=OnError):
  44. # os.walk allows us to modify dirnames to prevent decent into particular
  45. # directories. Avoid hidden directories.
  46. for dirname in dirnames:
  47. if ignore_hidden and dirname.startswith('.'):
  48. dirnames.remove(dirname)
  49. for filename in filenames:
  50. # nothing that starts with '.'
  51. if ignore_hidden and filename.startswith('.'):
  52. continue
  53. fullpath = os.path.join(dirpath, filename)
  54. if path_filter and not path_filter.match(fullpath):
  55. continue
  56. yield os.path.normpath(fullpath)