OLD | NEW |
| (Empty) |
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 | |
3 # found in the LICENSE file. | |
4 | |
5 def _GetJSONParseError(input_api, filename): | |
6 try: | |
7 contents = input_api.ReadFile(filename) | |
8 json_comment_eater = input_api.os_path.join( | |
9 input_api.PresubmitLocalPath(), | |
10 '..', '..', '..', '..', 'tools', | |
11 'json_comment_eater', 'json_comment_eater.py') | |
12 process = input_api.subprocess.Popen( | |
13 [input_api.python_executable, json_comment_eater], | |
14 stdin=input_api.subprocess.PIPE, | |
15 stdout=input_api.subprocess.PIPE, | |
16 universal_newlines=True) | |
17 (nommed, _) = process.communicate(input=contents) | |
18 input_api.json.loads(nommed) | |
19 except ValueError as e: | |
20 return e | |
21 return None | |
22 | |
23 | |
24 def _GetIDLParseError(input_api, filename): | |
25 idl_schema = input_api.os_path.join( | |
26 input_api.PresubmitLocalPath(), | |
27 '..', '..', '..', '..', 'tools', | |
28 'json_schema_compiler', 'idl_schema.py') | |
29 process = input_api.subprocess.Popen( | |
30 [input_api.python_executable, idl_schema, filename], | |
31 stdout=input_api.subprocess.PIPE, | |
32 stderr=input_api.subprocess.PIPE, | |
33 universal_newlines=True) | |
34 (_, error) = process.communicate() | |
35 return error or None | |
36 | |
37 | |
38 def _GetParseErrors(input_api, output_api): | |
39 # Run unit tests. | |
40 results = [] | |
41 if input_api.AffectedFiles( | |
42 file_filter=lambda f: 'PRESUBMIT' in f.LocalPath()): | |
43 results = input_api.canned_checks.RunUnitTestsInDirectory( | |
44 input_api, output_api, '.', whitelist=[r'^PRESUBMIT_test\.py$']) | |
45 | |
46 actions = { | |
47 '.idl': _GetIDLParseError, | |
48 '.json': _GetJSONParseError, | |
49 } | |
50 | |
51 def get_action(affected_file): | |
52 filename = affected_file.LocalPath() | |
53 return actions.get(input_api.os_path.splitext(filename)[1]) | |
54 | |
55 for affected_file in input_api.AffectedFiles( | |
56 file_filter= | |
57 lambda f: "test_presubmit" not in f.LocalPath() and get_action(f), | |
58 include_deletes=False): | |
59 parse_error = get_action(affected_file)(input_api, | |
60 affected_file.AbsoluteLocalPath()) | |
61 if parse_error: | |
62 results.append(output_api.PresubmitError('%s could not be parsed: %s' % | |
63 (affected_file.LocalPath(), parse_error))) | |
64 return results | |
65 | |
66 | |
67 def CheckChangeOnUpload(input_api, output_api): | |
68 return _GetParseErrors(input_api, output_api) | |
69 | |
70 | |
71 def CheckChangeOnCommit(input_api, output_api): | |
72 return _GetParseErrors(input_api, output_api) | |
OLD | NEW |