1# Copyright (C) 2018 The Android Open Source Project 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14""" Script to list all files in a directory filtering by pattern. 15 16Do NOT use this script to pull in sources for GN targets. Globbing inputs is 17a bad idea, as it plays very badly with git leaving untracked files around. This 18script should be used only for cases where false positives won't affect the 19output of the build but just cause spurious re-runs (e.g. as input section of 20an "action" target). 21""" 22from __future__ import print_function 23import argparse 24import fnmatch 25import os 26import sys 27 28 29def make_parent_dirs(file_path): 30 directory = os.path.dirname(file_path) 31 if not os.path.exists(directory): 32 os.makedirs(directory) 33 34 35def main(): 36 parser = argparse.ArgumentParser() 37 parser.add_argument('--filter', default=[], action='append') 38 parser.add_argument('--exclude', default=[], action='append') 39 parser.add_argument('--deps', default=None) 40 parser.add_argument('--output', default=None) 41 parser.add_argument('--root', required=True) 42 args = parser.parse_args() 43 44 if args.output: 45 make_parent_dirs(args.output) 46 fout = open(args.output, 'w') 47 else: 48 fout = sys.stdout 49 50 def writepath(path): 51 if args.deps: 52 path = '\t' + path 53 print(path, file=fout) 54 55 root = args.root 56 if not root.endswith('/'): 57 root += '/' 58 if not os.path.exists(root): 59 return 0 60 61 if args.deps: 62 print(args.deps + ':', file=fout) 63 for pardir, dirs, files in os.walk(root, topdown=True): 64 assert pardir.startswith(root) 65 relpar = pardir[len(root):] 66 dirs[:] = [d for d in dirs if os.path.join(relpar, d) not in args.exclude] 67 for fname in files: 68 fpath = os.path.join(pardir, fname) 69 match = len(args.filter) == 0 70 for filter in args.filter: 71 if fnmatch.fnmatch(fpath, filter): 72 match = True 73 break 74 if match: 75 writepath(fpath) 76 77 78if __name__ == '__main__': 79 sys.exit(main()) 80