Chromium Code Reviews| Index: PRESUBMIT.py |
| diff --git a/PRESUBMIT.py b/PRESUBMIT.py |
| index f850b03948c9b2e1a03a9960ef8f54a3bc2d9cb2..ea7b1e0c8cf916f5f3841ac016ca8724dfeb2ee7 100644 |
| --- a/PRESUBMIT.py |
| +++ b/PRESUBMIT.py |
| @@ -8,11 +8,69 @@ See http://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts |
| for more details about the presubmit API built into gcl. |
| """ |
| -def CheckChangeOnCommit(input_api, output_api): |
| +from __future__ import print_function |
| +import imp |
| +import os |
| +import scm |
| +import subprocess |
| +import tempfile |
| + |
| +def _CheckBuildStatus(input_api, output_api): |
| + results = [] |
| + status_check = input_api.canned_checks.CheckTreeIsOpen( |
| + input_api, |
| + json_url='http://dart-status.appspot.com/current?format=json') |
| + results.extend(status_check) |
| + return results |
| + |
| +def _CheckDartFormat(input_api, output_api): |
| + local_root = input_api.change.RepositoryRoot() |
| + upstream = input_api.change._upstream |
| + utils = imp.load_source('utils', |
| + os.path.join(local_root, 'tools', 'utils.py')) |
| + |
| results = [] |
| - status_check = input_api.canned_checks.CheckTreeIsOpen( |
| - input_api, |
| - output_api, |
| - json_url='http://dart-status.appspot.com/current?format=json') |
| - results.extend(status_check) |
| + prebuilt_dartfmt = os.path.join(utils.CheckedInSdkPath(), 'bin', 'dartfmt') |
|
zra
2017/03/21 19:51:17
Please add back the check that bails out if this d
Jacob
2017/03/21 20:34:24
Done.
|
| + |
| + for git_file in input_api.AffectedTextFiles(): |
| + filename = git_file.AbsoluteLocalPath() |
| + if filename.endswith('.dart'): |
| + cmd = '%s --set-exit-if-changed -n %s ' % (prebuilt_dartfmt, filename) |
| + if os.system(cmd): |
|
zra
2017/03/21 19:51:17
I think subprocess tends to be preferred over os.s
Jacob
2017/03/21 20:34:24
Done.
|
| + old_version_has_errors = False |
| + try: |
| + old_contents = scm.GIT.Capture( |
| + ['show', upstream + ':' + git_file.LocalPath()], cwd=local_root) |
| + fd, temp_path = tempfile.mkstemp() |
| + f = open(temp_path, 'w') |
|
zra
2017/03/21 19:51:17
with open(temp_path, 'w') as f:
print(old_conten
Jacob
2017/03/21 20:34:24
Done.
|
| + # Use print to make sure file is terminated with a newline as |
| + # git show is losing the trailing newline. |
| + print(old_contents, file=f) |
| + f.close() |
| + |
| + cmd_old_file = '%s --set-exit-if-changed -n %s ' % ( |
| + prebuilt_dartfmt, temp_path) |
| + if os.system(cmd_old_file): |
| + old_version_has_errors = True |
| + |
| + os.close(fd) |
| + except subprocess.CalledProcessError as e: |
| + # TODO(jacobr): verify that the error really is that the file was |
| + # added for this CL. |
| + old_version_has_errors = False |
| + |
| + if old_version_has_errors: |
| + print("WARNING: %s has existing and possibly new dartfmt issues" % |
| + git_file.LocalPath()) |
| + else: |
| + results += [output_api.PresubmitError( |
| + 'Run\ndartfmt -w %s' % filename)] |
| + |
| return results |
| + |
| +def CheckChangeOnCommit(input_api, output_api): |
| + return (_CheckBuildStatus(input_api, output_api) + |
| + _CheckDartFormat(input_api, output_api)) |
| + |
| +def CheckChangeOnUpload(input_api, output_api): |
| + return _CheckDartFormat(input_api, output_api) |