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 15 matching lines...) Expand all Loading... |
26 | 26 |
27 | 27 |
28 def _IsTestFile(filename): | 28 def _IsTestFile(filename): |
29 """Does a rudimentary check to try to skip test files; this could be | 29 """Does a rudimentary check to try to skip test files; this could be |
30 improved but is good enough for now. | 30 improved but is good enough for now. |
31 """ | 31 """ |
32 return re.match('(test|mock|dummy)_.*|.*_[a-z]*test\.(cc|mm|java)', filename) | 32 return re.match('(test|mock|dummy)_.*|.*_[a-z]*test\.(cc|mm|java)', filename) |
33 | 33 |
34 | 34 |
35 class DepsChecker(DepsBuilder): | 35 class DepsChecker(DepsBuilder): |
36 """Parses include_rules from DEPS files and erifies 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 """Creates a new DepsChecker. | 46 """Creates a new DepsChecker. |
(...skipping 29 matching lines...) Expand all Loading... |
76 """ | 76 """ |
77 java = java_checker.JavaChecker(self.base_directory, self.verbose) | 77 java = java_checker.JavaChecker(self.base_directory, self.verbose) |
78 cpp = cpp_checker.CppChecker(self.verbose) | 78 cpp = cpp_checker.CppChecker(self.verbose) |
79 checkers = dict( | 79 checkers = dict( |
80 (extension, checker) | 80 (extension, checker) |
81 for checker in [java, cpp] for extension in checker.EXTENSIONS) | 81 for checker in [java, cpp] for extension in checker.EXTENSIONS) |
82 self._CheckDirectoryImpl(checkers, start_dir) | 82 self._CheckDirectoryImpl(checkers, start_dir) |
83 | 83 |
84 def _CheckDirectoryImpl(self, checkers, dir_name): | 84 def _CheckDirectoryImpl(self, checkers, dir_name): |
85 rules = self.GetDirectoryRules(dir_name) | 85 rules = self.GetDirectoryRules(dir_name) |
86 if rules == None: | 86 if rules is None: |
87 return | 87 return |
88 | 88 |
89 # Collect a list of all files and directories to check. | 89 # Collect a list of all files and directories to check. |
90 files_to_check = [] | 90 files_to_check = [] |
91 dirs_to_check = [] | 91 dirs_to_check = [] |
92 contents = sorted(os.listdir(dir_name)) | 92 contents = sorted(os.listdir(dir_name)) |
93 for cur in contents: | 93 for cur in contents: |
94 full_name = os.path.join(dir_name, cur) | 94 full_name = os.path.join(dir_name, cur) |
95 if os.path.isdir(full_name): | 95 if os.path.isdir(full_name): |
96 dirs_to_check.append(full_name) | 96 dirs_to_check.append(full_name) |
(...skipping 21 matching lines...) Expand all Loading... |
118 | 118 |
119 Return: | 119 Return: |
120 A list of tuples, (bad_file_path, rule_type, rule_description) | 120 A list of tuples, (bad_file_path, rule_type, rule_description) |
121 where rule_type is one of Rule.DISALLOW or Rule.TEMP_ALLOW and | 121 where rule_type is one of Rule.DISALLOW or Rule.TEMP_ALLOW and |
122 rule_description is human-readable. Empty if no problems. | 122 rule_description is human-readable. Empty if no problems. |
123 """ | 123 """ |
124 cpp = cpp_checker.CppChecker(self.verbose) | 124 cpp = cpp_checker.CppChecker(self.verbose) |
125 problems = [] | 125 problems = [] |
126 for file_path, include_lines in added_includes: | 126 for file_path, include_lines in added_includes: |
127 if not cpp.IsCppFile(file_path): | 127 if not cpp.IsCppFile(file_path): |
128 pass | 128 continue |
129 rules_for_file = self.GetDirectoryRules(os.path.dirname(file_path)) | 129 rules_for_file = self.GetDirectoryRules(os.path.dirname(file_path)) |
130 if rules_for_file: | 130 if not rules_for_file: |
131 for line in include_lines: | 131 continue |
132 is_include, violation = cpp.CheckLine( | 132 for line in include_lines: |
133 rules_for_file, line, file_path, True) | 133 is_include, violation = cpp.CheckLine( |
134 if violation: | 134 rules_for_file, line, file_path, True) |
135 rule_type = violation.violated_rule.allow | 135 if not violation: |
136 if rule_type != Rule.ALLOW: | 136 continue |
137 violation_text = results.NormalResultsFormatter.FormatViolation( | 137 rule_type = violation.violated_rule.allow |
138 violation, self.verbose) | 138 if rule_type == Rule.ALLOW: |
139 problems.append((file_path, rule_type, violation_text)) | 139 continue |
| 140 violation_text = results.NormalResultsFormatter.FormatViolation( |
| 141 violation, self.verbose) |
| 142 problems.append((file_path, rule_type, violation_text)) |
140 return problems | 143 return problems |
141 | 144 |
142 | 145 |
143 def PrintUsage(): | 146 def PrintUsage(): |
144 print """Usage: python checkdeps.py [--root <root>] [tocheck] | 147 print """Usage: python checkdeps.py [--root <root>] [tocheck] |
145 | 148 |
146 --root ROOT Specifies the repository root. This defaults to "../../.." | 149 --root ROOT Specifies the repository root. This defaults to "../../.." |
147 relative to the script file. This will be correct given the | 150 relative to the script file. This will be correct given the |
148 normal location of the script in "<root>/tools/checkdeps". | 151 normal location of the script in "<root>/tools/checkdeps". |
149 | 152 |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
225 if options.json: | 228 if options.json: |
226 deps_checker.results_formatter = results.JSONResultsFormatter( | 229 deps_checker.results_formatter = results.JSONResultsFormatter( |
227 options.json, deps_checker.results_formatter) | 230 options.json, deps_checker.results_formatter) |
228 | 231 |
229 deps_checker.CheckDirectory(start_dir) | 232 deps_checker.CheckDirectory(start_dir) |
230 return deps_checker.Report() | 233 return deps_checker.Report() |
231 | 234 |
232 | 235 |
233 if '__main__' == __name__: | 236 if '__main__' == __name__: |
234 sys.exit(main()) | 237 sys.exit(main()) |
OLD | NEW |