1ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com#!/usr/bin/env python 28a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com# 3ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com# Copyright 2010 The Closure Library Authors. All Rights Reserved. 48a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com# 5ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com# Licensed under the Apache License, Version 2.0 (the "License"); 6ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com# you may not use this file except in compliance with the License. 78a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com# You may obtain a copy of the License at 88a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com# 9ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com# http://www.apache.org/licenses/LICENSE-2.0 108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com# 118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com# Unless required by applicable law or agreed to in writing, software 128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com# distributed under the License is distributed on an "AS-IS" BASIS, 138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com# See the License for the specific language governing permissions and 158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com# limitations under the License. 168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 17c73dd5c6880739f26216f198c757028fd28df1a4djsollen@google.com 184991b8f23482afc1494fd17647421ce68de53331robertphillips@google.com"""Shared utility functions for scanning directory trees.""" 198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comimport os 218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comimport re 228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com__author__ = 'nnaze@google.com (Nathan Naze)' 258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com# Matches a .js file path. 287ffb1b21abcc7bbed5a0fc711f6dd7b9dbb4f577ctguil@chromium.org_JS_FILE_REGEX = re.compile(r'^.+\.js$') 298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 300456e0b7b85060e9b9597ce414c4c2b19aff4f58robertphillips@google.com 310456e0b7b85060e9b9597ce414c4c2b19aff4f58robertphillips@google.comdef ScanTreeForJsFiles(root): 324e2b3d3fb1288c6dc0f3ea1c0aa4a0d7c603bd7breed@google.com """Scans a directory tree for JavaScript files. 3379fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org 3479fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org Args: 3579fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org root: str, Path to a root directory. 3679fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org 374e2b3d3fb1288c6dc0f3ea1c0aa4a0d7c603bd7breed@google.com Returns: 38e3beb6bd7de7fa211681abbb0be58e80b19885e0commit-bot@chromium.org An iterable of paths to JS files, relative to cwd. 3979fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org """ 4079fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org return ScanTree(root, path_filter=_JS_FILE_REGEX) 4179fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org 4279fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org 4379fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.orgdef ScanTree(root, path_filter=None, ignore_hidden=True): 4479fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org """Scans a directory tree for files. 4579fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org 4679fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org Args: 4779fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org root: str, Path to a root directory. 4879fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org path_filter: A regular expression filter. If set, only paths matching 4979fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org the path_filter are returned. 5079fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org ignore_hidden: If True, do not follow or return hidden directories or files 5179fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org (those starting with a '.' character). 5279fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org 5379fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org Yields: 5479fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org A string path to files, relative to cwd. 5579fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org """ 5679fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org 5779fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org def OnError(os_error): 5879fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org raise os_error 594e2b3d3fb1288c6dc0f3ea1c0aa4a0d7c603bd7breed@google.com 604e2b3d3fb1288c6dc0f3ea1c0aa4a0d7c603bd7breed@google.com for dirpath, dirnames, filenames in os.walk(root, onerror=OnError): 6179fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org # os.walk allows us to modify dirnames to prevent decent into particular 6279fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org # directories. Avoid hidden directories. 6379fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org for dirname in dirnames: 6479fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org if ignore_hidden and dirname.startswith('.'): 6579fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org dirnames.remove(dirname) 6679fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org 674e2b3d3fb1288c6dc0f3ea1c0aa4a0d7c603bd7breed@google.com for filename in filenames: 6879fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org 6979fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org # nothing that starts with '.' 7079fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org if ignore_hidden and filename.startswith('.'): 7179fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org continue 7279fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org 7379fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org fullpath = os.path.join(dirpath, filename) 7479fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org 7579fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org if path_filter and not path_filter.match(fullpath): 76fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com continue 779efd9a048aebaa6681afb76b18e1a7dd642078d3reed@google.com 789efd9a048aebaa6681afb76b18e1a7dd642078d3reed@google.com yield os.path.normpath(fullpath) 799efd9a048aebaa6681afb76b18e1a7dd642078d3reed@google.com