| OLD | NEW |
| 1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 # Copyright (c) 2012 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 """Checks Java files for illegal imports.""" | 5 """Checks Java files for illegal imports.""" |
| 6 | 6 |
| 7 import codecs | 7 import codecs |
| 8 import os | 8 import os |
| 9 import re | 9 import re |
| 10 | 10 |
| (...skipping 21 matching lines...) Expand all Loading... |
| 32 """ | 32 """ |
| 33 | 33 |
| 34 EXTENSIONS = ['.java'] | 34 EXTENSIONS = ['.java'] |
| 35 | 35 |
| 36 def __init__(self, base_directory, verbose): | 36 def __init__(self, base_directory, verbose): |
| 37 self._base_directory = base_directory | 37 self._base_directory = base_directory |
| 38 self._verbose = verbose | 38 self._verbose = verbose |
| 39 self._classmap = {} | 39 self._classmap = {} |
| 40 self._PrescanFiles() | 40 self._PrescanFiles() |
| 41 | 41 |
| 42 def _IgnoreDir(self, d): |
| 43 # Skip hidden directories. |
| 44 if d.startswith('.'): |
| 45 return True |
| 46 # Skip the "out" directory, as dealing with generated files is awkward. |
| 47 # We don't want paths like "out/Release/lib.java" in our DEPS files. |
| 48 # TODO(husky): We need some way of determining the "real" path to |
| 49 # a generated file -- i.e., where it would be in source control if |
| 50 # it weren't generated. |
| 51 if d == 'out': |
| 52 return True |
| 53 # Skip third-party directories. |
| 54 if d in ('third_party', 'ThirdParty'): |
| 55 return True |
| 56 return False |
| 57 |
| 42 def _PrescanFiles(self): | 58 def _PrescanFiles(self): |
| 43 for root, dirs, files in os.walk(self._base_directory): | 59 for root, dirs, files in os.walk(self._base_directory): |
| 44 # Skip unwanted subdirectories. TODO(husky): it would be better to do | 60 # Skip unwanted subdirectories. TODO(husky): it would be better to do |
| 45 # this via the skip_child_includes flag in DEPS files. Maybe hoist this | 61 # this via the skip_child_includes flag in DEPS files. Maybe hoist this |
| 46 # prescan logic into checkdeps.py itself? | 62 # prescan logic into checkdeps.py itself? |
| 47 for d in dirs: | 63 dirs[:] = [d for d in dirs if not self._IgnoreDir(d)] |
| 48 # Skip hidden directories. | |
| 49 if d.startswith('.'): | |
| 50 dirs.remove(d) | |
| 51 # Skip the "out" directory, as dealing with generated files is awkward. | |
| 52 # We don't want paths like "out/Release/lib.java" in our DEPS files. | |
| 53 # TODO(husky): We need some way of determining the "real" path to | |
| 54 # a generated file -- i.e., where it would be in source control if | |
| 55 # it weren't generated. | |
| 56 if d == 'out': | |
| 57 dirs.remove(d) | |
| 58 # Skip third-party directories. | |
| 59 if d in ('third_party', 'ThirdParty'): | |
| 60 dirs.remove(d) | |
| 61 for f in files: | 64 for f in files: |
| 62 if f.endswith('.java'): | 65 if f.endswith('.java'): |
| 63 self._PrescanFile(os.path.join(root, f)) | 66 self._PrescanFile(os.path.join(root, f)) |
| 64 | 67 |
| 65 def _PrescanFile(self, filepath): | 68 def _PrescanFile(self, filepath): |
| 66 if self._verbose: | 69 if self._verbose: |
| 67 print 'Prescanning: ' + filepath | 70 print 'Prescanning: ' + filepath |
| 68 with codecs.open(filepath, encoding='utf-8') as f: | 71 with codecs.open(filepath, encoding='utf-8') as f: |
| 69 short_class_name, _ = os.path.splitext(os.path.basename(filepath)) | 72 short_class_name, _ = os.path.splitext(os.path.basename(filepath)) |
| 70 for line in f: | 73 for line in f: |
| (...skipping 27 matching lines...) Expand all Loading... |
| 98 include_path = include_path.replace(os.path.sep, '/') | 101 include_path = include_path.replace(os.path.sep, '/') |
| 99 rule = rules.RuleApplyingTo(include_path, filepath) | 102 rule = rules.RuleApplyingTo(include_path, filepath) |
| 100 if rule.allow == Rule.DISALLOW: | 103 if rule.allow == Rule.DISALLOW: |
| 101 dependee_status.AddViolation( | 104 dependee_status.AddViolation( |
| 102 results.DependencyViolation(include_path, rule, rules)) | 105 results.DependencyViolation(include_path, rule, rules)) |
| 103 if '{' in line: | 106 if '{' in line: |
| 104 # This is code, so we're finished reading imports for this file. | 107 # This is code, so we're finished reading imports for this file. |
| 105 break | 108 break |
| 106 | 109 |
| 107 return dependee_status | 110 return dependee_status |
| OLD | NEW |