3 # Copyright 2010 The Closure Library Authors. All Rights Reserved.
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
9 # http://www.apache.org/licenses/LICENSE-2.0
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.
18 """Shared utility functions for scanning directory trees."""
24 __author__
= 'nnaze@google.com (Nathan Naze)'
27 # Matches a .js file path.
28 _JS_FILE_REGEX
= re
.compile(r
'^.+\.js$')
31 def ScanTreeForJsFiles(root
):
32 """Scans a directory tree for JavaScript files.
35 root: str, Path to a root directory.
38 An iterable of paths to JS files, relative to cwd.
40 return ScanTree(root
, path_filter
=_JS_FILE_REGEX
)
43 def ScanTree(root
, path_filter
=None, ignore_hidden
=True):
44 """Scans a directory tree for files.
47 root: str, Path to a root directory.
48 path_filter: A regular expression filter. If set, only paths matching
49 the path_filter are returned.
50 ignore_hidden: If True, do not follow or return hidden directories or files
51 (those starting with a '.' character).
54 A string path to files, relative to cwd.
57 def OnError(os_error
):
60 for dirpath
, dirnames
, filenames
in os
.walk(root
, onerror
=OnError
):
61 # os.walk allows us to modify dirnames to prevent decent into particular
62 # directories. Avoid hidden directories.
63 for dirname
in dirnames
:
64 if ignore_hidden
and dirname
.startswith('.'):
65 dirnames
.remove(dirname
)
67 for filename
in filenames
:
69 # nothing that starts with '.'
70 if ignore_hidden
and filename
.startswith('.'):
73 fullpath
= os
.path
.join(dirpath
, filename
)
75 if path_filter
and not path_filter
.match(fullpath
):
78 yield os
.path
.normpath(fullpath
)