build/buildutils/finddirs.py
changeset 80 d6dafc5d983f
equal deleted inserted replaced
78:71ad690e91f5 80:d6dafc5d983f
       
     1 #
       
     2 # Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
       
     3 # All rights reserved.
       
     4 # This component and the accompanying materials are made available
       
     5 # under the terms of "Eclipse Public License v1.0"
       
     6 # which accompanies this distribution, and is available
       
     7 # at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 #
       
     9 # Initial Contributors:
       
    10 # Nokia Corporation - initial contribution.
       
    11 #
       
    12 # Contributors:
       
    13 #
       
    14 # Description: Find all paths matching pattern
       
    15 #
       
    16 #! /usr/bin/python
       
    17 
       
    18 import os.path, fnmatch, sys
       
    19 from optparse import OptionParser
       
    20 
       
    21 def findDirs(root, pattern, exclude):
       
    22     result = []
       
    23     for path, dirs, files in os.walk(root):
       
    24         if exclude and fnmatch.filter(path.replace("\\", "/").split("/"), exclude):
       
    25             continue
       
    26         result.extend([os.path.join(path, d) 
       
    27                        for d in fnmatch.filter(dirs, pattern)])
       
    28     return result
       
    29 
       
    30 def main():
       
    31     parser = OptionParser()
       
    32     parser.add_option(
       
    33         "-e", "--exclude", dest = "exclude", 
       
    34         help = "Exclude paths having this pattern as directory component, e.g. tsrc")
       
    35     parser.add_option(
       
    36         "-o", "--output", dest = "out", help = "Output file")
       
    37     (options, args) = parser.parse_args()
       
    38 
       
    39     if len(args) != 2:
       
    40         print "Usage: %s [options] <root> <pattern>" % sys.argv[0]
       
    41         sys.exit(1)
       
    42 
       
    43     root = args[0]
       
    44     pattern = args[1]
       
    45 
       
    46     dirs = findDirs(root, pattern, options.exclude)
       
    47     output = "\n".join(dirs) + "\n"
       
    48 
       
    49     if options.out:
       
    50         open(options.out, "w").write(output)
       
    51         print "Wrote %s" % options.out
       
    52     else:
       
    53         print output
       
    54 
       
    55     
       
    56 if __name__ == "__main__":
       
    57     main()