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 17 18"""Shared utility functions for scanning directory trees.""" 19 20import os 21import re 22 23 24__author__ = 'nnaze@google.com (Nathan Naze)' 25 26 27# Matches a .js file path. 28_JS_FILE_REGEX = re.compile(r'^.+\.js$') 29 30 31def ScanTreeForJsFiles(root): 32 """Scans a directory tree for JavaScript files. 33 34 Args: 35 root: str, Path to a root directory. 36 37 Returns: 38 An iterable of paths to JS files, relative to cwd. 39 """ 40 return ScanTree(root, path_filter=_JS_FILE_REGEX) 41 42 43def ScanTree(root, path_filter=None, ignore_hidden=True): 44 """Scans a directory tree for files. 45 46 Args: 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). 52 53 Yields: 54 A string path to files, relative to cwd. 55 """ 56 57 def OnError(os_error): 58 raise os_error 59 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) 66 67 for filename in filenames: 68 69 # nothing that starts with '.' 70 if ignore_hidden and filename.startswith('.'): 71 continue 72 73 fullpath = os.path.join(dirpath, filename) 74 75 if path_filter and not path_filter.match(fullpath): 76 continue 77 78 yield os.path.normpath(fullpath) 79