Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 # Copyright 2014 The Chromium Authors. All rights reserved. | 1 # Copyright 2014 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 """Presubmit script for mojo | 5 """Presubmit script for mojo |
| 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 depot_tools. | 8 for more details about the presubmit API built into depot_tools. |
| 9 """ | 9 """ |
| 10 | 10 |
| 11 import os.path | 11 import os.path |
| 12 import re | 12 import re |
| 13 | 13 |
| 14 _SDK_WHITELISTED_EXTERNAL_PATHS = [ | 14 # NOTE: The EDK allows all external paths, so doesn't need a whitelist. |
| 15 "//testing/gtest", | 15 _PACKAGE_WHITELISTED_EXTERNAL_PATHS = { |
| 16 "//third_party/cython", | 16 "SDK": [ "//testing/gtest", |
|
qsr
2015/01/28 14:57:53
You should indent by 4.
blundell
2015/01/28 15:23:02
Actually, aligned with opening delimiter is accept
| |
| 17 "//third_party/khronos", | 17 "//third_party/cython", |
| 18 ] | 18 "//third_party/khronos"], |
| 19 "services": [ "//testing/gtest" ] | |
| 20 } | |
| 21 | |
| 19 | 22 |
| 20 _PACKAGE_PATH_PREFIXES = {"SDK": "mojo/public/", | 23 _PACKAGE_PATH_PREFIXES = {"SDK": "mojo/public/", |
| 21 "EDK": "mojo/edk/"} | 24 "EDK": "mojo/edk/", |
| 25 "services": "mojo/services"} | |
| 22 | 26 |
| 23 # TODO(etiennej): python_binary_source_set added due to crbug.com/443147 | 27 # TODO(etiennej): python_binary_source_set added due to crbug.com/443147 |
| 24 _PACKAGE_SOURCE_SET_TYPES = {"SDK": ["mojo_sdk_source_set", | 28 _PACKAGE_SOURCE_SET_TYPES = {"SDK": ["mojo_sdk_source_set", |
| 25 "python_binary_source_set"], | 29 "python_binary_source_set"], |
| 26 "EDK": ["mojo_edk_source_set"]} | 30 "EDK": ["mojo_edk_source_set"], |
| 31 "services": ["mojo_sdk_source_set"]} | |
| 27 | 32 |
| 28 _ILLEGAL_EXTERNAL_PATH_WARNING_MESSAGE = \ | 33 _ILLEGAL_EXTERNAL_PATH_WARNING_MESSAGE = \ |
| 29 "Found disallowed external paths within SDK buildfiles." | 34 "Found disallowed external paths within SDK buildfiles." |
| 30 | 35 |
| 36 _ILLEGAL_SERVICES_ABSOLUTE_PATH_WARNING_MESSAGE = \ | |
| 37 "Found references to services' public buildfiles via absolute paths " \ | |
| 38 "within services' public buildfiles.", | |
| 39 | |
| 31 _ILLEGAL_EDK_ABSOLUTE_PATH_WARNING_MESSAGE = \ | 40 _ILLEGAL_EDK_ABSOLUTE_PATH_WARNING_MESSAGE = \ |
| 32 "Found references to the EDK via absolute paths within EDK buildfiles.", | 41 "Found references to the EDK via absolute paths within EDK buildfiles.", |
| 33 | 42 |
| 34 _ILLEGAL_SDK_ABSOLUTE_PATH_WARNING_MESSAGE_TEMPLATE = \ | 43 _ILLEGAL_SDK_ABSOLUTE_PATH_WARNING_MESSAGE_TEMPLATE = \ |
| 35 "Found references to the SDK via absolute paths within %s buildfiles." | 44 "Found references to the SDK via absolute paths within %s buildfiles." |
| 36 | 45 |
| 37 _ILLEGAL_SDK_ABSOLUTE_PATH_WARNING_MESSAGES = { | 46 _ILLEGAL_SDK_ABSOLUTE_PATH_WARNING_MESSAGES = { |
| 38 "SDK": _ILLEGAL_SDK_ABSOLUTE_PATH_WARNING_MESSAGE_TEMPLATE % "SDK", | 47 "SDK": _ILLEGAL_SDK_ABSOLUTE_PATH_WARNING_MESSAGE_TEMPLATE % "SDK", |
| 39 "EDK": _ILLEGAL_SDK_ABSOLUTE_PATH_WARNING_MESSAGE_TEMPLATE % "EDK", | 48 "EDK": _ILLEGAL_SDK_ABSOLUTE_PATH_WARNING_MESSAGE_TEMPLATE % "EDK", |
| 49 "services": _ILLEGAL_SDK_ABSOLUTE_PATH_WARNING_MESSAGE_TEMPLATE | |
| 50 % "services' public", | |
| 40 } | 51 } |
| 41 | 52 |
| 42 _INCORRECT_SOURCE_SET_TYPE_WARNING_MESSAGE_TEMPLATE = \ | 53 _INCORRECT_SOURCE_SET_TYPE_WARNING_MESSAGE_TEMPLATE = \ |
| 43 "All source sets in the %s must be constructed via %s." | 54 "All source sets in %s must be constructed via %s." |
| 44 | 55 |
| 45 _INCORRECT_SOURCE_SET_TYPE_WARNING_MESSAGES = { | 56 _INCORRECT_SOURCE_SET_TYPE_WARNING_MESSAGES = { |
| 46 "SDK": _INCORRECT_SOURCE_SET_TYPE_WARNING_MESSAGE_TEMPLATE | 57 "SDK": _INCORRECT_SOURCE_SET_TYPE_WARNING_MESSAGE_TEMPLATE |
| 47 % ("SDK", _PACKAGE_SOURCE_SET_TYPES["SDK"]), | 58 % ("the SDK", _PACKAGE_SOURCE_SET_TYPES["SDK"]), |
| 48 "EDK": _INCORRECT_SOURCE_SET_TYPE_WARNING_MESSAGE_TEMPLATE | 59 "EDK": _INCORRECT_SOURCE_SET_TYPE_WARNING_MESSAGE_TEMPLATE |
| 49 % ("EDK", _PACKAGE_SOURCE_SET_TYPES["EDK"]), | 60 % ("the EDK", _PACKAGE_SOURCE_SET_TYPES["EDK"]), |
| 61 "services": _INCORRECT_SOURCE_SET_TYPE_WARNING_MESSAGE_TEMPLATE | |
| 62 % ("services' client libs", _PACKAGE_SOURCE_SET_TYPES["services"]), | |
| 50 } | 63 } |
| 51 | 64 |
| 52 def _IsBuildFileWithinPackage(f, package): | 65 def _IsBuildFileWithinPackage(f, package): |
| 53 """Returns whether |f| specifies a GN build file within |package| ("SDK" or | 66 """Returns whether |f| specifies a GN build file within |package|.""" |
| 54 "EDK").""" | |
| 55 assert package in _PACKAGE_PATH_PREFIXES | 67 assert package in _PACKAGE_PATH_PREFIXES |
| 56 package_path_prefix = _PACKAGE_PATH_PREFIXES[package] | 68 package_path_prefix = _PACKAGE_PATH_PREFIXES[package] |
| 57 | 69 |
| 58 if not f.LocalPath().startswith(package_path_prefix): | 70 if not f.LocalPath().startswith(package_path_prefix): |
| 59 return False | 71 return False |
| 60 if (not f.LocalPath().endswith("/BUILD.gn") and | 72 if (not f.LocalPath().endswith("/BUILD.gn") and |
| 61 not f.LocalPath().endswith(".gni")): | 73 not f.LocalPath().endswith(".gni")): |
| 62 return False | 74 return False |
| 63 return True | 75 return True |
| 64 | 76 |
| 65 def _AffectedBuildFilesWithinPackage(input_api, package): | 77 def _AffectedBuildFilesWithinPackage(input_api, package): |
| 66 """Returns all the affected build files within |package| ("SDK" or "EDK").""" | 78 """Returns all the affected build files within |package|.""" |
| 67 return [f for f in input_api.AffectedFiles() | 79 return [f for f in input_api.AffectedFiles() |
| 68 if _IsBuildFileWithinPackage(f, package)] | 80 if _IsBuildFileWithinPackage(f, package)] |
| 69 | 81 |
| 70 def _FindIllegalAbsolutePathsInBuildFiles(input_api, package): | 82 def _FindIllegalAbsolutePathsInBuildFiles(input_api, package): |
| 71 """Finds illegal absolute paths within the build files in | 83 """Finds illegal absolute paths within the build files in |
| 72 |input_api.AffectedFiles()| that are within |package| ("SDK" or "EDK"). | 84 |input_api.AffectedFiles()| that are within |package|. |
| 73 An illegal absolute path within the SDK is one that is to the SDK itself | 85 An illegal absolute path within the SDK or a service's SDK is one that is to |
| 74 or a non-whitelisted external path. An illegal absolute path within the | 86 the SDK itself or a non-whitelisted external path. An illegal absolute path |
| 75 EDK is one that is to the SDK or the EDK. | 87 within the EDK is one that is to the SDK or the EDK. |
| 76 Returns any such references in a list of (file_path, line_number, | 88 Returns any such references in a list of (file_path, line_number, |
| 77 referenced_path) tuples.""" | 89 referenced_path) tuples.""" |
| 78 illegal_references = [] | 90 illegal_references = [] |
| 79 for f in _AffectedBuildFilesWithinPackage(input_api, package): | 91 for f in _AffectedBuildFilesWithinPackage(input_api, package): |
| 80 for line_num, line in f.ChangedContents(): | 92 for line_num, line in f.ChangedContents(): |
| 81 # Determine if this is a reference to an absolute path. | 93 # Determine if this is a reference to an absolute path. |
| 82 m = re.search(r'"(//[^"]*)"', line) | 94 m = re.search(r'"(//[^"]*)"', line) |
| 83 if not m: | 95 if not m: |
| 84 continue | 96 continue |
| 85 referenced_path = m.group(1) | 97 referenced_path = m.group(1) |
| 86 | 98 |
| 87 # In the EDK, all external absolute paths are allowed. | 99 if not referenced_path.startswith("//mojo"): |
| 88 if package == "EDK" and not referenced_path.startswith("//mojo"): | 100 # In the EDK, all external absolute paths are allowed. |
| 89 continue | 101 if package == "EDK": |
|
qsr
2015/01/28 14:57:53
Could you put this if before the previous one?
blundell
2015/01/28 15:23:02
Before which previous one? This check should only
| |
| 102 continue | |
| 90 | 103 |
| 91 # Determine if this is a whitelisted external path. | 104 # Determine if this is a whitelisted external path. |
| 92 if referenced_path in _SDK_WHITELISTED_EXTERNAL_PATHS: | 105 if referenced_path in _PACKAGE_WHITELISTED_EXTERNAL_PATHS[package]: |
| 93 continue | 106 continue |
| 94 | 107 |
| 95 illegal_references.append((f.LocalPath(), line_num, referenced_path)) | 108 illegal_references.append((f.LocalPath(), line_num, referenced_path)) |
| 96 | 109 |
| 97 return illegal_references | 110 return illegal_references |
| 98 | 111 |
| 99 def _PathReferenceInBuildFileWarningItem(build_file, line_num, referenced_path): | 112 def _PathReferenceInBuildFileWarningItem(build_file, line_num, referenced_path): |
| 100 """Returns a string expressing a warning item that |referenced_path| is | 113 """Returns a string expressing a warning item that |referenced_path| is |
| 101 referenced at |line_num| in |build_file|.""" | 114 referenced at |line_num| in |build_file|.""" |
| 102 return "%s, line %d (%s)" % (build_file, line_num, referenced_path) | 115 return "%s, line %d (%s)" % (build_file, line_num, referenced_path) |
| 103 | 116 |
| 104 def _IncorrectSourceSetTypeWarningItem(build_file, line_num): | 117 def _IncorrectSourceSetTypeWarningItem(build_file, line_num): |
| 105 """Returns a string expressing that the error occurs at |line_num| in | 118 """Returns a string expressing that the error occurs at |line_num| in |
| 106 |build_file|.""" | 119 |build_file|.""" |
| 107 return "%s, line %d" % (build_file, line_num) | 120 return "%s, line %d" % (build_file, line_num) |
| 108 | 121 |
| 109 def _CheckNoIllegalAbsolutePathsInBuildFiles(input_api, output_api, package): | 122 def _CheckNoIllegalAbsolutePathsInBuildFiles(input_api, output_api, package): |
| 110 """Makes sure that the BUILD.gn files within |package| ("SDK" or "EDK") do not | 123 """Makes sure that the BUILD.gn files within |package| do not reference the |
| 111 reference the SDK/EDK via absolute paths, and do not reference disallowed | 124 SDK/EDK via absolute paths, and do not reference disallowed external |
| 112 external dependencies.""" | 125 dependencies.""" |
| 113 sdk_references = [] | 126 sdk_references = [] |
| 114 edk_references = [] | 127 edk_references = [] |
| 115 external_deps_references = [] | 128 external_deps_references = [] |
| 129 services_references = [] | |
| 116 | 130 |
| 117 # Categorize any illegal references. | 131 # Categorize any illegal references. |
| 118 illegal_references = _FindIllegalAbsolutePathsInBuildFiles(input_api, package) | 132 illegal_references = _FindIllegalAbsolutePathsInBuildFiles(input_api, package) |
| 119 for build_file, line_num, referenced_path in illegal_references: | 133 for build_file, line_num, referenced_path in illegal_references: |
| 120 reference_string = _PathReferenceInBuildFileWarningItem(build_file, | 134 reference_string = _PathReferenceInBuildFileWarningItem(build_file, |
| 121 line_num, | 135 line_num, |
| 122 referenced_path) | 136 referenced_path) |
| 123 if referenced_path.startswith("//mojo/public"): | 137 if referenced_path.startswith("//mojo/public"): |
| 124 sdk_references.append(reference_string) | 138 sdk_references.append(reference_string) |
| 125 elif package == "SDK": | 139 elif package == "SDK": |
| 126 external_deps_references.append(reference_string) | 140 external_deps_references.append(reference_string) |
| 141 elif package == "services": | |
| 142 if referenced_path.startswith("//mojo/services"): | |
| 143 services_references.append(reference_string) | |
| 144 else: | |
| 145 external_deps_references.append(reference_string) | |
| 127 elif referenced_path.startswith("//mojo/edk"): | 146 elif referenced_path.startswith("//mojo/edk"): |
| 128 edk_references.append(reference_string) | 147 edk_references.append(reference_string) |
| 129 | 148 |
| 130 # Package up categorized illegal references into results. | 149 # Package up categorized illegal references into results. |
| 131 results = [] | 150 results = [] |
| 132 if sdk_references: | 151 if sdk_references: |
| 133 results.extend([output_api.PresubmitError( | 152 results.extend([output_api.PresubmitError( |
| 134 _ILLEGAL_SDK_ABSOLUTE_PATH_WARNING_MESSAGES[package], | 153 _ILLEGAL_SDK_ABSOLUTE_PATH_WARNING_MESSAGES[package], |
| 135 items=sdk_references)]) | 154 items=sdk_references)]) |
| 136 | 155 |
| 137 if external_deps_references: | 156 if external_deps_references: |
| 138 assert package == "SDK" | 157 assert package == "SDK" or package == "services" |
| 139 results.extend([output_api.PresubmitError( | 158 results.extend([output_api.PresubmitError( |
| 140 _ILLEGAL_EXTERNAL_PATH_WARNING_MESSAGE, | 159 _ILLEGAL_EXTERNAL_PATH_WARNING_MESSAGE, |
| 141 items=external_deps_references)]) | 160 items=external_deps_references)]) |
| 142 | 161 |
| 162 if services_references: | |
| 163 assert package == "services" | |
| 164 results.extend([output_api.PresubmitError( | |
| 165 _ILLEGAL_SERVICES_ABSOLUTE_PATH_WARNING_MESSAGE, | |
| 166 items=services_references)]) | |
| 167 | |
| 143 if edk_references: | 168 if edk_references: |
| 144 assert package == "EDK" | 169 assert package == "EDK" |
| 145 results.extend([output_api.PresubmitError( | 170 results.extend([output_api.PresubmitError( |
| 146 _ILLEGAL_EDK_ABSOLUTE_PATH_WARNING_MESSAGE, | 171 _ILLEGAL_EDK_ABSOLUTE_PATH_WARNING_MESSAGE, |
| 147 items=edk_references)]) | 172 items=edk_references)]) |
| 148 | 173 |
| 149 return results | 174 return results |
| 150 | 175 |
| 151 def _CheckSourceSetsAreOfCorrectType(input_api, output_api, package): | 176 def _CheckSourceSetsAreOfCorrectType(input_api, output_api, package): |
| 152 """Makes sure that the BUILD.gn files always use the correct wrapper type for | 177 """Makes sure that the BUILD.gn files always use the correct wrapper type for |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 208 mojo_python_bindings_tests_path, | 233 mojo_python_bindings_tests_path, |
| 209 mojo_roll_tools_path, | 234 mojo_roll_tools_path, |
| 210 mopy_path, | 235 mopy_path, |
| 211 ] | 236 ] |
| 212 results.extend(input_api.canned_checks.RunPylint( | 237 results.extend(input_api.canned_checks.RunPylint( |
| 213 input_api, output_api, extra_paths_list=pylint_extra_paths, | 238 input_api, output_api, extra_paths_list=pylint_extra_paths, |
| 214 black_list=temporary_black_list)) | 239 black_list=temporary_black_list)) |
| 215 return results | 240 return results |
| 216 | 241 |
| 217 def _BuildFileChecks(input_api, output_api): | 242 def _BuildFileChecks(input_api, output_api): |
| 218 """Performs checks on SDK and EDK buildfiles.""" | 243 """Performs checks on SDK, EDK, and services' public buildfiles.""" |
| 219 results = [] | 244 results = [] |
| 220 for package in ["SDK", "EDK"]: | 245 for package in ["SDK", "EDK", "services"]: |
| 221 results.extend(_CheckNoIllegalAbsolutePathsInBuildFiles(input_api, | 246 results.extend(_CheckNoIllegalAbsolutePathsInBuildFiles(input_api, |
| 222 output_api, | 247 output_api, |
| 223 package)) | 248 package)) |
| 224 results.extend(_CheckSourceSetsAreOfCorrectType(input_api, | 249 results.extend(_CheckSourceSetsAreOfCorrectType(input_api, |
| 225 output_api, | 250 output_api, |
| 226 package)) | 251 package)) |
| 227 return results | 252 return results |
| 228 | 253 |
| 229 def _CommonChecks(input_api, output_api): | 254 def _CommonChecks(input_api, output_api): |
| 230 """Checks common to both upload and commit.""" | 255 """Checks common to both upload and commit.""" |
| 231 results = [] | 256 results = [] |
| 232 results.extend(_BuildFileChecks(input_api, output_api)) | 257 results.extend(_BuildFileChecks(input_api, output_api)) |
| 233 return results | 258 return results |
| 234 | 259 |
| 235 def CheckChangeOnUpload(input_api, output_api): | 260 def CheckChangeOnUpload(input_api, output_api): |
| 236 results = [] | 261 results = [] |
| 237 results.extend(_CommonChecks(input_api, output_api)) | 262 results.extend(_CommonChecks(input_api, output_api)) |
| 238 results.extend(_CheckChangePylintsClean(input_api, output_api)) | 263 results.extend(_CheckChangePylintsClean(input_api, output_api)) |
| 239 return results | 264 return results |
| 240 | 265 |
| 241 def CheckChangeOnCommit(input_api, output_api): | 266 def CheckChangeOnCommit(input_api, output_api): |
| 242 results = [] | 267 results = [] |
| 243 results.extend(_CommonChecks(input_api, output_api)) | 268 results.extend(_CommonChecks(input_api, output_api)) |
| 244 return results | 269 return results |
| OLD | NEW |