annotate python/multigrep.py @ 563:2daf6543e42c

adding multigrep.py
author Jeff Hammel <jhammel@mozilla.com>
date Sun, 15 Dec 2013 22:19:05 -0800
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
563
2daf6543e42c adding multigrep.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1 #!/usr/bin/env python
2daf6543e42c adding multigrep.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
2
2daf6543e42c adding multigrep.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
3 """
2daf6543e42c adding multigrep.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
4 grep through multiple directories
2daf6543e42c adding multigrep.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
5 """
2daf6543e42c adding multigrep.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
6
2daf6543e42c adding multigrep.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
7 # TODO: use a python grep implemention v subshell (probably)
2daf6543e42c adding multigrep.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
8
2daf6543e42c adding multigrep.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
9 import optparse
2daf6543e42c adding multigrep.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
10 import os
2daf6543e42c adding multigrep.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
11 import subprocess
2daf6543e42c adding multigrep.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
12 import sys
2daf6543e42c adding multigrep.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
13
2daf6543e42c adding multigrep.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
14 def main(args=sys.argv[1:]):
2daf6543e42c adding multigrep.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
15
2daf6543e42c adding multigrep.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
16 # CLI
2daf6543e42c adding multigrep.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
17 usage = '%prog [options] PATTERN DIRECTORY [DIRECTORY] [...]'
2daf6543e42c adding multigrep.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
18 parser = optparse.OptionParser(usage=usage, description=__doc__)
2daf6543e42c adding multigrep.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
19 parser.add_option('-f', '--files', dest='files', default='*',
2daf6543e42c adding multigrep.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
20 help="file pattern [DEFAULT: %default]")
2daf6543e42c adding multigrep.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
21 parser.add_option('-g', '--grep', dest='grep',
2daf6543e42c adding multigrep.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
22 action='append', default=[],
2daf6543e42c adding multigrep.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
23 help="`grep` command [DEFAULT: grep]")
2daf6543e42c adding multigrep.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
24 parser.add_option('-p', '--print', dest='print_commands',
2daf6543e42c adding multigrep.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
25 action='store_true', default=False,
2daf6543e42c adding multigrep.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
26 help="print grep commands to run and exit")
2daf6543e42c adding multigrep.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
27 options, args = parser.parse_args(args)
2daf6543e42c adding multigrep.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
28 if len(args) < 2:
2daf6543e42c adding multigrep.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
29 parser.print_help()
2daf6543e42c adding multigrep.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
30 parser.exit()
2daf6543e42c adding multigrep.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
31 options.grep = options.grep or ['grep']
2daf6543e42c adding multigrep.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
32 pattern, directories = args[0], args[1:]
2daf6543e42c adding multigrep.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
33
2daf6543e42c adding multigrep.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
34 # verify directories
2daf6543e42c adding multigrep.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
35 missing = [i for i in directories
2daf6543e42c adding multigrep.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
36 if not os.path.isdir(i)]
2daf6543e42c adding multigrep.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
37 if missing:
2daf6543e42c adding multigrep.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
38 sep = '\n '
2daf6543e42c adding multigrep.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
39 parser.error("Not a directory:%s%s" % (sep, sep.join(missing)))
2daf6543e42c adding multigrep.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
40
2daf6543e42c adding multigrep.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
41 # build command line
2daf6543e42c adding multigrep.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
42 command = options.grep[:]
2daf6543e42c adding multigrep.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
43 command.extend([pattern, options.files])
2daf6543e42c adding multigrep.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
44 cwd = os.getcwd()
2daf6543e42c adding multigrep.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
45
2daf6543e42c adding multigrep.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
46 if options.print_commands:
2daf6543e42c adding multigrep.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
47 # print grep commands
2daf6543e42c adding multigrep.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
48 for directory in directories:
2daf6543e42c adding multigrep.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
49 print "cd %s; %s" % (repr(directory), subprocess.list2cmdline(command))
2daf6543e42c adding multigrep.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
50 print "cd %s" % (repr(cwd))
2daf6543e42c adding multigrep.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
51 parser.exit()
2daf6543e42c adding multigrep.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
52
2daf6543e42c adding multigrep.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
53 # loop through directories
2daf6543e42c adding multigrep.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
54 for directory in directories:
2daf6543e42c adding multigrep.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
55 print directory
2daf6543e42c adding multigrep.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
56 subprocess.call(subprocess.list2cmdline(command),
2daf6543e42c adding multigrep.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
57 cwd=directory,
2daf6543e42c adding multigrep.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
58 shell=True)
2daf6543e42c adding multigrep.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
59
2daf6543e42c adding multigrep.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
60 if __name__ == '__main__':
2daf6543e42c adding multigrep.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
61 main()