Beispiel #1
0
 def scandir(self, path):
     # Invoke the main walker lib
     try:
         
         walker = paths_from_path_patterns([path],
                 dirs="always",
                 follow_symlinks=True,
                 includes=self.opts.get("includes", []),
                 excludes=self.opts.get("excludes", []),
                 yield_structure=True,
                 recursive=False)
         for subPath, dirnames, filenames in walker: # recursive=false means we only get 1 result
             return [dirnames, filenames]
         
     except OSError, e:
         log.error("OSError while walking path: " + path)
Beispiel #2
0
 def scandir(self, path):
     # Invoke the main walker lib
     try:
         
         walker = paths_from_path_patterns([path],
                 dirs="always",
                 follow_symlinks=True,
                 includes=self.opts.get("includes", []),
                 excludes=self.opts.get("excludes", []),
                 yield_structure=True,
                 recursive=False)
         for subPath, dirnames, filenames in walker: # recursive=false means we only get 1 result
             return [dirnames, filenames]
         
     except OSError, e:
         log.error("OSError while walking path: " + path)
Beispiel #3
0
 def genLocalPaths(self, gatherDirs=False, follow_symlinks=True):
     sys.path.insert(0, join(dirname(dirname(dirname(dirname(
         abspath(__file__))))), "find"))
     try:
         import findlib2
     finally:
         del sys.path[0]
     paths = findlib2.paths_from_path_patterns(
         [self._base_dir],
         dirs=(gatherDirs and "always" or "never"),
         recursive=True,
         includes=self._includes,
         excludes=self._excludes,
         on_error=None,
         follow_symlinks=follow_symlinks,
         skip_dupe_dirs=True)
     for path in paths:
         yield path
         if self._slow:
             time.sleep(0.1)
Beispiel #4
0
 def genLocalPaths(self, gatherDirs=False, follow_symlinks=True):
     sys.path.insert(0, join(dirname(dirname(dirname(dirname(
         abspath(__file__))))), "find"))
     try:
         import findlib2
     finally:
         del sys.path[0]
     paths = findlib2.paths_from_path_patterns(
         [self._base_dir],
         dirs=(gatherDirs and "always" or "never"),
         recursive=True,
         includes=self._includes,
         excludes=self._excludes,
         on_error=None,
         follow_symlinks=follow_symlinks,
         skip_dupe_dirs=True)
     for path in paths:
         yield path
         if self._slow:
             time.sleep(0.1)
Beispiel #5
0
def main(argv):
    if "--test" in argv:  # Quick self-test.
        import doctest
        nerrors, ntests = doctest.testmod()
        return nerrors

    usage = "usage: %prog PATTERN FILES..."
    version = "%prog "+__version__
    parser = optparse.OptionParser(usage=usage,
        version=version, description=__doc__,
        formatter=_NoReflowFormatter())
    parser.add_option("-q", "--quiet", dest="log_level",
        action="store_const", const=logging.WARNING,
        help="quieter output")
    parser.add_option("-v", "--verbose", dest="log_level",
        action="store_const", const=logging.INFO-1,
        help="more verbose output")
    parser.add_option("-d", "--debug", dest="log_level",
        action="store_const", const=logging.DEBUG,
        help="verbose debugging output")
    parser.add_option("-w", "--word", action="store_true",
        help="restrict pattern match to whole words")
    parser.add_option("-l", "--list", action="store_true",
        help="list matching files (instead of the matches within them)")
