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 prebuilt_dartfmt = os.path.join(utils.CheckedInSdkPath(), 'bin', 'dartfmt') | |
33 | |
34 if not os.path.isfile(prebuilt_dartfmt): | |
35 print('WARNING: dartfmt not found: %s' % (prebuilt_dartfmt)) | |
36 return [] | |
37 | |
38 def HasFormatErrors(filename): | |
39 try: | |
40 subprocess.check_output( | |
41 [prebuilt_dartfmt, '--set-exit-if-changed', '-n', filename]) | |
42 except subprocess.CalledProcessError: | |
43 return True | |
44 return False | |
45 | |
46 unformatted_files = [] | |
47 for git_file in input_api.AffectedTextFiles(): | |
48 filename = git_file.AbsoluteLocalPath() | |
49 if filename.endswith('.dart'): | |
50 if HasFormatErrors(filename): | |
51 old_version_has_errors = False | |
52 try: | |
53 old_contents = scm.GIT.Capture( | |
54 ['show', upstream + ':' + git_file.LocalPath()], cwd=local_root) | |
55 fd, temp_path = tempfile.mkstemp() | |
56 # Use print to make sure file is terminated with a newline as | |
57 # "git show" is losing the trailing newline. | |
58 with open(temp_path, 'w') as f: | |
59 print(old_contents, file=f) | |
60 | |
61 if HasFormatErrors(temp_path): | |
62 old_version_has_errors = True | |
63 | |
64 os.close(fd) | |
zra
2017/03/21 20:47:49
It might also be a good idea to manually delete th
Jacob
2017/03/21 22:04:21
Worked around a bug in how the formatter handles r
| |
65 except subprocess.CalledProcessError as e: | |
zra
2017/03/21 20:47:49
Can this still happen? It looks like HasFormatErro
Jacob
2017/03/21 22:04:22
scm.GIT.Capture is throwing this error.
| |
66 # TODO(jacobr): verify that the error really is that the file was | |
67 # added for this CL. | |
68 old_version_has_errors = False | |
69 | |
70 if old_version_has_errors: | |
71 print("WARNING: %s has existing and possibly new dartfmt issues" % | |
72 git_file.LocalPath()) | |
73 else: | |
74 unformatted_files.append(filename) | |
75 | |
76 if unformatted_files: | |
77 return [output_api.PresubmitError( | |
78 'File output does not match dartfmt.\n' | |
79 'Fix these issues with:\n' | |
80 'dartfmt -w \\\n%s' % ' \\\n'.join(unformatted_files))] | |
81 | |
82 return [] | |
83 | |
11 def CheckChangeOnCommit(input_api, output_api): | 84 def CheckChangeOnCommit(input_api, output_api): |
12 results = [] | 85 return (_CheckBuildStatus(input_api, output_api) + |
13 status_check = input_api.canned_checks.CheckTreeIsOpen( | 86 _CheckDartFormat(input_api, output_api)) |
14 input_api, | 87 |
15 output_api, | 88 def CheckChangeOnUpload(input_api, output_api): |
16 json_url='http://dart-status.appspot.com/current?format=json') | 89 return _CheckDartFormat(input_api, output_api) |
17 results.extend(status_check) | |
18 return results | |
OLD | NEW |