OLD | NEW |
---|---|
1 # Copyright 2013 The Chromium Authors. All rights reserved. | 1 # Copyright 2013 The Chromium Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 def _GetJSONParseError(input_api, contents): | 5 def _GetJSONParseError(input_api, contents): |
6 try: | 6 try: |
7 json_comment_eater = input_api.os_path.join( | 7 json_comment_eater = input_api.os_path.join( |
8 input_api.PresubmitLocalPath(), | 8 input_api.PresubmitLocalPath(), |
9 '..', '..', '..', '..', 'tools', | 9 '..', '..', '..', '..', 'tools', |
10 'json_comment_eater', 'json_comment_eater.py') | 10 'json_comment_eater', 'json_comment_eater.py') |
11 process = input_api.subprocess.Popen( | 11 process = input_api.subprocess.Popen( |
12 [input_api.python_executable, json_comment_eater], | 12 [input_api.python_executable, json_comment_eater], |
13 stdin=input_api.subprocess.PIPE, | 13 stdin=input_api.subprocess.PIPE, |
14 stdout=input_api.subprocess.PIPE) | 14 stdout=input_api.subprocess.PIPE) |
15 (nommed, _) = process.communicate(input=contents) | 15 (nommed, _) = process.communicate(input=contents) |
16 input_api.json.loads(nommed) | 16 input_api.json.loads(nommed) |
17 except ValueError as e: | 17 except ValueError as e: |
18 return e | 18 return e |
19 return None | 19 return None |
20 | 20 |
21 | 21 |
22 def _GetIDLParseError(input_api, filename): | |
23 idl_schema = input_api.os_path.join( | |
24 input_api.PresubmitLocalPath(), | |
25 '..', '..', '..', '..', 'tools', | |
26 'json_schema_compiler', 'idl_schema.py') | |
27 process = input_api.subprocess.Popen( | |
28 [input_api.python_executable, idl_schema, filename], | |
29 stdout=input_api.subprocess.PIPE, | |
30 stderr=input_api.subprocess.PIPE) | |
31 (_, error) = process.communicate() | |
32 if error: | |
33 return error | |
34 return None | |
35 | |
36 | |
22 def _GetParseErrors(input_api, output_api): | 37 def _GetParseErrors(input_api, output_api): |
23 # Run unit tests. | 38 # Run unit tests. |
24 results = [] | 39 results = [] |
25 if input_api.AffectedFiles( | 40 if input_api.AffectedFiles( |
26 file_filter=lambda f: 'PRESUBMIT' in f.LocalPath()): | 41 file_filter=lambda f: 'PRESUBMIT' in f.LocalPath()): |
27 results = input_api.canned_checks.RunUnitTestsInDirectory( | 42 results = input_api.canned_checks.RunUnitTestsInDirectory( |
28 input_api, output_api, '.', whitelist=[r'^PRESUBMIT_test\.py$']) | 43 input_api, output_api, '.', whitelist=[r'^PRESUBMIT_test\.py$']) |
29 | 44 |
30 for affected_file in input_api.AffectedFiles( | 45 for affected_file in input_api.AffectedFiles( |
31 file_filter=lambda f: f.LocalPath().endswith('.json'), | 46 file_filter=lambda f: f.LocalPath().endswith('.json'), |
32 include_deletes=False): | 47 include_deletes=False): |
33 filename = affected_file.AbsoluteLocalPath() | 48 filename = affected_file.AbsoluteLocalPath() |
34 contents = input_api.ReadFile(filename) | 49 contents = input_api.ReadFile(filename) |
35 parse_error = _GetJSONParseError(input_api, contents) | 50 parse_error = _GetJSONParseError(input_api, contents) |
36 if parse_error: | 51 if parse_error: |
37 results.append(output_api.PresubmitError( | 52 results.append(output_api.PresubmitError( |
38 'Features file %s could not be parsed: %s' % | 53 'Features file %s could not be parsed: %s' % |
39 (affected_file.LocalPath(), parse_error))) | 54 (affected_file.LocalPath(), parse_error))) |
40 # TODO(yoz): Also ensure IDL files are parseable. | 55 |
56 for affected_file in input_api.AffectedFiles( | |
57 file_filter=lambda f: f.LocalPath().endswith('.idl')): | |
58 filename = affected_file.AbsoluteLocalPath() | |
not at google - send to devlin
2013/09/09 16:14:07
too much indent
Haojian Wu
2013/09/10 02:51:04
Done.
| |
59 parse_error = _GetIDLParseError(input_api, filename) | |
60 if parse_error: | |
61 results.append(output_api.PresubmitError( | |
62 'Features file %s could not be parsed: %s' % | |
not at google - send to devlin
2013/09/09 16:14:07
s/Features/IDL/
Haojian Wu
2013/09/10 02:51:04
Done.
| |
63 (affected_file.LocalPath(), parse_error))) | |
not at google - send to devlin
2013/09/09 16:14:07
You could factor this more concisely:
actions = {
Haojian Wu
2013/09/10 02:51:04
Done. Thanks for the detailed instructions!
| |
41 return results | 64 return results |
42 | 65 |
43 | 66 |
44 def CheckChangeOnUpload(input_api, output_api): | 67 def CheckChangeOnUpload(input_api, output_api): |
45 return _GetParseErrors(input_api, output_api) | 68 return _GetParseErrors(input_api, output_api) |
46 | 69 |
47 | 70 |
48 def CheckChangeOnCommit(input_api, output_api): | 71 def CheckChangeOnCommit(input_api, output_api): |
49 return _GetParseErrors(input_api, output_api) | 72 return _GetParseErrors(input_api, output_api) |
OLD | NEW |