[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.

TBR=danno@chromium.org

Bug: v8:7793
Change-Id: Ifba85c4db553e19a65b87217fd2f670698c6b2c9
Reviewed-on: https://chromium-review.googlesource.com/c/1333679
Reviewed-by: Michael Stanton <mvstanton@chromium.org>
Commit-Queue: Michael Stanton <mvstanton@chromium.org>
Cr-Commit-Position: refs/heads/master@{#57482}
This commit is contained in:
Mike Stanton 2018-11-13 17:18:03 +01:00 committed by Commit Bot
parent cdf750a8f4
commit 0ff69e7e93

View File

@ -81,35 +81,58 @@ def postprocess(output):
return output
if len(sys.argv) < 2 or len(sys.argv) > 3:
print "invalid number of arguments"
sys.exit(-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 only_lint:
if (output != original_input):
print >>sys.stderr, filename + ' requires formatting'
elif use_stdout:
print output
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
use_stdout = True
lint = False
if len(sys.argv) == 3:
if sys.argv[1] == '-i':
use_stdout = False
if sys.argv[1] == '-l':
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);
filename = sys.argv[len(sys.argv) - 1]
for filename in sys.argv[2:]:
process(filename, 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):
sys.exit(rc);
if lint:
if (output != original_input):
print >>sys.stderr, filename + ' requires formatting'
elif use_stdout:
print output
else:
output_file = open(filename, 'w')
output_file.write(output);
output_file.close()
return 0
if __name__ == '__main__':
sys.exit(Main());