# HG changeset patch # User Jeff Hammel # Date 1387174745 28800 # Node ID 2daf6543e42c2e4e6e9fe8071466481ffcca6d87 # Parent 8d28fce0a6d62614dcf670c3dc399a6085b851e8 adding multigrep.py diff -r 8d28fce0a6d6 -r 2daf6543e42c .bashrc --- a/.bashrc Wed Dec 11 02:52:53 2013 -0800 +++ b/.bashrc Sun Dec 15 22:19:05 2013 -0800 @@ -483,6 +483,12 @@ combinediff <(git diff) <(git diff --cached) } +hg-add-commit() { + # add a file + commit + MESSAGE=$(hg add $@) + hg commit -m "${MESSAGE}" +} + hg-update-all() { # update all hg repositories in the current directory for i in *; diff -r 8d28fce0a6d6 -r 2daf6543e42c python/multigrep.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/python/multigrep.py Sun Dec 15 22:19:05 2013 -0800 @@ -0,0 +1,61 @@ +#!/usr/bin/env python + +""" +grep through multiple directories +""" + +# TODO: use a python grep implemention v subshell (probably) + +import optparse +import os +import subprocess +import sys + +def main(args=sys.argv[1:]): + + # CLI + usage = '%prog [options] PATTERN DIRECTORY [DIRECTORY] [...]' + parser = optparse.OptionParser(usage=usage, description=__doc__) + parser.add_option('-f', '--files', dest='files', default='*', + help="file pattern [DEFAULT: %default]") + parser.add_option('-g', '--grep', dest='grep', + action='append', default=[], + help="`grep` command [DEFAULT: grep]") + parser.add_option('-p', '--print', dest='print_commands', + action='store_true', default=False, + help="print grep commands to run and exit") + options, args = parser.parse_args(args) + if len(args) < 2: + parser.print_help() + parser.exit() + options.grep = options.grep or ['grep'] + pattern, directories = args[0], args[1:] + + # verify directories + missing = [i for i in directories + if not os.path.isdir(i)] + if missing: + sep = '\n ' + parser.error("Not a directory:%s%s" % (sep, sep.join(missing))) + + # build command line + command = options.grep[:] + command.extend([pattern, options.files]) + cwd = os.getcwd() + + if options.print_commands: + # print grep commands + for directory in directories: + print "cd %s; %s" % (repr(directory), subprocess.list2cmdline(command)) + print "cd %s" % (repr(cwd)) + parser.exit() + + # loop through directories + for directory in directories: + print directory + subprocess.call(subprocess.list2cmdline(command), + cwd=directory, + shell=True) + +if __name__ == '__main__': + main()