| 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 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 62 DEPS_VAR_NAME = "deps" | 62 DEPS_VAR_NAME = "deps" |
| 63 | 63 |
| 64 # 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 |
| 65 # the module-level deps. | 65 # the module-level deps. |
| 66 INCLUDE_RULES_VAR_NAME = "include_rules" | 66 INCLUDE_RULES_VAR_NAME = "include_rules" |
| 67 | 67 |
| 68 # 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 |
| 69 # 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. |
| 70 SKIP_SUBDIRS_VAR_NAME = "skip_child_includes" | 70 SKIP_SUBDIRS_VAR_NAME = "skip_child_includes" |
| 71 | 71 |
| 72 # We'll search for lines beginning with this string for checking. | |
| 73 INCLUDE_PREFIX = "#include" | |
| 74 | |
| 75 # The maximum number of lines to check in each source file before giving up. | 72 # The maximum number of lines to check in each source file before giving up. |
| 76 MAX_LINES = 150 | 73 MAX_LINES = 150 |
| 77 | 74 |
| 78 # The maximum line length, this is to be efficient in the case of very long | 75 # The maximum line length, this is to be efficient in the case of very long |
| 79 # lines (which can't be #includes). | 76 # lines (which can't be #includes). |
| 80 MAX_LINE_LENGTH = 128 | 77 MAX_LINE_LENGTH = 128 |
| 81 | 78 |
| 82 # Set to true for more output. This is set by the command line options. | 79 # Set to true for more output. This is set by the command line options. |
| 83 VERBOSE = False | 80 VERBOSE = False |
| 84 | 81 |
| 85 # This regular expression will be used to extract filenames from include | 82 # This regular expression will be used to extract filenames from include |
| 86 # statements. | 83 # statements. |
| 87 EXTRACT_INCLUDE_FILENAME = re.compile(INCLUDE_PREFIX + ' *"(.*)"') | 84 EXTRACT_INCLUDE_PATH = re.compile('[ \t]*#[ \t]*(?:include|import)[ \t]+"(.*)"') |
| 88 | 85 |
| 89 # In lowercase, using forward slashes as directory separators, ending in a | 86 # In lowercase, using forward slashes as directory separators, ending in a |
| 90 # forward slash. Set by the command line options. | 87 # forward slash. Set by the command line options. |
| 91 BASE_DIRECTORY = "" | 88 BASE_DIRECTORY = "" |
| 92 | 89 |
| 93 # Specifies a single rule for an include, which can be either allow or disallow. | 90 # Specifies a single rule for an include, which can be either allow or disallow. |
| 94 class Rule(object): | 91 class Rule(object): |
| 95 def __init__(self, allow, dir, source): | 92 def __init__(self, allow, dir, source): |
| 96 self._allow = allow | 93 self._allow = allow |
| 97 self._dir = dir | 94 self._dir = dir |
| (...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 267 deps = local_scope.get(DEPS_VAR_NAME, {}) | 264 deps = local_scope.get(DEPS_VAR_NAME, {}) |
| 268 include_rules = local_scope.get(INCLUDE_RULES_VAR_NAME, []) | 265 include_rules = local_scope.get(INCLUDE_RULES_VAR_NAME, []) |
| 269 skip_subdirs = local_scope.get(SKIP_SUBDIRS_VAR_NAME, []) | 266 skip_subdirs = local_scope.get(SKIP_SUBDIRS_VAR_NAME, []) |
| 270 | 267 |
| 271 return (ApplyRules(existing_rules, deps, include_rules, dir_name), | 268 return (ApplyRules(existing_rules, deps, include_rules, dir_name), |
| 272 skip_subdirs) | 269 skip_subdirs) |
| 273 | 270 |
| 274 | 271 |
| 275 def ShouldCheckFile(file_name): | 272 def ShouldCheckFile(file_name): |
| 276 """Returns True if the given file is a type we want to check.""" | 273 """Returns True if the given file is a type we want to check.""" |
| 277 if len(file_name) < 2: | 274 checked_extensions = [ |
| 278 return False | 275 '.h', |
| 279 return file_name.endswith(".cc") or file_name.endswith(".h") | 276 '.cc', |
| 277 '.m', |
| 278 '.mm', |
| 279 ] |
| 280 basename, extension = os.path.splitext(file_name) |
| 281 return extension in checked_extensions |
| 280 | 282 |
| 281 | 283 |
| 282 def CheckLine(rules, line): | 284 def CheckLine(rules, line): |
| 283 """Checks the given file with the given rule set. If the line is an #include | 285 """Checks the given file with the given rule set. If the line is an #include |
| 284 directive and is illegal, a string describing the error will be returned. | 286 directive and is illegal, a string describing the error will be returned. |
| 285 Otherwise, None will be returned.""" | 287 Otherwise, None will be returned.""" |
| 286 if line[0:8] != "#include": | 288 found_item = EXTRACT_INCLUDE_PATH.match(line) |
| 287 return None # Not an include line | |
| 288 | |
| 289 found_item = EXTRACT_INCLUDE_FILENAME.match(line) | |
| 290 if not found_item: | 289 if not found_item: |
| 291 return None # Not a match | 290 return None # Not a match |
| 292 | 291 |
| 293 include_path = found_item.group(1) | 292 include_path = found_item.group(1) |
| 294 | 293 |
| 295 # Fix up backslashes in case somebody accidentally used them. | 294 # Fix up backslashes in case somebody accidentally used them. |
| 296 include_path.replace("\\", "/") | 295 include_path.replace("\\", "/") |
| 297 | 296 |
| 298 if include_path.find("/") < 0: | 297 if include_path.find("/") < 0: |
| 299 # Don't fail when no directory is specified. We may want to be more | 298 # Don't fail when no directory is specified. We may want to be more |
| (...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 459 if '__main__' == __name__: | 458 if '__main__' == __name__: |
| 460 option_parser = optparse.OptionParser() | 459 option_parser = optparse.OptionParser() |
| 461 option_parser.add_option("", "--root", default="", dest="base_directory", | 460 option_parser.add_option("", "--root", default="", dest="base_directory", |
| 462 help='Specifies the repository root. This defaults ' | 461 help='Specifies the repository root. This defaults ' |
| 463 'to "../../.." relative to the script file, which ' | 462 'to "../../.." relative to the script file, which ' |
| 464 'will normally be the repository root.') | 463 'will normally be the repository root.') |
| 465 option_parser.add_option("-v", "--verbose", action="store_true", | 464 option_parser.add_option("-v", "--verbose", action="store_true", |
| 466 default=False, help="Print debug logging") | 465 default=False, help="Print debug logging") |
| 467 options, args = option_parser.parse_args() | 466 options, args = option_parser.parse_args() |
| 468 main(options, args) | 467 main(options, args) |
| OLD | NEW |