OLD | NEW |
1 #!/usr/bin/python | 1 #!/usr/bin/python |
2 # Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. |
3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 | 5 |
6 """Makes sure that files include headers from allowed directories. | 6 """Makes sure that files include headers from allowed directories. |
7 | 7 |
8 Checks DEPS files in the source tree for rules, and applies those rules to | 8 Checks DEPS files in the source tree for rules, and applies those rules to |
9 "#include" commands in source files. Any source file including something not | 9 "#include" commands in source files. Any source file including something not |
10 permitted by the DEPS files will fail. | 10 permitted by the DEPS files will fail. |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
49 | 49 |
50 Note that all directory separators must be slashes (Unix-style) and not | 50 Note that all directory separators must be slashes (Unix-style) and not |
51 backslashes. All directories should be relative to the source root and use | 51 backslashes. All directories should be relative to the source root and use |
52 only lowercase. | 52 only lowercase. |
53 """ | 53 """ |
54 | 54 |
55 import os | 55 import os |
56 import optparse | 56 import optparse |
57 import re | 57 import re |
58 import sys | 58 import sys |
| 59 import copy |
59 | 60 |
60 # Variable name used in the DEPS file to specify module-level deps. | 61 # Variable name used in the DEPS file to specify module-level deps. |
61 DEPS_VAR_NAME = "deps" | 62 DEPS_VAR_NAME = "deps" |
62 | 63 |
63 # Variable name used in the DEPS file to add or subtract include files from | 64 # Variable name used in the DEPS file to add or subtract include files from |
64 # the module-level deps. | 65 # the module-level deps. |
65 INCLUDE_RULES_VAR_NAME = "include_rules" | 66 INCLUDE_RULES_VAR_NAME = "include_rules" |
66 | 67 |
67 # Optionally present in the DEPS file to list subdirectories which should not | 68 # Optionally present in the DEPS file to list subdirectories which should not |
68 # be checked. This allows us to skip third party code, for example. | 69 # be checked. This allows us to skip third party code, for example. |
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
173 Args: | 174 Args: |
174 existing_rules: A set of existing rules that will be combined. | 175 existing_rules: A set of existing rules that will be combined. |
175 deps: The list of imports from the "deps" section of the DEPS file. | 176 deps: The list of imports from the "deps" section of the DEPS file. |
176 include: The list of rules from the "include_rules" section of DEPS. | 177 include: The list of rules from the "include_rules" section of DEPS. |
177 cur_dir: The current directory. We will create an implicit rule that | 178 cur_dir: The current directory. We will create an implicit rule that |
178 allows inclusion from this directory. | 179 allows inclusion from this directory. |
179 | 180 |
180 Returns: A new set of rules combining the existing_rules with the other | 181 Returns: A new set of rules combining the existing_rules with the other |
181 arguments. | 182 arguments. |
182 """ | 183 """ |
183 rules = existing_rules | 184 rules = copy.copy(existing_rules) |
184 | 185 |
185 # First apply the implicit "allow" rule for the current directory. | 186 # First apply the implicit "allow" rule for the current directory. |
186 if cur_dir.lower().startswith(BASE_DIRECTORY): | 187 if cur_dir.lower().startswith(BASE_DIRECTORY): |
187 relative_dir = cur_dir[len(BASE_DIRECTORY) + 1:] | 188 relative_dir = cur_dir[len(BASE_DIRECTORY) + 1:] |
188 # Normalize path separators to slashes. | 189 # Normalize path separators to slashes. |
189 relative_dir = relative_dir.replace("\\", "/") | 190 relative_dir = relative_dir.replace("\\", "/") |
190 source = relative_dir | 191 source = relative_dir |
191 if len(source) == 0: | 192 if len(source) == 0: |
192 source = "top level" # Make the help string a little more meaningful. | 193 source = "top level" # Make the help string a little more meaningful. |
193 rules.AddRule("+" + relative_dir, "Default rule for " + source) | 194 rules.AddRule("+" + relative_dir, "Default rule for " + source) |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
248 | 249 |
249 def Lookup(self, var_name): | 250 def Lookup(self, var_name): |
250 """Implements the Var syntax.""" | 251 """Implements the Var syntax.""" |
251 if var_name in self._local_scope.get("vars", {}): | 252 if var_name in self._local_scope.get("vars", {}): |
252 return self._local_scope["vars"][var_name] | 253 return self._local_scope["vars"][var_name] |
253 raise Error("Var is not defined: %s" % var_name) | 254 raise Error("Var is not defined: %s" % var_name) |
254 | 255 |
255 local_scope = {} | 256 local_scope = {} |
256 global_scope = {"From": FromImpl, "Var": _VarImpl(local_scope).Lookup} | 257 global_scope = {"From": FromImpl, "Var": _VarImpl(local_scope).Lookup} |
257 deps_file = os.path.join(dir_name, "DEPS") | 258 deps_file = os.path.join(dir_name, "DEPS") |
258 if not os.path.exists(deps_file): | |
259 if VERBOSE: | |
260 print " No deps file found in", dir_name | |
261 return (existing_rules, []) # Nothing to change from the input rules. | |
262 | 259 |
263 execfile(deps_file, global_scope, local_scope) | 260 if os.path.exists(deps_file): |
| 261 execfile(deps_file, global_scope, local_scope) |
| 262 elif VERBOSE: |
| 263 print " No deps file found in", dir_name |
264 | 264 |
| 265 # Even if a DEPS file does not exist we still invoke ApplyRules |
| 266 # to apply the implicit "allow" rule for the current directory |
265 deps = local_scope.get(DEPS_VAR_NAME, {}) | 267 deps = local_scope.get(DEPS_VAR_NAME, {}) |
266 include_rules = local_scope.get(INCLUDE_RULES_VAR_NAME, []) | 268 include_rules = local_scope.get(INCLUDE_RULES_VAR_NAME, []) |
267 skip_subdirs = local_scope.get(SKIP_SUBDIRS_VAR_NAME, []) | 269 skip_subdirs = local_scope.get(SKIP_SUBDIRS_VAR_NAME, []) |
268 | 270 |
269 return (ApplyRules(existing_rules, deps, include_rules, dir_name), | 271 return (ApplyRules(existing_rules, deps, include_rules, dir_name), |
270 skip_subdirs) | 272 skip_subdirs) |
271 | 273 |
272 | 274 |
273 def ShouldCheckFile(file_name): | 275 def ShouldCheckFile(file_name): |
274 """Returns True if the given file is a type we want to check.""" | 276 """Returns True if the given file is a type we want to check.""" |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
341 if VERBOSE: | 343 if VERBOSE: |
342 print "Unable to open file: " + file_name | 344 print "Unable to open file: " + file_name |
343 cur_file.close() | 345 cur_file.close() |
344 | 346 |
345 # Map empty string to None for easier checking. | 347 # Map empty string to None for easier checking. |
346 if len(ret_val) == 0: | 348 if len(ret_val) == 0: |
347 return None | 349 return None |
348 return ret_val | 350 return ret_val |
349 | 351 |
350 | 352 |
351 def CheckDirectory(rules, dir_name): | 353 def CheckDirectory(parent_rules, dir_name): |
352 (rules, skip_subdirs) = ApplyDirectoryRules(rules, dir_name) | 354 (rules, skip_subdirs) = ApplyDirectoryRules(parent_rules, dir_name) |
353 if rules == None: | 355 if rules == None: |
354 return True | 356 return True |
355 | 357 |
356 # Collect a list of all files and directories to check. | 358 # Collect a list of all files and directories to check. |
357 files_to_check = [] | 359 files_to_check = [] |
358 dirs_to_check = [] | 360 dirs_to_check = [] |
359 success = True | 361 success = True |
360 contents = os.listdir(dir_name) | 362 contents = os.listdir(dir_name) |
361 for cur in contents: | 363 for cur in contents: |
362 if cur in skip_subdirs: | 364 if cur in skip_subdirs: |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
446 option_parser.add_option("", "--root", default="", dest="base_directory", | 448 option_parser.add_option("", "--root", default="", dest="base_directory", |
447 help='Specifies the repository root. This defaults ' | 449 help='Specifies the repository root. This defaults ' |
448 'to "../../.." relative to the script file, which ' | 450 'to "../../.." relative to the script file, which ' |
449 'will normally be the repository root.') | 451 'will normally be the repository root.') |
450 option_parser.add_option("-v", "--verbose", action="store_true", | 452 option_parser.add_option("-v", "--verbose", action="store_true", |
451 default=False, help="Print debug logging") | 453 default=False, help="Print debug logging") |
452 options, args = option_parser.parse_args() | 454 options, args = option_parser.parse_args() |
453 main(options, args) | 455 main(options, args) |
454 | 456 |
455 | 457 |
OLD | NEW |