Home | History | Annotate | Download | only in tools
      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 
     26 def perform_find(mindepth, prune, dirlist, filename):
     27   result = []
     28   pruneleaves = set(map(lambda x: os.path.split(x)[1], prune))
     29   for rootdir in dirlist:
     30     rootdepth = rootdir.count("/")
     31     for root, dirs, files in os.walk(rootdir):
     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):
     41           if dirs[i] in prune:
     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
     51       if filename in files:
     52         result.append(os.path.join(root, filename))
     53         del dirs[:]
     54   return result
     55 
     56 def usage():
     57   sys.stderr.write("""Usage: %(progName)s [<options>] <dirlist> <filename>
     58 Options:
     59    --mindepth=<mindepth>
     60        Both behave in the same way as their find(1) equivalents.
     61    --prune=<dirname>
     62        Avoids returning results from inside any directory called <dirname>
     63        (e.g., "*/out/*"). May be used multiple times.
     64 """ % {
     65       "progName": os.path.split(sys.argv[0])[1],
     66     })
     67   sys.exit(1)
     68 
     69 def main(argv):
     70   mindepth = -1
     71   prune = []
     72   i=1
     73   while i<len(argv) and len(argv[i])>2 and argv[i][0:2] == "--":
     74     arg = argv[i]
     75     if arg.startswith("--mindepth="):
     76       try:
     77         mindepth = int(arg[len("--mindepth="):])
     78       except ValueError:
     79         usage()
     80     elif arg.startswith("--prune="):
     81       p = arg[len("--prune="):]
     82       if len(p) == 0:
     83         usage()
     84       prune.append(p)
     85     else:
     86       usage()
     87     i += 1
     88   if len(argv)-i < 2: # need both <dirlist> and <filename>
     89     usage()
     90   dirlist = argv[i:-1]
     91   filename = argv[-1]
     92   results = perform_find(mindepth, prune, dirlist, filename)
     93   results.sort()
     94   for r in set(results):
     95     print r
     96 
     97 if __name__ == "__main__":
     98   main(sys.argv)
     99