[Torque] format-torque.py accepts wildcards

Now you can type:
  tools/torque/format-torque.py -i src/builtins/*.tq

  to format all the torque files in a particular directory. Is handy.

Bug: v8:7793
Change-Id: I334b2c555c63fd7864636ebfd83a2631a5d44806
Reviewed-on: https://chromium-review.googlesource.com/c/1333671
Reviewed-by: Daniel Clifford <danno@chromium.org>
Commit-Queue: Michael Stanton <mvstanton@chromium.org>
Cr-Commit-Position: refs/heads/master@{#57479}
This commit is contained in:
Mike Stanton 2018-11-13 14:45:56 +01:00 committed by Commit Bot
parent b50ad96be8
commit 96a17c03da

View File

@ -81,30 +81,19 @@ def postprocess(output):
return output
if len(sys.argv) < 2 or len(sys.argv) > 3:
print "invalid number of arguments"
sys.exit(-1)
use_stdout = True
lint = False
if len(sys.argv) == 3:
if sys.argv[1] == '-i':
use_stdout = False
if sys.argv[1] == '-l':
lint = True
filename = sys.argv[len(sys.argv) - 1]
def process(filename, only_lint, use_stdout):
with open(filename, 'r') as content_file:
content = content_file.read()
original_input = content
p = Popen(['clang-format', '-assume-filename=.ts'], stdin=PIPE, stdout=PIPE, stderr=PIPE)
output, err = p.communicate(preprocess(content))
output = postprocess(output)
rc = p.returncode
if (rc <> 0):
print "error code " + str(rc) + " running clang-format. Exiting..."
sys.exit(rc);
if lint:
if only_lint:
if (output != original_input):
print >>sys.stderr, filename + ' requires formatting'
elif use_stdout:
@ -113,3 +102,35 @@ else:
output_file = open(filename, 'w')
output_file.write(output);
output_file.close()
def print_usage():
print 'format-torque -i file1[, file2[, ...]]'
print ' format and overwrite input files'
print 'format-torque -l file1[, file2[, ...]]'
print ' merely indicate which files need formatting'
def Main():
if len(sys.argv) < 3:
print "error: at least 2 arguments required"
print_usage();
sys.exit(-1)
use_stdout = True
lint = False
if sys.argv[1] == '-i':
use_stdout = False
elif sys.argv[1] == '-l':
lint = True
else:
print "error: -i or -l must be specified as the first argument"
print_usage();
sys.exit(-1);
for filename in sys.argv[2:]:
process(filename, lint, use_stdout)
return 0
if __name__ == '__main__':
sys.exit(Main());