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