Commit bb909b5a authored by Tamer Tas's avatar Tamer Tas Committed by Commit Bot

[tools] auto-format unformatted Torque source files

Presubmit script complains when an unformatted Torque file is submitted.

This CL automates the formatting process of the Torque files.

Presubmit script is run before every 'git cl upload', the workflow will make
sure that the upload is canceled, but the files are formatted.

Bug: chromium:898436, v8:8805
Change-Id: I821ce36907c62e222451e883c5e3e18a9359f20e
No-Try: true
Reviewed-on: https://chromium-review.googlesource.com/c/1458222Reviewed-by: 's avatarDaniel Clifford <danno@chromium.org>
Reviewed-by: 's avatarSergiy Belozorov <sergiyb@chromium.org>
Commit-Queue: Tamer Tas <tmrts@chromium.org>
Cr-Commit-Position: refs/heads/master@{#59453}
parent c6d21691
...@@ -86,7 +86,7 @@ def postprocess(output): ...@@ -86,7 +86,7 @@ def postprocess(output):
return output return output
def process(filename, only_lint, use_stdout): def process(filename, lint, should_format):
with open(filename, 'r') as content_file: with open(filename, 'r') as content_file:
content = content_file.read() content = content_file.read()
...@@ -100,12 +100,11 @@ def process(filename, only_lint, use_stdout): ...@@ -100,12 +100,11 @@ def process(filename, only_lint, use_stdout):
print "error code " + str(rc) + " running clang-format. Exiting..." print "error code " + str(rc) + " running clang-format. Exiting..."
sys.exit(rc); sys.exit(rc);
if only_lint: if lint:
if (output != original_input): if (output != original_input):
print >>sys.stderr, filename + ' requires formatting' print >>sys.stderr, filename + ' requires formatting'
elif use_stdout:
print output if should_format:
else:
output_file = open(filename, 'w') output_file = open(filename, 'w')
output_file.write(output); output_file.write(output);
output_file.close() output_file.close()
...@@ -122,20 +121,28 @@ def Main(): ...@@ -122,20 +121,28 @@ def Main():
print_usage(); print_usage();
sys.exit(-1) sys.exit(-1)
def is_option(arg):
return arg in ['-i', '-l', '-il']
should_format = lint = False
use_stdout = True use_stdout = True
lint = False
if sys.argv[1] == '-i': flag, files = sys.argv[1], sys.argv[2:]
use_stdout = False if is_option(flag):
elif sys.argv[1] == '-l': if '-i' == flag:
lint = True should_format = True
elif '-l' == flag:
lint = True
else:
lint = True
should_format = True
else: else:
print "error: -i or -l must be specified as the first argument" print "error: -i and/or -l flags must be specified"
print_usage(); print_usage();
sys.exit(-1); sys.exit(-1);
for filename in sys.argv[2:]: for filename in files:
process(filename, lint, use_stdout) process(filename, lint, should_format)
return 0 return 0
......
...@@ -114,7 +114,8 @@ def TorqueLintWorker(command): ...@@ -114,7 +114,8 @@ def TorqueLintWorker(command):
error_count += 1 error_count += 1
sys.stdout.write(out_lines) sys.stdout.write(out_lines)
if error_count != 0: if error_count != 0:
sys.stdout.write("tip: use 'tools/torque/format-torque.py -i <filename>'\n"); sys.stdout.write(
"warning: formatting and overwriting unformatted Torque files\n")
return error_count return error_count
except KeyboardInterrupt: except KeyboardInterrupt:
process.kill() process.kill()
...@@ -371,7 +372,7 @@ class TorqueLintProcessor(CacheableSourceFileProcessor): ...@@ -371,7 +372,7 @@ class TorqueLintProcessor(CacheableSourceFileProcessor):
def GetProcessorScript(self): def GetProcessorScript(self):
torque_tools = os.path.join(TOOLS_PATH, "torque") torque_tools = os.path.join(TOOLS_PATH, "torque")
torque_path = os.path.join(torque_tools, "format-torque.py") torque_path = os.path.join(torque_tools, "format-torque.py")
arguments = ['-l'] arguments = ["-il"]
if os.path.isfile(torque_path): if os.path.isfile(torque_path):
return torque_path, arguments return torque_path, arguments
......
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