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 import os |
M-A Ruel
2013/09/11 13:37:59
Not necessary
Haojian Wu
2013/09/11 14:48:57
Done.
| |
6 | |
7 | |
8 def _GetJSONParseError(input_api, filename): | |
6 try: | 9 try: |
10 contents = input_api.ReadFile(filename) | |
7 json_comment_eater = input_api.os_path.join( | 11 json_comment_eater = input_api.os_path.join( |
8 input_api.PresubmitLocalPath(), | 12 input_api.PresubmitLocalPath(), |
9 '..', '..', '..', '..', 'tools', | 13 '..', '..', '..', '..', 'tools', |
10 'json_comment_eater', 'json_comment_eater.py') | 14 'json_comment_eater', 'json_comment_eater.py') |
11 process = input_api.subprocess.Popen( | 15 process = input_api.subprocess.Popen( |
12 [input_api.python_executable, json_comment_eater], | 16 [input_api.python_executable, json_comment_eater], |
13 stdin=input_api.subprocess.PIPE, | 17 stdin=input_api.subprocess.PIPE, |
14 stdout=input_api.subprocess.PIPE) | 18 stdout=input_api.subprocess.PIPE) |
15 (nommed, _) = process.communicate(input=contents) | 19 (nommed, _) = process.communicate(input=contents) |
16 input_api.json.loads(nommed) | 20 input_api.json.loads(nommed) |
17 except ValueError as e: | 21 except ValueError as e: |
18 return e | 22 return e |
19 return None | 23 return None |
20 | 24 |
21 | 25 |
26 def _GetIDLParseError(input_api, filename): | |
27 idl_schema = input_api.os_path.join( | |
28 input_api.PresubmitLocalPath(), | |
29 '..', '..', '..', '..', 'tools', | |
30 'json_schema_compiler', 'idl_schema.py') | |
31 process = input_api.subprocess.Popen( | |
32 [input_api.python_executable, idl_schema, filename], | |
33 stdout=input_api.subprocess.PIPE, | |
34 stderr=input_api.subprocess.PIPE) | |
35 (_, error) = process.communicate() | |
36 return error or None | |
37 | |
38 | |
22 def _GetParseErrors(input_api, output_api): | 39 def _GetParseErrors(input_api, output_api): |
23 # Run unit tests. | 40 # Run unit tests. |
24 results = [] | 41 results = [] |
25 if input_api.AffectedFiles( | 42 if input_api.AffectedFiles( |
26 file_filter=lambda f: 'PRESUBMIT' in f.LocalPath()): | 43 file_filter=lambda f: 'PRESUBMIT' in f.LocalPath()): |
27 results = input_api.canned_checks.RunUnitTestsInDirectory( | 44 results = input_api.canned_checks.RunUnitTestsInDirectory( |
28 input_api, output_api, '.', whitelist=[r'^PRESUBMIT_test\.py$']) | 45 input_api, output_api, '.', whitelist=[r'^PRESUBMIT_test\.py$']) |
29 | 46 |
47 actions = { | |
48 '.json': _GetJSONParseError, | |
49 '.idl': _GetIDLParseError | |
50 } | |
51 | |
52 def get_action(affected_file): | |
53 filename = affected_file.AbsoluteLocalPath() | |
M-A Ruel
2013/09/11 13:37:59
FYI, (optional)
filename = affected_file.LocalPath
Haojian Wu
2013/09/11 14:48:57
Done.
| |
54 return actions.get(os.path.splitext(filename)[1]) | |
M-A Ruel
2013/09/11 13:37:59
input_api.os_path.splitext()
Haojian Wu
2013/09/11 14:48:57
Done.
| |
55 | |
30 for affected_file in input_api.AffectedFiles( | 56 for affected_file in input_api.AffectedFiles( |
31 file_filter=lambda f: f.LocalPath().endswith('.json'), | 57 file_filter=get_action, |
32 include_deletes=False): | 58 include_deletes=False): |
33 filename = affected_file.AbsoluteLocalPath() | 59 parse_error = get_action(affected_file)(input_api, |
34 contents = input_api.ReadFile(filename) | 60 affected_file.AbsoluteLocalPath()) |
35 parse_error = _GetJSONParseError(input_api, contents) | |
36 if parse_error: | 61 if parse_error: |
37 results.append(output_api.PresubmitError( | 62 results.append(output_api.PresubmitError('%s could not be parsed: %s' % |
38 'Features file %s could not be parsed: %s' % | 63 (affected_file.LocalPath(), parse_error))) |
M-A Ruel
2013/09/11 13:37:59
weird alignment
Haojian Wu
2013/09/11 14:48:57
Done.
| |
39 (affected_file.LocalPath(), parse_error))) | |
40 # TODO(yoz): Also ensure IDL files are parseable. | |
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 |