#!/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 = FileUtils.find_file_paths(options.root, args, options.single) for fp in found_paths: print fp