From cc7b42ff1571d8b79e503a4016c2402b30836188 Mon Sep 17 00:00:00 2001 From: Michael Orlitzky Date: Thu, 12 Nov 2009 20:07:42 -0500 Subject: [PATCH] Added the find_file_paths script which utilizes the new find_file_paths function within the FileUtils module. --- bin/find_file_paths | 87 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100755 bin/find_file_paths diff --git a/bin/find_file_paths b/bin/find_file_paths new file mode 100755 index 0000000..cf4d9c5 --- /dev/null +++ b/bin/find_file_paths @@ -0,0 +1,87 @@ +#!/usr/bin/env python + +""" +Find the location of one or more filenames, and output them to +stdout. The search can be anchored within a directory using the --root +parameter. By default, all matching paths (for all arguments) will be +output. However, if the --single flag is set, only the first path +found will be output. + +Examples: + +$ ./find_file_paths --root /bin -s mv + + /bin/mv + + +$ ./find_file_paths --root /bin mv ls + + /bin/mv + /bin/ls + +""" + +from optparse import OptionParser +import os +import site +import sys + +# Basically, add '../src' to our path. +# Needed for the imports that follow. +site.addsitedir(os.path.dirname(os.path.abspath(sys.argv[0])) + '/../src') + +import CLI +import ExitCodes +import FileUtils + + +usage = '%prog [options] file1 [file2, file3...]' +fmtr = CLI.VerbatimHelpFormatter() +parser = OptionParser(usage, formatter=fmtr) + +parser.add_option('-r', + '--root', + help='The folder under which to search. Defaults to /.', + default='/') + +parser.add_option('-s', + '--single', + action='store_true', + help='Return only the first matching path.', + default=False) + +# Use this module's docstring as the description. +parser.description = __doc__ + +(options, args) = parser.parse_args() + + +if len(args) < 1: + print "\nERROR: You must supply at least one filename.\n" + parser.print_help() + print '' # Print a newline. + raise SystemExit(ExitCodes.NOT_ENOUGH_ARGS) + + +found_paths = [] + +# Build a list of found paths, in case there are / we want more than +# one. +for filename in args: + # If --single was passed, we relay it to the find_file_paths + # function so that it completes sooner (it doesn't have to keep + # looking after the first match). + found_paths += FileUtils.find_file_paths(options.root, + filename, + options.single) + + if options.single and (len(found_paths) > 0): + # If we found anything and we only want the first match, just + # print it and quit. + print found_paths[0] + raise SystemExit(ExitCodes.EXIT_SUCCESS) + + +# If we weren't passed --single, spit out all matches. +for fp in found_paths: + print fp -- 2.43.2