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 = [ |
viettrungluu
2014/12/10 00:02:02
Maybe this should be _SDK_... (leading underscore)
blundell
2014/12/10 16:13:05
Done.
| |
15 "//testing/gtest", | |
16 "//third_party/cython", | |
17 "//third_party/khronos", | |
18 ] | |
19 | |
20 def _IsBuildFileWithinPackage(f, package): | |
21 """Returns whether |f| specifies a GN build file within |package| ("SDK" or | |
22 "EDK").""" | |
viettrungluu
2014/12/10 00:02:02
nit: I don't believe docstrings should be indented
blundell
2014/12/10 16:13:06
Done.
| |
23 if package == "SDK": | |
viettrungluu
2014/12/10 00:02:02
Suggestion:
Define a global:
_PACKAGE_PATH_PREFI
blundell
2014/12/10 16:13:05
Done.
| |
24 package_path_prefix = "mojo/public" | |
25 elif package == "EDK": | |
26 package_path_prefix = "mojo/edk" | |
27 else: | |
28 assert 0, "unknown package: %s" % package | |
29 | |
30 if not f.LocalPath().startswith(package_path_prefix): | |
viettrungluu
2014/12/10 00:02:02
Arguably, you should include a trailing '/' in the
blundell
2014/12/10 16:13:05
Done.
| |
31 return False | |
32 if (not f.LocalPath().endswith("BUILD.gn") and | |
viettrungluu
2014/12/10 00:02:02
This isn't quite right. <shrug>
blundell
2014/12/10 16:13:06
I assume you mean because it will return True for
| |
33 not f.LocalPath().endswith(".gni")): | |
34 return False | |
35 return True | |
36 | |
37 def _FindIllegalAbsolutePathsInBuildFiles(input_api, package): | |
38 """Finds illegal absolute paths within the build files in | |
39 |input_api.AffectedFiles()| that are within |package| ("SDK" or "EDK"). | |
40 An illegal absolute path within the SDK is one that is to the SDK itself | |
41 or a non-whitelisted external path. An illegal absolute path within the | |
42 EDK is one that is to the SDK or the EDK. | |
43 Returns any such references in a list of (file_path, line_number, | |
44 referenced_path) tuples.""" | |
45 illegal_references = [] | |
46 for f in input_api.AffectedFiles(): | |
47 if not _IsBuildFileWithinPackage(f, package): | |
viettrungluu
2014/12/10 00:02:02
Maybe the "correct" thing to do is to define a hel
blundell
2014/12/10 16:13:06
Done.
| |
48 continue | |
49 | |
50 for line_num, line in f.ChangedContents(): | |
51 # Determine if this is a reference to an absolute path. | |
52 m = re.search(r'"(//[^"]*)"', line) | |
53 if not m: | |
54 continue | |
55 referenced_path = m.group(1) | |
56 | |
57 # In the EDK, all external absolute paths are allowed. | |
58 if package == "EDK" and not referenced_path.startswith("//mojo"): | |
59 continue | |
60 | |
61 # Determine if this is a whitelisted external path. | |
62 for whitelisted_path in SDK_WHITELISTED_EXTERNAL_PATHS: | |
63 if referenced_path.startswith(whitelisted_path): | |
64 continue | |
65 | |
66 illegal_references.append((f.LocalPath(), line_num, referenced_path)) | |
67 | |
68 return illegal_references | |
69 | |
70 def _CheckNoIllegalAbsolutePathsInBuildFiles(input_api, output_api, package): | |
71 """Makes sure that the BUILD.gn files within |package| ("SDK" or "EDK") do not | |
72 reference the SDK/EDK via absolute paths, and do not reference disallowed | |
73 external dependencies.""" | |
74 sdk_references = [] | |
75 edk_references = [] | |
76 external_deps_references = [] | |
77 | |
78 # Categorize any illegal references. | |
79 illegal_references = _FindIllegalAbsolutePathsInBuildFiles(input_api, package) | |
80 for build_file, line_num, referenced_path in illegal_references: | |
81 reference_string = "%s, line %d (%s)" % (build_file, | |
82 line_num, | |
83 referenced_path) | |
84 if referenced_path.startswith("//mojo/public"): | |
85 sdk_references.append(reference_string) | |
86 elif package == "SDK": | |
87 external_deps_references.append(reference_string) | |
88 else: | |
89 assert referenced_path.startswith("//mojo/edk") | |
90 edk_references.append(reference_string) | |
91 | |
92 # Package up categorized illegal references into results. | |
93 results = [] | |
94 if sdk_references: | |
95 results += [output_api.PresubmitError( | |
viettrungluu
2014/12/10 00:02:02
results.append() instead?
blundell
2014/12/10 16:13:05
results.extend() seems to be the canonical form he
| |
96 ("Found references to the SDK via absolute paths within %s buildfiles." | |
97 % package), | |
98 items=sdk_references)] | |
99 | |
100 if external_deps_references: | |
101 assert package == "SDK" | |
102 results += [output_api.PresubmitError( | |
viettrungluu
2014/12/10 00:02:02
" (etc.)
blundell
2014/12/10 16:13:05
Done.
| |
103 "Found disallowed external paths within SDK buildfiles.", | |
104 items=external_deps_references)] | |
105 | |
106 if edk_references: | |
107 assert package == "EDK" | |
108 results += [output_api.PresubmitError( | |
109 "Found references to the EDK via absolute paths within EDK buildfiles.", | |
110 items=edk_references)] | |
111 | |
112 return results | |
113 | |
114 def _CheckSourceSetsAreOfCorrectType(input_api, output_api, package): | |
115 """Makes sure that the BUILD.gn files always use the correct wrapper type for | |
116 |package|, which can be one of ["SDK", "EDK"], to construct source_set | |
117 targets.""" | |
118 if package == "SDK": | |
viettrungluu
2014/12/10 00:02:02
Similar suggestion as above.
blundell
2014/12/10 16:13:05
Done.
| |
119 required_source_set_type = "mojo_sdk_source_set" | |
120 elif package == "EDK": | |
121 required_source_set_type = "mojo_edk_source_set" | |
122 else: | |
123 assert 0, "unknown package: %s" % package | |
124 | |
125 problems = [] | |
126 for f in input_api.AffectedFiles(): | |
127 if not _IsBuildFileWithinPackage(f, package): | |
128 continue | |
129 | |
130 for line_num, line in f.ChangedContents(): | |
131 m = re.search(r"[a-z_]*source_set\(", line) | |
132 if not m: | |
133 continue | |
134 source_set_type = m.group(0)[:-1] | |
135 if source_set_type == required_source_set_type: | |
136 continue | |
137 problems.append("%s, line %d" % (f.LocalPath(), line_num)) | |
138 | |
139 if not problems: | |
140 return [] | |
141 return [output_api.PresubmitError( | |
142 "All source sets in the %s must be constructed via %s." % ( | |
143 package, required_source_set_type), | |
144 items=problems)] | |
145 | |
146 def _CheckChangePylintsClean(input_api, output_api): | |
14 # Additional python module paths (we're in src/mojo/); not everyone needs | 147 # 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. | 148 # them, but it's easiest to add them to everyone's path. |
16 # For ply and jinja2: | 149 # For ply and jinja2: |
17 third_party_path = os.path.join( | 150 third_party_path = os.path.join( |
18 input_api.PresubmitLocalPath(), "..", "third_party") | 151 input_api.PresubmitLocalPath(), "..", "third_party") |
19 # For the bindings generator: | 152 # For the bindings generator: |
20 mojo_public_bindings_pylib_path = os.path.join( | 153 mojo_public_bindings_pylib_path = os.path.join( |
21 input_api.PresubmitLocalPath(), "public", "tools", "bindings", "pylib") | 154 input_api.PresubmitLocalPath(), "public", "tools", "bindings", "pylib") |
22 # For the python bindings: | 155 # For the python bindings: |
23 mojo_python_bindings_path = os.path.join( | 156 mojo_python_bindings_path = os.path.join( |
(...skipping 20 matching lines...) Expand all Loading... | |
44 mojo_public_bindings_pylib_path, | 177 mojo_public_bindings_pylib_path, |
45 mojo_python_bindings_path, | 178 mojo_python_bindings_path, |
46 mojo_python_bindings_tests_path, | 179 mojo_python_bindings_tests_path, |
47 mojo_roll_tools_path, | 180 mojo_roll_tools_path, |
48 mopy_path, | 181 mopy_path, |
49 ] | 182 ] |
50 results += input_api.canned_checks.RunPylint( | 183 results += input_api.canned_checks.RunPylint( |
51 input_api, output_api, extra_paths_list=pylint_extra_paths, | 184 input_api, output_api, extra_paths_list=pylint_extra_paths, |
52 black_list=temporary_black_list) | 185 black_list=temporary_black_list) |
53 return results | 186 return results |
187 | |
188 def _CommonChecks(input_api, output_api): | |
189 """Checks common to both upload and commit.""" | |
190 results = [] | |
191 for package in ["SDK", "EDK"]: | |
192 results.extend(_CheckNoIllegalAbsolutePathsInBuildFiles(input_api, | |
193 output_api, | |
194 package)) | |
195 results.extend(_CheckSourceSetsAreOfCorrectType(input_api, | |
196 output_api, | |
197 package)) | |
198 return results | |
199 | |
200 def CheckChangeOnUpload(input_api, output_api): | |
201 results = [] | |
202 results.extend(_CommonChecks(input_api, output_api)) | |
203 results.extend(_CheckChangePylintsClean(input_api, output_api)) | |
204 return results | |
205 | |
206 def CheckChangeOnCommit(input_api, output_api): | |
207 results = [] | |
208 results.extend(_CommonChecks(input_api, output_api)) | |
209 return results | |
OLD | NEW |