#TODO: Need to handle grouping contiguous blocks for this. Use the
#      Provided hit.lines_with_context(n) for this.
#    parser.add_option("-C", "--context", type="int", metavar="NUM",
#         help="Print NUM lines of context.")
    parser.add_option("-r", "--recursive", action="store_true",
        help="find files recursively")
    parser.add_option("-n", dest="show_line_number", action="store_true",
        help="show line numbers for each hit")
    parser.add_option("-u", "--undo", metavar="[ID]", dest="undo",
        action="callback", callback=_optparse_undo_arg,
        help="Without an argument this will list replacements that can "
             "be undone (the last 5, most recent first). Specify a "
             "replacement id to undo it.")
    parser.add_option("-i", "--include", dest="includes",
        action="append", metavar="PATTERN",
        help="Path patterns to include. Alternatively, the argument can "
             "be of the form FIELD:VALUE to filter based on textinfo "
             "attributes of a file; for example, '-i lang:Python'.")
    parser.add_option("-x", "--exclude", dest="excludes",
        action="append", metavar="PATTERN",
        help="Path patterns to exclude. Alternatively, the argument can "
             "be of the form FIELD:VALUE to filter based on textinfo "
             "attributes of a file; for example, '-x encoding:ascii'.")
    parser.add_option("-f", "--force", dest="confirm", action="store_false",
        help="Make replacements without confirmation.")
    parser.add_option("-c", "--confirm", action="store_true",
        help="Confirm replacements before making any changes on disk "
             "(the default).")
    parser.add_option("--first-on-line", action="store_true",
        help="When replacing, only replace first instance on a line. (This "
             "is to support Vi's replacement without 'g' flag.)")
    parser.add_option("--dry-run", action="store_true",
        help="Do a dry-run replacement.")
    parser.set_defaults(log_level=logging.INFO, recursive=False,
        show_line_number=False, word=False, list=False, context=0,
        includes=[], excludes=[], confirm=True, first_on_line=False,
        dry_run=False)
    opts, args = parser.parse_args()
    log.setLevel(opts.log_level)
    findlib2.log.setLevel(opts.log_level)

    # The -u|--undo actions don't use and args. Handle them first.
    if opts.undo is True:
        return main_list_journals(opts)
    elif opts.undo is not None:
        return main_undo(opts)

    # Process includes and excludes.
    path_includes = []
    textinfo_includes = []
    for i in opts.includes:
        if '=' in i:
            field, value = i.split('=', 1)
            textinfo_includes.append( (field, _value_from_str(value)) )
        elif ':' in i:
            field, value = i.split(':', 1)
            textinfo_includes.append( (field, _value_from_str(value)) )
        else:
            path_includes.append(i)
    path_excludes = []
    textinfo_excludes = []
    for i in opts.excludes:
        if '=' in i:
            field, value = i.split('=', 1)
            textinfo_excludes.append( (field, _value_from_str(value)) )
        elif ':' in i:
            field, value = i.split(':', 1)
            textinfo_excludes.append( (field, _value_from_str(value)) )
        else:
            path_excludes.append(i)

    # Validate and prepare the args.
    if len(args) < 1:
        log.error("incorrect number of arguments (see `%s --help')", argv[0])
        return 1
    elif len(args) == 1:
        # GNU find-like functionality uses one arg.
        action = "find"
        path_patterns = args
        recursive = True    # -r is implied for find functionality
    else:
        pattern_str, path_patterns = args[0], args[1:]
        regex, repl = findlib2.regex_info_from_str(
            pattern_str, word_match=opts.word, universal_newlines=True)
        action = (repl is None and "grep" or "replace")
        if opts.list:
            if action == "replace":
                raise FrepError("cannot use -l|--list for a replacement")
            action = "grep-list"
        recursive = opts.recursive

    # Dispatch to the appropriate action.
    paths = findlib2.paths_from_path_patterns(
                path_patterns, recursive=recursive,
                follow_symlinks=True,
                includes=path_includes, excludes=path_excludes)
    if log.isEnabledFor(logging.DEBUG):
        def log_it_first(paths):
            for path in paths:
                log.debug("considering '%s'...", path)
                yield path
        paths = log_it_first(paths)
    if action == "find":
        return main_find(paths, textinfo_includes, textinfo_excludes, opts)
    if action == "grep-list":
        return main_find_matching_files(regex, paths,
            textinfo_includes, textinfo_excludes, opts)
    elif action == "grep":
        return main_grep(regex, paths,
            textinfo_includes, textinfo_excludes, opts)
    elif action == "replace":
        return main_replace(regex, repl, paths,
            textinfo_includes, textinfo_excludes, opts.confirm,
            argv, opts)
    else:
        raise FrepError("unexpected action: %r" % action)