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 | 13 |
13 def CheckChangeOnUpload(input_api, output_api): | 14 _SDK_WHITELISTED_EXTERNAL_PATHS = [ |
| 15 "//testing/gtest", |
| 16 "//third_party/cython", |
| 17 "//third_party/khronos", |
| 18 ] |
| 19 |
| 20 _PACKAGE_PATH_PREFIXES = {"SDK": "mojo/public/", |
| 21 "EDK": "mojo/edk/"} |
| 22 |
| 23 _PACKAGE_SOURCE_SET_TYPES = {"SDK": "mojo_sdk_source_set", |
| 24 "EDK": "mojo_edk_source_set"} |
| 25 |
| 26 _ILLEGAL_EXTERNAL_PATH_WARNING_MESSAGE = \ |
| 27 "Found disallowed external paths within SDK buildfiles." |
| 28 |
| 29 _ILLEGAL_EDK_ABSOLUTE_PATH_WARNING_MESSAGE = \ |
| 30 "Found references to the EDK via absolute paths within EDK buildfiles.", |
| 31 |
| 32 _ILLEGAL_SDK_ABSOLUTE_PATH_WARNING_MESSAGE_TEMPLATE = \ |
| 33 "Found references to the SDK via absolute paths within %s buildfiles." |
| 34 |
| 35 _ILLEGAL_SDK_ABSOLUTE_PATH_WARNING_MESSAGES = { |
| 36 "SDK": _ILLEGAL_SDK_ABSOLUTE_PATH_WARNING_MESSAGE_TEMPLATE % "SDK", |
| 37 "EDK": _ILLEGAL_SDK_ABSOLUTE_PATH_WARNING_MESSAGE_TEMPLATE % "EDK", |
| 38 } |
| 39 |
| 40 _INCORRECT_SOURCE_SET_TYPE_WARNING_MESSAGE_TEMPLATE = \ |
| 41 "All source sets in the %s must be constructed via %s." |
| 42 |
| 43 _INCORRECT_SOURCE_SET_TYPE_WARNING_MESSAGES = { |
| 44 "SDK": _INCORRECT_SOURCE_SET_TYPE_WARNING_MESSAGE_TEMPLATE |
| 45 % ("SDK", _PACKAGE_SOURCE_SET_TYPES["SDK"]), |
| 46 "EDK": _INCORRECT_SOURCE_SET_TYPE_WARNING_MESSAGE_TEMPLATE |
| 47 % ("EDK", _PACKAGE_SOURCE_SET_TYPES["EDK"]), |
| 48 } |
| 49 |
| 50 def _IsBuildFileWithinPackage(f, package): |
| 51 """Returns whether |f| specifies a GN build file within |package| ("SDK" or |
| 52 "EDK").""" |
| 53 assert package in _PACKAGE_PATH_PREFIXES |
| 54 package_path_prefix = _PACKAGE_PATH_PREFIXES[package] |
| 55 |
| 56 if not f.LocalPath().startswith(package_path_prefix): |
| 57 return False |
| 58 if (not f.LocalPath().endswith("/BUILD.gn") and |
| 59 not f.LocalPath().endswith(".gni")): |
| 60 return False |
| 61 return True |
| 62 |
| 63 def _AffectedBuildFilesWithinPackage(input_api, package): |
| 64 """Returns all the affected build files within |package| ("SDK" or "EDK").""" |
| 65 return [f for f in input_api.AffectedFiles() |
| 66 if _IsBuildFileWithinPackage(f, package)] |
| 67 |
| 68 def _FindIllegalAbsolutePathsInBuildFiles(input_api, package): |
| 69 """Finds illegal absolute paths within the build files in |
| 70 |input_api.AffectedFiles()| that are within |package| ("SDK" or "EDK"). |
| 71 An illegal absolute path within the SDK is one that is to the SDK itself |
| 72 or a non-whitelisted external path. An illegal absolute path within the |
| 73 EDK is one that is to the SDK or the EDK. |
| 74 Returns any such references in a list of (file_path, line_number, |
| 75 referenced_path) tuples.""" |
| 76 illegal_references = [] |
| 77 for f in _AffectedBuildFilesWithinPackage(input_api, package): |
| 78 for line_num, line in f.ChangedContents(): |
| 79 # Determine if this is a reference to an absolute path. |
| 80 m = re.search(r'"(//[^"]*)"', line) |
| 81 if not m: |
| 82 continue |
| 83 referenced_path = m.group(1) |
| 84 |
| 85 # In the EDK, all external absolute paths are allowed. |
| 86 if package == "EDK" and not referenced_path.startswith("//mojo"): |
| 87 continue |
| 88 |
| 89 # Determine if this is a whitelisted external path. |
| 90 if referenced_path in _SDK_WHITELISTED_EXTERNAL_PATHS: |
| 91 continue |
| 92 |
| 93 illegal_references.append((f.LocalPath(), line_num, referenced_path)) |
| 94 |
| 95 return illegal_references |
| 96 |
| 97 def _PathReferenceInBuildFileWarningItem(build_file, line_num, referenced_path): |
| 98 """Returns a string expressing a warning item that |referenced_path| is |
| 99 referenced at |line_num| in |build_file|.""" |
| 100 return "%s, line %d (%s)" % (build_file, line_num, referenced_path) |
| 101 |
| 102 def _IncorrectSourceSetTypeWarningItem(build_file, line_num): |
| 103 """Returns a string expressing that the error occurs at |line_num| in |
| 104 |build_file|.""" |
| 105 return "%s, line %d" % (build_file, line_num) |
| 106 |
| 107 def _CheckNoIllegalAbsolutePathsInBuildFiles(input_api, output_api, package): |
| 108 """Makes sure that the BUILD.gn files within |package| ("SDK" or "EDK") do not |
| 109 reference the SDK/EDK via absolute paths, and do not reference disallowed |
| 110 external dependencies.""" |
| 111 sdk_references = [] |
| 112 edk_references = [] |
| 113 external_deps_references = [] |
| 114 |
| 115 # Categorize any illegal references. |
| 116 illegal_references = _FindIllegalAbsolutePathsInBuildFiles(input_api, package) |
| 117 for build_file, line_num, referenced_path in illegal_references: |
| 118 reference_string = _PathReferenceInBuildFileWarningItem(build_file, |
| 119 line_num, |
| 120 referenced_path) |
| 121 if referenced_path.startswith("//mojo/public"): |
| 122 sdk_references.append(reference_string) |
| 123 elif package == "SDK": |
| 124 external_deps_references.append(reference_string) |
| 125 else: |
| 126 assert referenced_path.startswith("//mojo/edk") |
| 127 edk_references.append(reference_string) |
| 128 |
| 129 # Package up categorized illegal references into results. |
| 130 results = [] |
| 131 if sdk_references: |
| 132 results.extend([output_api.PresubmitError( |
| 133 _ILLEGAL_SDK_ABSOLUTE_PATH_WARNING_MESSAGES[package], |
| 134 items=sdk_references)]) |
| 135 |
| 136 if external_deps_references: |
| 137 assert package == "SDK" |
| 138 results.extend([output_api.PresubmitError( |
| 139 _ILLEGAL_EXTERNAL_PATH_WARNING_MESSAGE, |
| 140 items=external_deps_references)]) |
| 141 |
| 142 if edk_references: |
| 143 assert package == "EDK" |
| 144 results.extend([output_api.PresubmitError( |
| 145 _ILLEGAL_EDK_ABSOLUTE_PATH_WARNING_MESSAGE, |
| 146 items=edk_references)]) |
| 147 |
| 148 return results |
| 149 |
| 150 def _CheckSourceSetsAreOfCorrectType(input_api, output_api, package): |
| 151 """Makes sure that the BUILD.gn files always use the correct wrapper type for |
| 152 |package|, which can be one of ["SDK", "EDK"], to construct source_set |
| 153 targets.""" |
| 154 assert package in _PACKAGE_SOURCE_SET_TYPES |
| 155 required_source_set_type = _PACKAGE_SOURCE_SET_TYPES[package] |
| 156 |
| 157 problems = [] |
| 158 for f in _AffectedBuildFilesWithinPackage(input_api, package): |
| 159 for line_num, line in f.ChangedContents(): |
| 160 m = re.search(r"[a-z_]*source_set\(", line) |
| 161 if not m: |
| 162 continue |
| 163 source_set_type = m.group(0)[:-1] |
| 164 if source_set_type == required_source_set_type: |
| 165 continue |
| 166 problems.append(_IncorrectSourceSetTypeWarningItem(f.LocalPath(), |
| 167 line_num)) |
| 168 |
| 169 if not problems: |
| 170 return [] |
| 171 return [output_api.PresubmitError( |
| 172 _INCORRECT_SOURCE_SET_TYPE_WARNING_MESSAGES[package], |
| 173 items=problems)] |
| 174 |
| 175 def _CheckChangePylintsClean(input_api, output_api): |
14 # Additional python module paths (we're in src/mojo/); not everyone needs | 176 # Additional python module paths (we're in src/mojo/); not everyone needs |
15 # them, but it's easiest to add them to everyone's path. | 177 # them, but it's easiest to add them to everyone's path. |
16 # For ply and jinja2: | 178 # For ply and jinja2: |
17 third_party_path = os.path.join( | 179 third_party_path = os.path.join( |
18 input_api.PresubmitLocalPath(), "..", "third_party") | 180 input_api.PresubmitLocalPath(), "..", "third_party") |
19 # For the bindings generator: | 181 # For the bindings generator: |
20 mojo_public_bindings_pylib_path = os.path.join( | 182 mojo_public_bindings_pylib_path = os.path.join( |
21 input_api.PresubmitLocalPath(), "public", "tools", "bindings", "pylib") | 183 input_api.PresubmitLocalPath(), "public", "tools", "bindings", "pylib") |
22 # For the python bindings: | 184 # For the python bindings: |
23 mojo_python_bindings_path = os.path.join( | 185 mojo_python_bindings_path = os.path.join( |
(...skipping 16 matching lines...) Expand all Loading... |
40 | 202 |
41 results = [] | 203 results = [] |
42 pylint_extra_paths = [ | 204 pylint_extra_paths = [ |
43 third_party_path, | 205 third_party_path, |
44 mojo_public_bindings_pylib_path, | 206 mojo_public_bindings_pylib_path, |
45 mojo_python_bindings_path, | 207 mojo_python_bindings_path, |
46 mojo_python_bindings_tests_path, | 208 mojo_python_bindings_tests_path, |
47 mojo_roll_tools_path, | 209 mojo_roll_tools_path, |
48 mopy_path, | 210 mopy_path, |
49 ] | 211 ] |
50 results += input_api.canned_checks.RunPylint( | 212 results.extend(input_api.canned_checks.RunPylint( |
51 input_api, output_api, extra_paths_list=pylint_extra_paths, | 213 input_api, output_api, extra_paths_list=pylint_extra_paths, |
52 black_list=temporary_black_list) | 214 black_list=temporary_black_list)) |
53 return results | 215 return results |
| 216 |
| 217 def _BuildFileChecks(input_api, output_api): |
| 218 """Performs checks on SDK and EDK buildfiles.""" |
| 219 results = [] |
| 220 for package in ["SDK", "EDK"]: |
| 221 results.extend(_CheckNoIllegalAbsolutePathsInBuildFiles(input_api, |
| 222 output_api, |
| 223 package)) |
| 224 results.extend(_CheckSourceSetsAreOfCorrectType(input_api, |
| 225 output_api, |
| 226 package)) |
| 227 return results |
| 228 |
| 229 def _CommonChecks(input_api, output_api): |
| 230 """Checks common to both upload and commit.""" |
| 231 results = [] |
| 232 results.extend(_BuildFileChecks(input_api, output_api)) |
| 233 return results |
| 234 |
| 235 def CheckChangeOnUpload(input_api, output_api): |
| 236 results = [] |
| 237 results.extend(_CommonChecks(input_api, output_api)) |
| 238 results.extend(_CheckChangePylintsClean(input_api, output_api)) |
| 239 return results |
| 240 |
| 241 def CheckChangeOnCommit(input_api, output_api): |
| 242 results = [] |
| 243 results.extend(_CommonChecks(input_api, output_api)) |
| 244 return results |
OLD | NEW |