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