Joe Onorato | dc1a728 | 2009-08-04 15:58:26 -0400 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # |
| 3 | # Copyright (C) 2009 The Android Open Source Project |
| 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 | # |
| 19 | # Finds files with the specified name under a particular directory, stopping |
| 20 | # the search in a given subdirectory when the file is found. |
| 21 | # |
| 22 | |
| 23 | import os |
| 24 | import sys |
| 25 | |
Colin Cross | 9808645 | 2016-05-27 15:20:31 -0700 | [diff] [blame] | 26 | def perform_find(mindepth, prune, dirlist, filenames): |
Joe Onorato | dc1a728 | 2009-08-04 15:58:26 -0400 | [diff] [blame] | 27 | result = [] |
| 28 | pruneleaves = set(map(lambda x: os.path.split(x)[1], prune)) |
| 29 | for rootdir in dirlist: |
| 30 | rootdepth = rootdir.count("/") |
Conley Owens | d8a3285 | 2012-08-24 16:24:45 -0700 | [diff] [blame] | 31 | for root, dirs, files in os.walk(rootdir, followlinks=True): |
Joe Onorato | dc1a728 | 2009-08-04 15:58:26 -0400 | [diff] [blame] | 32 | # prune |
| 33 | check_prune = False |
| 34 | for d in dirs: |
| 35 | if d in pruneleaves: |
| 36 | check_prune = True |
| 37 | break |
| 38 | if check_prune: |
| 39 | i = 0 |
| 40 | while i < len(dirs): |
Andrew Stadler | fbe107a | 2009-08-11 15:05:59 -0700 | [diff] [blame] | 41 | if dirs[i] in prune: |
Joe Onorato | dc1a728 | 2009-08-04 15:58:26 -0400 | [diff] [blame] | 42 | del dirs[i] |
| 43 | else: |
| 44 | i += 1 |
| 45 | # mindepth |
| 46 | if mindepth > 0: |
| 47 | depth = 1 + root.count("/") - rootdepth |
| 48 | if depth < mindepth: |
| 49 | continue |
| 50 | # match |
Colin Cross | 9808645 | 2016-05-27 15:20:31 -0700 | [diff] [blame] | 51 | for filename in filenames: |
| 52 | if filename in files: |
| 53 | result.append(os.path.join(root, filename)) |
| 54 | del dirs[:] |
Joe Onorato | dc1a728 | 2009-08-04 15:58:26 -0400 | [diff] [blame] | 55 | return result |
| 56 | |
| 57 | def usage(): |
Colin Cross | 9808645 | 2016-05-27 15:20:31 -0700 | [diff] [blame] | 58 | sys.stderr.write("""Usage: %(progName)s [<options>] [--dir=<dir>] <filenames> |
Joe Onorato | dc1a728 | 2009-08-04 15:58:26 -0400 | [diff] [blame] | 59 | Options: |
| 60 | --mindepth=<mindepth> |
| 61 | Both behave in the same way as their find(1) equivalents. |
| 62 | --prune=<dirname> |
| 63 | Avoids returning results from inside any directory called <dirname> |
| 64 | (e.g., "*/out/*"). May be used multiple times. |
Colin Cross | 9808645 | 2016-05-27 15:20:31 -0700 | [diff] [blame] | 65 | --dir=<dir> |
| 66 | Add a directory to search. May be repeated multiple times. For backwards |
| 67 | compatibility, if no --dir argument is provided then all but the last entry |
| 68 | in <filenames> are treated as directories. |
Joe Onorato | dc1a728 | 2009-08-04 15:58:26 -0400 | [diff] [blame] | 69 | """ % { |
| 70 | "progName": os.path.split(sys.argv[0])[1], |
| 71 | }) |
| 72 | sys.exit(1) |
| 73 | |
| 74 | def main(argv): |
| 75 | mindepth = -1 |
| 76 | prune = [] |
Colin Cross | 9808645 | 2016-05-27 15:20:31 -0700 | [diff] [blame] | 77 | dirlist = [] |
Joe Onorato | dc1a728 | 2009-08-04 15:58:26 -0400 | [diff] [blame] | 78 | i=1 |
| 79 | while i<len(argv) and len(argv[i])>2 and argv[i][0:2] == "--": |
| 80 | arg = argv[i] |
| 81 | if arg.startswith("--mindepth="): |
| 82 | try: |
| 83 | mindepth = int(arg[len("--mindepth="):]) |
| 84 | except ValueError: |
| 85 | usage() |
| 86 | elif arg.startswith("--prune="): |
| 87 | p = arg[len("--prune="):] |
| 88 | if len(p) == 0: |
| 89 | usage() |
| 90 | prune.append(p) |
Colin Cross | 9808645 | 2016-05-27 15:20:31 -0700 | [diff] [blame] | 91 | elif arg.startswith("--dir="): |
| 92 | d = arg[len("--dir="):] |
| 93 | if len(p) == 0: |
| 94 | usage() |
| 95 | dirlist.append(d) |
Joe Onorato | dc1a728 | 2009-08-04 15:58:26 -0400 | [diff] [blame] | 96 | else: |
| 97 | usage() |
| 98 | i += 1 |
Colin Cross | 9808645 | 2016-05-27 15:20:31 -0700 | [diff] [blame] | 99 | if len(dirlist) == 0: # backwards compatibility |
| 100 | if len(argv)-i < 2: # need both <dirlist> and <filename> |
| 101 | usage() |
| 102 | dirlist = argv[i:-1] |
| 103 | filenames = [argv[-1]] |
| 104 | else: |
| 105 | if len(argv)-i < 1: # need <filename> |
| 106 | usage() |
| 107 | filenames = argv[i:] |
| 108 | results = list(set(perform_find(mindepth, prune, dirlist, filenames))) |
Joe Onorato | d36e945 | 2009-08-06 16:05:02 -0700 | [diff] [blame] | 109 | results.sort() |
Ishida, Haruyasu | 58e52b4 | 2010-12-20 15:37:54 +0100 | [diff] [blame] | 110 | for r in results: |
Joe Onorato | dc1a728 | 2009-08-04 15:58:26 -0400 | [diff] [blame] | 111 | print r |
| 112 | |
| 113 | if __name__ == "__main__": |
| 114 | main(sys.argv) |