OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright 2012 The Chromium Authors. All rights reserved. | 2 # Copyright 2012 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" and "import" directives in the .cpp and .java source files. | 9 "#include" and "import" directives in the .cpp and .java source files. |
10 Any source file including something not permitted by the DEPS files will fail. | 10 Any source file including something not permitted by the DEPS files will fail. |
(...skipping 24 matching lines...) Expand all Loading... |
35 class DepsChecker(DepsBuilder): | 35 class DepsChecker(DepsBuilder): |
36 """Parses include_rules from DEPS files and verifies files in the | 36 """Parses include_rules from DEPS files and verifies files in the |
37 source tree against them. | 37 source tree against them. |
38 """ | 38 """ |
39 | 39 |
40 def __init__(self, | 40 def __init__(self, |
41 base_directory=None, | 41 base_directory=None, |
42 verbose=False, | 42 verbose=False, |
43 being_tested=False, | 43 being_tested=False, |
44 ignore_temp_rules=False, | 44 ignore_temp_rules=False, |
45 skip_tests=False): | 45 skip_tests=False, |
| 46 resolve_dotdot=False): |
46 """Creates a new DepsChecker. | 47 """Creates a new DepsChecker. |
47 | 48 |
48 Args: | 49 Args: |
49 base_directory: OS-compatible path to root of checkout, e.g. C:\chr\src. | 50 base_directory: OS-compatible path to root of checkout, e.g. C:\chr\src. |
50 verbose: Set to true for debug output. | 51 verbose: Set to true for debug output. |
51 being_tested: Set to true to ignore the DEPS file at tools/checkdeps/DEPS. | 52 being_tested: Set to true to ignore the DEPS file at tools/checkdeps/DEPS. |
52 ignore_temp_rules: Ignore rules that start with Rule.TEMP_ALLOW ("!"). | 53 ignore_temp_rules: Ignore rules that start with Rule.TEMP_ALLOW ("!"). |
53 """ | 54 """ |
54 DepsBuilder.__init__( | 55 DepsBuilder.__init__( |
55 self, base_directory, verbose, being_tested, ignore_temp_rules) | 56 self, base_directory, verbose, being_tested, ignore_temp_rules) |
56 | 57 |
57 self._skip_tests = skip_tests | 58 self._skip_tests = skip_tests |
| 59 self._resolve_dotdot = resolve_dotdot |
58 self.results_formatter = results.NormalResultsFormatter(verbose) | 60 self.results_formatter = results.NormalResultsFormatter(verbose) |
59 | 61 |
60 def Report(self): | 62 def Report(self): |
61 """Prints a report of results, and returns an exit code for the process.""" | 63 """Prints a report of results, and returns an exit code for the process.""" |
62 if self.results_formatter.GetResults(): | 64 if self.results_formatter.GetResults(): |
63 self.results_formatter.PrintResults() | 65 self.results_formatter.PrintResults() |
64 return 1 | 66 return 1 |
65 print '\nSUCCESS\n' | 67 print '\nSUCCESS\n' |
66 return 0 | 68 return 0 |
67 | 69 |
68 def CheckDirectory(self, start_dir): | 70 def CheckDirectory(self, start_dir): |
69 """Checks all relevant source files in the specified directory and | 71 """Checks all relevant source files in the specified directory and |
70 its subdirectories for compliance with DEPS rules throughout the | 72 its subdirectories for compliance with DEPS rules throughout the |
71 tree (starting at |self.base_directory|). |start_dir| must be a | 73 tree (starting at |self.base_directory|). |start_dir| must be a |
72 subdirectory of |self.base_directory|. | 74 subdirectory of |self.base_directory|. |
73 | 75 |
74 On completion, self.results_formatter has the results of | 76 On completion, self.results_formatter has the results of |
75 processing, and calling Report() will print a report of results. | 77 processing, and calling Report() will print a report of results. |
76 """ | 78 """ |
77 java = java_checker.JavaChecker(self.base_directory, self.verbose) | 79 java = java_checker.JavaChecker(self.base_directory, self.verbose) |
78 cpp = cpp_checker.CppChecker(self.verbose) | 80 cpp = cpp_checker.CppChecker( |
| 81 self.verbose, self._resolve_dotdot, self.base_directory) |
79 checkers = dict( | 82 checkers = dict( |
80 (extension, checker) | 83 (extension, checker) |
81 for checker in [java, cpp] for extension in checker.EXTENSIONS) | 84 for checker in [java, cpp] for extension in checker.EXTENSIONS) |
82 self._CheckDirectoryImpl(checkers, start_dir) | 85 self._CheckDirectoryImpl(checkers, start_dir) |
83 | 86 |
84 def _CheckDirectoryImpl(self, checkers, dir_name): | 87 def _CheckDirectoryImpl(self, checkers, dir_name): |
85 rules = self.GetDirectoryRules(dir_name) | 88 rules = self.GetDirectoryRules(dir_name) |
86 if rules is None: | 89 if rules is None: |
87 return | 90 return |
88 | 91 |
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
185 '', '--skip-tests', | 188 '', '--skip-tests', |
186 action='store_true', dest='skip_tests', default=False, | 189 action='store_true', dest='skip_tests', default=False, |
187 help='Skip checking test files (best effort).') | 190 help='Skip checking test files (best effort).') |
188 option_parser.add_option( | 191 option_parser.add_option( |
189 '-v', '--verbose', | 192 '-v', '--verbose', |
190 action='store_true', default=False, | 193 action='store_true', default=False, |
191 help='Print debug logging') | 194 help='Print debug logging') |
192 option_parser.add_option( | 195 option_parser.add_option( |
193 '', '--json', | 196 '', '--json', |
194 help='Path to JSON output file') | 197 help='Path to JSON output file') |
| 198 option_parser.add_option( |
| 199 '', '--resolve-dotdot', |
| 200 action='store_true', dest='resolve_dotdot', default=False, |
| 201 help='resolve leading ../ in include directive paths relative ' |
| 202 'to the file perfoming the inclusion.') |
| 203 |
195 options, args = option_parser.parse_args() | 204 options, args = option_parser.parse_args() |
196 | 205 |
197 deps_checker = DepsChecker(options.base_directory, | 206 deps_checker = DepsChecker(options.base_directory, |
198 verbose=options.verbose, | 207 verbose=options.verbose, |
199 ignore_temp_rules=options.ignore_temp_rules, | 208 ignore_temp_rules=options.ignore_temp_rules, |
200 skip_tests=options.skip_tests) | 209 skip_tests=options.skip_tests, |
| 210 resolve_dotdot=options.resolve_dotdot) |
201 base_directory = deps_checker.base_directory # Default if needed, normalized | 211 base_directory = deps_checker.base_directory # Default if needed, normalized |
202 | 212 |
203 # Figure out which directory we have to check. | 213 # Figure out which directory we have to check. |
204 start_dir = base_directory | 214 start_dir = base_directory |
205 if len(args) == 1: | 215 if len(args) == 1: |
206 # Directory specified. Start here. It's supposed to be relative to the | 216 # Directory specified. Start here. It's supposed to be relative to the |
207 # base directory. | 217 # base directory. |
208 start_dir = os.path.abspath(os.path.join(base_directory, args[0])) | 218 start_dir = os.path.abspath(os.path.join(base_directory, args[0])) |
209 elif len(args) >= 2 or (options.generate_temp_rules and | 219 elif len(args) >= 2 or (options.generate_temp_rules and |
210 options.count_violations): | 220 options.count_violations): |
(...skipping 17 matching lines...) Expand all Loading... |
228 if options.json: | 238 if options.json: |
229 deps_checker.results_formatter = results.JSONResultsFormatter( | 239 deps_checker.results_formatter = results.JSONResultsFormatter( |
230 options.json, deps_checker.results_formatter) | 240 options.json, deps_checker.results_formatter) |
231 | 241 |
232 deps_checker.CheckDirectory(start_dir) | 242 deps_checker.CheckDirectory(start_dir) |
233 return deps_checker.Report() | 243 return deps_checker.Report() |
234 | 244 |
235 | 245 |
236 if '__main__' == __name__: | 246 if '__main__' == __name__: |
237 sys.exit(main()) | 247 sys.exit(main()) |
OLD | NEW |