| Index: tools/sort-headers.py
|
| diff --git a/tools/sort-headers.py b/tools/sort-headers.py
|
| index e51343ca861c54405e8c673e2985d075ff0e6480..2a17fddd88e13aecdaac4f63d553c7c0ee0a7eda 100755
|
| --- a/tools/sort-headers.py
|
| +++ b/tools/sort-headers.py
|
| @@ -10,10 +10,14 @@ Shows a diff and prompts for confirmation before doing the deed.
|
|
|
| import optparse
|
| import os
|
| +import subprocess
|
| import sys
|
| import termios
|
| import tty
|
|
|
| +# List of extensions whose headers this script knows how to sort.
|
| +_SOURCE_EXTENSIONS = ('h', 'hh', 'hpp', 'c', 'cc', 'cpp', 'cxx', 'mm',)
|
| +
|
| def YesNo(prompt):
|
| """Prompts with a yes/no question, returns True if yes."""
|
| print prompt,
|
| @@ -71,36 +75,80 @@ def SortHeader(infile, outfile):
|
| outfile.write(line)
|
|
|
|
|
| +def DiffAndConfirm(filename, should_confirm):
|
| + fixfilename = filename + '.new'
|
| + infile = open(filename, 'r')
|
| + outfile = open(fixfilename, 'w')
|
| + SortHeader(infile, outfile)
|
| + infile.close()
|
| + outfile.close() # Important so the below diff gets the updated contents.
|
| +
|
| + try:
|
| + diff = os.system('diff -u %s %s' % (filename, fixfilename))
|
| + if diff >> 8 == 0: # Check exit code.
|
| + print '%s: no change' % filename
|
| + return
|
| +
|
| + if not should_confirm or YesNo('Use new file (y/N)?'):
|
| + os.rename(fixfilename, filename)
|
| + finally:
|
| + try:
|
| + os.remove(fixfilename)
|
| + except OSError:
|
| + # If the file isn't there, we don't care.
|
| + pass
|
| +
|
| +
|
| +def GitShell(args, ignore_return=False):
|
| + """A shell invocation suitable for communicating with git. Returns
|
| + output as list of lines, raises exception on error.
|
| + """
|
| + job = subprocess.Popen(args,
|
| + shell=True,
|
| + stdout=subprocess.PIPE,
|
| + stderr=subprocess.STDOUT)
|
| + (out, err) = job.communicate()
|
| + if job.returncode != 0 and not ignore_return:
|
| + print out
|
| + raise Exception("Error %d running command %s" % (
|
| + job.returncode, args))
|
| + return out.split('\n')
|
| +
|
| +
|
| +def FilenamesFromGit(branch_name):
|
| + lines = GitShell('git diff --stat=600,500 %s' % branch_name)
|
| + filenames = []
|
| + for line in lines:
|
| + line = line.lstrip()
|
| + # Avoid summary line, and files that have been deleted (no plus).
|
| + if line.find('|') != -1 and line.find('+') != -1:
|
| + filename = line.split()[0]
|
| + if filename:
|
| + filename = filename.rstrip()
|
| + ext = filename.rsplit('.')[-1]
|
| + if ext in _SOURCE_EXTENSIONS:
|
| + filenames.append(filename)
|
| + return filenames
|
| +
|
| +
|
| def main():
|
| parser = optparse.OptionParser(usage='%prog filename1 filename2 ...')
|
| - opts, args = parser.parse_args()
|
| -
|
| - if len(args) < 1:
|
| + parser.add_option('-g', '--gitbranch', dest='git_branch',
|
| + help='Get list of files as diff from git branch.')
|
| + parser.add_option('-q', '--quiet', action='store_false', default=True,
|
| + dest='should_confirm',
|
| + help='Turn off confirmation prompt.')
|
| + opts, filenames = parser.parse_args()
|
| +
|
| + if len(filenames) < 1 and not opts.git_branch:
|
| parser.print_help()
|
| sys.exit(1)
|
|
|
| - for filename in args:
|
| - fixfilename = filename + '.new'
|
| - infile = open(filename, 'r')
|
| - outfile = open(fixfilename, 'w')
|
| - SortHeader(infile, outfile)
|
| - infile.close()
|
| - outfile.close() # Important so the below diff gets the updated contents.
|
| + if opts.git_branch:
|
| + filenames = FilenamesFromGit(opts.git_branch)
|
|
|
| - try:
|
| - diff = os.system('diff -u %s %s' % (filename, fixfilename))
|
| - if diff >> 8 == 0: # Check exit code.
|
| - print '%s: no change' % filename
|
| - continue
|
| -
|
| - if YesNo('Use new file (y/N)?'):
|
| - os.rename(fixfilename, filename)
|
| - finally:
|
| - try:
|
| - os.remove(fixfilename)
|
| - except OSError:
|
| - # If the file isn't there, we don't care.
|
| - pass
|
| + for filename in filenames:
|
| + DiffAndConfirm(filename, opts.should_confirm)
|
|
|
|
|
| if __name__ == '__main__':
|
|
|