Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(862)

Side by Side Diff: PRESUBMIT.py

Issue 239283008: Add global presubmit that JSON and IDL files can be parsed. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebase Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | PRESUBMIT_test.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 # Copyright (c) 2012 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 """Top-level presubmit script for Chromium. 5 """Top-level presubmit script for Chromium.
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
(...skipping 1045 matching lines...) Expand 10 before | Expand all | Expand 10 after
1056 action = 'name="{0}"'.format(action_name) 1056 action = 'name="{0}"'.format(action_name)
1057 if action not in current_actions: 1057 if action not in current_actions:
1058 return [output_api.PresubmitPromptWarning( 1058 return [output_api.PresubmitPromptWarning(
1059 'File %s line %d: %s is missing in ' 1059 'File %s line %d: %s is missing in '
1060 'tools/metrics/actions/actions.xml. Please run ' 1060 'tools/metrics/actions/actions.xml. Please run '
1061 'tools/metrics/actions/extract_actions.py to update.' 1061 'tools/metrics/actions/extract_actions.py to update.'
1062 % (f.LocalPath(), line_num, action_name))] 1062 % (f.LocalPath(), line_num, action_name))]
1063 return [] 1063 return []
1064 1064
1065 1065
1066 def _GetJSONParseError(input_api, filename, eat_comments=True):
1067 try:
1068 contents = input_api.ReadFile(filename)
1069 if eat_comments:
1070 json_comment_eater = input_api.os_path.join(
1071 input_api.PresubmitLocalPath(),
1072 'tools', 'json_comment_eater', 'json_comment_eater.py')
1073 process = input_api.subprocess.Popen(
1074 [input_api.python_executable, json_comment_eater],
1075 stdin=input_api.subprocess.PIPE,
1076 stdout=input_api.subprocess.PIPE,
1077 universal_newlines=True)
1078 (contents, _) = process.communicate(input=contents)
1079
1080 input_api.json.loads(contents)
1081 except ValueError as e:
1082 return e
1083 return None
1084
1085
1086 def _GetIDLParseError(input_api, filename):
1087 try:
1088 contents = input_api.ReadFile(filename)
1089 idl_schema = input_api.os_path.join(
1090 input_api.PresubmitLocalPath(),
1091 'tools', 'json_schema_compiler', 'idl_schema.py')
1092 process = input_api.subprocess.Popen(
1093 [input_api.python_executable, idl_schema],
1094 stdin=input_api.subprocess.PIPE,
1095 stdout=input_api.subprocess.PIPE,
1096 stderr=input_api.subprocess.PIPE,
1097 universal_newlines=True)
1098 (_, error) = process.communicate(input=contents)
1099 return error or None
1100 except ValueError as e:
1101 return e
1102
1103
1104 def _CheckParseErrors(input_api, output_api):
1105 """Check that IDL and JSON files do not contain syntax errors."""
1106 actions = {
1107 '.idl': _GetIDLParseError,
1108 '.json': _GetJSONParseError,
1109 }
1110 # These paths contain test data and other known invalid JSON files.
1111 excluded_patterns = [
1112 'test/data/',
1113 '^components/policy/resources/policy_templates.json$',
1114 ]
1115 # Most JSON files are preprocessed and support comments, but these do not.
1116 json_no_comments_patterns = [
1117 '^testing/',
1118 ]
1119 # Only run IDL checker on files in these directories.
1120 idl_included_patterns = [
1121 '^chrome/common/extensions/api/',
1122 '^extensions/common/api/',
1123 ]
1124
1125 def get_action(affected_file):
1126 filename = affected_file.LocalPath()
1127 return actions.get(input_api.os_path.splitext(filename)[1])
1128
1129 def MatchesFile(patterns, path):
1130 for pattern in patterns:
1131 if input_api.re.search(pattern, path):
1132 return True
1133 return False
1134
1135 def FilterFile(affected_file):
1136 action = get_action(affected_file)
1137 if not action:
1138 return False
1139 path = affected_file.LocalPath()
1140
1141 if MatchesFile(excluded_patterns, path):
1142 return False
1143
1144 if (action == _GetIDLParseError and
1145 not MatchesFile(idl_included_patterns, path)):
1146 return False
1147 return True
1148
1149 results = []
1150 for affected_file in input_api.AffectedFiles(
1151 file_filter=FilterFile, include_deletes=False):
1152 action = get_action(affected_file)
1153 kwargs = {}
1154 if (action == _GetJSONParseError and
1155 MatchesFile(json_no_comments_patterns, affected_file.LocalPath())):
1156 kwargs['eat_comments'] = False
1157 parse_error = action(input_api,
1158 affected_file.AbsoluteLocalPath(),
1159 **kwargs)
1160 if parse_error:
1161 results.append(output_api.PresubmitError('%s could not be parsed: %s' %
1162 (affected_file.LocalPath(), parse_error)))
1163 return results
1164
1165
1066 def _CheckJavaStyle(input_api, output_api): 1166 def _CheckJavaStyle(input_api, output_api):
1067 """Runs checkstyle on changed java files and returns errors if any exist.""" 1167 """Runs checkstyle on changed java files and returns errors if any exist."""
1068 original_sys_path = sys.path 1168 original_sys_path = sys.path
1069 try: 1169 try:
1070 sys.path = sys.path + [input_api.os_path.join( 1170 sys.path = sys.path + [input_api.os_path.join(
1071 input_api.PresubmitLocalPath(), 'tools', 'android', 'checkstyle')] 1171 input_api.PresubmitLocalPath(), 'tools', 'android', 'checkstyle')]
1072 import checkstyle 1172 import checkstyle
1073 finally: 1173 finally:
1074 # Restore sys.path to what it was before. 1174 # Restore sys.path to what it was before.
1075 sys.path = original_sys_path 1175 sys.path = original_sys_path
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
1155 results.extend( 1255 results.extend(
1156 input_api.canned_checks.CheckChangeHasNoTabs( 1256 input_api.canned_checks.CheckChangeHasNoTabs(
1157 input_api, 1257 input_api,
1158 output_api, 1258 output_api,
1159 source_file_filter=lambda x: x.LocalPath().endswith('.grd'))) 1259 source_file_filter=lambda x: x.LocalPath().endswith('.grd')))
1160 results.extend(_CheckSpamLogging(input_api, output_api)) 1260 results.extend(_CheckSpamLogging(input_api, output_api))
1161 results.extend(_CheckForAnonymousVariables(input_api, output_api)) 1261 results.extend(_CheckForAnonymousVariables(input_api, output_api))
1162 results.extend(_CheckCygwinShell(input_api, output_api)) 1262 results.extend(_CheckCygwinShell(input_api, output_api))
1163 results.extend(_CheckUserActionUpdate(input_api, output_api)) 1263 results.extend(_CheckUserActionUpdate(input_api, output_api))
1164 results.extend(_CheckNoDeprecatedCSS(input_api, output_api)) 1264 results.extend(_CheckNoDeprecatedCSS(input_api, output_api))
1265 results.extend(_CheckParseErrors(input_api, output_api))
1165 1266
1166 if any('PRESUBMIT.py' == f.LocalPath() for f in input_api.AffectedFiles()): 1267 if any('PRESUBMIT.py' == f.LocalPath() for f in input_api.AffectedFiles()):
1167 results.extend(input_api.canned_checks.RunUnitTestsInDirectory( 1268 results.extend(input_api.canned_checks.RunUnitTestsInDirectory(
1168 input_api, output_api, 1269 input_api, output_api,
1169 input_api.PresubmitLocalPath(), 1270 input_api.PresubmitLocalPath(),
1170 whitelist=[r'^PRESUBMIT_test\.py$'])) 1271 whitelist=[r'^PRESUBMIT_test\.py$']))
1171 return results 1272 return results
1172 1273
1173 1274
1174 def _CheckSubversionConfig(input_api, output_api): 1275 def _CheckSubversionConfig(input_api, output_api):
(...skipping 379 matching lines...) Expand 10 before | Expand all | Expand 10 after
1554 builders.extend(['cros_x86']) 1655 builders.extend(['cros_x86'])
1555 1656
1556 # The AOSP bot doesn't build the chrome/ layer, so ignore any changes to it 1657 # The AOSP bot doesn't build the chrome/ layer, so ignore any changes to it
1557 # unless they're .gyp(i) files as changes to those files can break the gyp 1658 # unless they're .gyp(i) files as changes to those files can break the gyp
1558 # step on that bot. 1659 # step on that bot.
1559 if (not all(re.search('^chrome', f) for f in files) or 1660 if (not all(re.search('^chrome', f) for f in files) or
1560 any(re.search('\.gypi?$', f) for f in files)): 1661 any(re.search('\.gypi?$', f) for f in files)):
1561 builders.extend(['android_aosp']) 1662 builders.extend(['android_aosp'])
1562 1663
1563 return GetDefaultTryConfigs(builders) 1664 return GetDefaultTryConfigs(builders)
OLDNEW
« no previous file with comments | « no previous file | PRESUBMIT_test.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698