Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 # Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 # Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| 2 # for details. All rights reserved. Use of this source code is governed by a | 2 # for details. All rights reserved. Use of this source code is governed by a |
| 3 # BSD-style license that can be found in the LICENSE file. | 3 # BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 """Top-level presubmit script for Dart. | 5 """Top-level presubmit script for Dart. |
| 6 | 6 |
| 7 See http://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts | 7 See http://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts |
| 8 for more details about the presubmit API built into gcl. | 8 for more details about the presubmit API built into gcl. |
| 9 """ | 9 """ |
| 10 | 10 |
| 11 from __future__ import print_function | |
| 12 import imp | |
| 13 import os | |
| 14 import scm | |
| 15 import subprocess | |
| 16 import tempfile | |
| 17 | |
| 18 def _CheckBuildStatus(input_api, output_api): | |
| 19 results = [] | |
| 20 status_check = input_api.canned_checks.CheckTreeIsOpen( | |
| 21 input_api, | |
| 22 json_url='http://dart-status.appspot.com/current?format=json') | |
| 23 results.extend(status_check) | |
| 24 return results | |
| 25 | |
| 26 def _CheckDartFormat(input_api, output_api): | |
| 27 local_root = input_api.change.RepositoryRoot() | |
| 28 upstream = input_api.change._upstream | |
| 29 utils = imp.load_source('utils', | |
| 30 os.path.join(local_root, 'tools', 'utils.py')) | |
| 31 | |
| 32 results = [] | |
| 33 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.
| |
| 34 | |
| 35 for git_file in input_api.AffectedTextFiles(): | |
| 36 filename = git_file.AbsoluteLocalPath() | |
| 37 if filename.endswith('.dart'): | |
| 38 cmd = '%s --set-exit-if-changed -n %s ' % (prebuilt_dartfmt, filename) | |
| 39 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.
| |
| 40 old_version_has_errors = False | |
| 41 try: | |
| 42 old_contents = scm.GIT.Capture( | |
| 43 ['show', upstream + ':' + git_file.LocalPath()], cwd=local_root) | |
| 44 fd, temp_path = tempfile.mkstemp() | |
| 45 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.
| |
| 46 # Use print to make sure file is terminated with a newline as | |
| 47 # git show is losing the trailing newline. | |
| 48 print(old_contents, file=f) | |
| 49 f.close() | |
| 50 | |
| 51 cmd_old_file = '%s --set-exit-if-changed -n %s ' % ( | |
| 52 prebuilt_dartfmt, temp_path) | |
| 53 if os.system(cmd_old_file): | |
| 54 old_version_has_errors = True | |
| 55 | |
| 56 os.close(fd) | |
| 57 except subprocess.CalledProcessError as e: | |
| 58 # TODO(jacobr): verify that the error really is that the file was | |
| 59 # added for this CL. | |
| 60 old_version_has_errors = False | |
| 61 | |
| 62 if old_version_has_errors: | |
| 63 print("WARNING: %s has existing and possibly new dartfmt issues" % | |
| 64 git_file.LocalPath()) | |
| 65 else: | |
| 66 results += [output_api.PresubmitError( | |
| 67 'Run\ndartfmt -w %s' % filename)] | |
| 68 | |
| 69 return results | |
| 70 | |
| 11 def CheckChangeOnCommit(input_api, output_api): | 71 def CheckChangeOnCommit(input_api, output_api): |
| 12 results = [] | 72 return (_CheckBuildStatus(input_api, output_api) + |
| 13 status_check = input_api.canned_checks.CheckTreeIsOpen( | 73 _CheckDartFormat(input_api, output_api)) |
| 14 input_api, | 74 |
| 15 output_api, | 75 def CheckChangeOnUpload(input_api, output_api): |
| 16 json_url='http://dart-status.appspot.com/current?format=json') | 76 return _CheckDartFormat(input_api, output_api) |
| 17 results.extend(status_check) | |
| 18 return results | |
| OLD | NEW |