| OLD | NEW |
| 1 # Copyright 2013 The Chromium Authors. All rights reserved. | 1 # Copyright 2013 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 """Module scan and load system. | 5 """Module scan and load system. |
| 6 | 6 |
| 7 The main interface to this module is the Scan function, which triggers a | 7 The main interface to this module is the Scan function, which triggers a |
| 8 recursive scan of all packages and modules below cr, with modules being | 8 recursive scan of all packages and modules below cr, with modules being |
| 9 imported as they are found. | 9 imported as they are found. |
| 10 This allows all the plugins in the system to self register. | 10 This allows all the plugins in the system to self register. |
| (...skipping 28 matching lines...) Expand all Loading... |
| 39 | 39 |
| 40 | 40 |
| 41 def _Import(name): | 41 def _Import(name): |
| 42 """Import a module or package if it is not already imported.""" | 42 """Import a module or package if it is not already imported.""" |
| 43 module = sys.modules.get(name, None) | 43 module = sys.modules.get(name, None) |
| 44 if module is not None: | 44 if module is not None: |
| 45 return module | 45 return module |
| 46 return import_module(name, None) | 46 return import_module(name, None) |
| 47 | 47 |
| 48 | 48 |
| 49 def _TryImport(name): |
| 50 """Try to import a module or package if it is not already imported.""" |
| 51 try: |
| 52 return _Import(name) |
| 53 except ImportError: |
| 54 if cr.context.verbose: |
| 55 print 'Warning: Failed to load module', name |
| 56 return None |
| 57 |
| 58 |
| 49 def _ScanModule(module): | 59 def _ScanModule(module): |
| 50 """Runs all the scan_hooks for a module.""" | 60 """Runs all the scan_hooks for a module.""" |
| 51 scanner_tags = getattr(module, _MODULE_SCANNED_TAG, None) | 61 scanner_tags = getattr(module, _MODULE_SCANNED_TAG, None) |
| 52 if scanner_tags is None: | 62 if scanner_tags is None: |
| 53 # First scan, add the scanned marker set. | 63 # First scan, add the scanned marker set. |
| 54 scanner_tags = set() | 64 scanner_tags = set() |
| 55 setattr(module, _MODULE_SCANNED_TAG, scanner_tags) | 65 setattr(module, _MODULE_SCANNED_TAG, scanner_tags) |
| 56 for scan in scan_hooks: | 66 for scan in scan_hooks: |
| 57 if scan not in scanner_tags: | 67 if scan not in scanner_tags: |
| 58 scanner_tags.add(scan) | 68 scanner_tags.add(scan) |
| (...skipping 10 matching lines...) Expand all Loading... |
| 69 except OSError: | 79 except OSError: |
| 70 basenames = [] | 80 basenames = [] |
| 71 packages = [] | 81 packages = [] |
| 72 for basename in basenames: | 82 for basename in basenames: |
| 73 fullpath = os.path.join(path, basename) | 83 fullpath = os.path.join(path, basename) |
| 74 if os.path.isdir(fullpath): | 84 if os.path.isdir(fullpath): |
| 75 name = '.'.join([package.__name__, basename]) | 85 name = '.'.join([package.__name__, basename]) |
| 76 packages.append(name) | 86 packages.append(name) |
| 77 elif basename.endswith('.py') and not basename.startswith('_'): | 87 elif basename.endswith('.py') and not basename.startswith('_'): |
| 78 name = '.'.join([package.__name__, basename[:-3]]) | 88 name = '.'.join([package.__name__, basename[:-3]]) |
| 79 module = _Import(name) | 89 module = _TryImport(name) |
| 80 _ScanModule(module) | 90 if module: |
| 81 modules.append(module) | 91 _ScanModule(module) |
| 92 modules.append(module) |
| 82 for name in packages: | 93 for name in packages: |
| 83 child = _Import(name) | 94 child = _TryImport(name) |
| 84 modules.extend(_ScanPackage(child)) | 95 if child: |
| 96 modules.extend(_ScanPackage(child)) |
| 85 return modules | 97 return modules |
| 86 | 98 |
| 87 | 99 |
| 88 def Import(package, name): | 100 def Import(package, name): |
| 89 module = _Import(package + '.' + name) | 101 module = _Import(package + '.' + name) |
| 90 path = getattr(module, '__path__', None) | 102 path = getattr(module, '__path__', None) |
| 91 if path: | 103 if path: |
| 92 _ScanPackage(module) | 104 _ScanPackage(module) |
| 93 else: | 105 else: |
| 94 _ScanModule(module) | 106 _ScanModule(module) |
| (...skipping 10 matching lines...) Expand all Loading... |
| 105 namespace directly. | 117 namespace directly. |
| 106 | 118 |
| 107 Modules are allowed to refer to each other, their import will be retried | 119 Modules are allowed to refer to each other, their import will be retried |
| 108 until it succeeds or no progress can be made on any module. | 120 until it succeeds or no progress can be made on any module. |
| 109 """ | 121 """ |
| 110 modules = _ScanPackage(cr) | 122 modules = _ScanPackage(cr) |
| 111 # Now scan all the found modules one more time. | 123 # Now scan all the found modules one more time. |
| 112 # This happens after all imports, in case any imports register scan hooks. | 124 # This happens after all imports, in case any imports register scan hooks. |
| 113 for module in modules: | 125 for module in modules: |
| 114 _ScanModule(module) | 126 _ScanModule(module) |
| OLD | NEW |