Commit 8ad7fba0 authored by blundell@chromium.org's avatar blundell@chromium.org

Augment mffr.py to be able to take arguments from a file.

Example use case is iterating on follow-up changes to a file changing
namespaces, impacting all the symbols within that file. 

R=joi

Review URL: https://chromiumcodereview.appspot.com/17527004

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@207883 0039d316-1c4b-4281-b951-d872f2087c98
parent 1046739a
...@@ -17,6 +17,7 @@ REGEXP uses full Python regexp syntax. REPLACEMENT can use ...@@ -17,6 +17,7 @@ REGEXP uses full Python regexp syntax. REPLACEMENT can use
back-references. back-references.
""" """
import optparse
import re import re
import subprocess import subprocess
import sys import sys
...@@ -73,25 +74,57 @@ def MultiFileFindReplace(original, replacement, file_globs): ...@@ -73,25 +74,57 @@ def MultiFileFindReplace(original, replacement, file_globs):
def main(): def main():
file_globs = [] parser = optparse.OptionParser(usage='''
force_unsafe_run = False (1) %prog <options> REGEXP REPLACEMENT
args = sys.argv[1:] REGEXP uses full Python regexp syntax. REPLACEMENT can use back-references.
while args and args[0].startswith('-'):
if args[0] == '-d': (2) %prog <options> -i <file>
file_globs = ['*.cc', '*.h', '*.m', '*.mm'] <file> should contain a list (in Python syntax) of
args = args[1:] [REGEXP, REPLACEMENT, [GLOBS]] lists, e.g.:
elif args[0] == '-g': [
file_globs.append(args[1]) [r"(foo|bar)", r"\1baz", ["*.cc", "*.h"]],
args = args[2:] ["54", "42"],
elif args[0] == '-f': ]
force_unsafe_run = True As shown above, [GLOBS] can be omitted for a given search-replace list, in which
args = args[1:] case the corresponding search-replace will use the globs specified on the
if not file_globs: command line.''')
file_globs = ['*.*'] parser.add_option('-d', action='store_true',
if not args: dest='use_default_glob',
print globals()['__doc__'] help='Perform the change on C++ and Objective-C(++) source '
'and header files.')
parser.add_option('-f', action='store_true',
dest='force_unsafe_run',
help='Perform the run even if there are uncommitted local '
'changes.')
parser.add_option('-g', action='append',
type='string',
default=[],
metavar="<glob>",
dest='user_supplied_globs',
help='Perform the change on the specified glob. Can be '
'specified multiple times, in which case the globs are '
'unioned.')
parser.add_option('-i', "--input_file",
type='string',
action='store',
default='',
metavar="<file>",
dest='input_filename',
help='Read arguments from <file> rather than the command '
'line. NOTE: To be sure of regular expressions being '
'interpreted correctly, use raw strings.')
opts, args = parser.parse_args()
if opts.use_default_glob and opts.user_supplied_globs:
print '"-d" and "-g" cannot be used together'
parser.print_help()
return 1 return 1
if not force_unsafe_run:
from_file = opts.input_filename != ""
if (from_file and len(args) != 0) or (not from_file and len(args) != 2):
parser.print_help()
return 1
if not opts.force_unsafe_run:
out, err = subprocess.Popen(['git', 'status', '--porcelain'], out, err = subprocess.Popen(['git', 'status', '--porcelain'],
stdout=subprocess.PIPE, stdout=subprocess.PIPE,
shell=_USE_SHELL).communicate() shell=_USE_SHELL).communicate()
...@@ -103,12 +136,32 @@ def main(): ...@@ -103,12 +136,32 @@ def main():
print '' print ''
print 'To override this safeguard, pass the -f flag.' print 'To override this safeguard, pass the -f flag.'
return 1 return 1
original = args[0]
replacement = args[1] global_file_globs = ['*.*']
print 'File globs: %s' % file_globs if opts.use_default_glob:
print 'Original: %s' % original global_file_globs = ['*.cc', '*.h', '*.m', '*.mm']
print 'Replacement: %s' % replacement elif opts.user_supplied_globs:
MultiFileFindReplace(original, replacement, file_globs) global_file_globs = opts.user_supplied_globs
# Construct list of search-replace tasks.
search_replace_tasks = []
if opts.input_filename == '':
original = args[0]
replacement = args[1]
search_replace_tasks.append([original, replacement, global_file_globs])
else:
f = open(opts.input_filename)
search_replace_tasks = eval("".join(f.readlines()))
for task in search_replace_tasks:
if len(task) == 2:
task.append(global_file_globs)
f.close()
for (original, replacement, file_globs) in search_replace_tasks:
print 'File globs: %s' % file_globs
print 'Original: %s' % original
print 'Replacement: %s' % replacement
MultiFileFindReplace(original, replacement, file_globs)
return 0 return 0
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment