changeset 563:2daf6543e42c

adding multigrep.py
author Jeff Hammel <jhammel@mozilla.com>
date Sun, 15 Dec 2013 22:19:05 -0800
parents 8d28fce0a6d6
children 047a53a8fbad
files .bashrc python/multigrep.py
diffstat 2 files changed, 67 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- 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 *;
--- /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()