123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- """Shared utility functions for scanning directory trees."""
- import os
- import re
- __author__ = 'nnaze@google.com (Nathan Naze)'
- _JS_FILE_REGEX = re.compile(r'^.+\.js$')
- def ScanTreeForJsFiles(root):
- """Scans a directory tree for JavaScript files.
- Args:
- root: str, Path to a root directory.
- Returns:
- An iterable of paths to JS files, relative to cwd.
- """
- return ScanTree(root, path_filter=_JS_FILE_REGEX)
- def ScanTree(root, path_filter=None, ignore_hidden=True):
- """Scans a directory tree for files.
- Args:
- root: str, Path to a root directory.
- path_filter: A regular expression filter. If set, only paths matching
- the path_filter are returned.
- ignore_hidden: If True, do not follow or return hidden directories or files
- (those starting with a '.' character).
- Yields:
- A string path to files, relative to cwd.
- """
- def OnError(os_error):
- raise os_error
- for dirpath, dirnames, filenames in os.walk(root, onerror=OnError):
-
-
- for dirname in dirnames:
- if ignore_hidden and dirname.startswith('.'):
- dirnames.remove(dirname)
- for filename in filenames:
-
- if ignore_hidden and filename.startswith('.'):
- continue
- fullpath = os.path.join(dirpath, filename)
- if path_filter and not path_filter.match(fullpath):
- continue
- yield os.path.normpath(fullpath)
|