Skip to content

Commit

Permalink
Ignore import errors in cr recursive module loading
Browse files Browse the repository at this point in the history
Without this change, cr throws an ImportError whenever it tries to
import a folder with Python containing other folders (without Python
code).

BUG=

Review URL: https://codereview.chromium.org/1155193003

Cr-Commit-Position: refs/heads/master@{#332650}
  • Loading branch information
petrcermak authored and Commit bot committed Jun 3, 2015
1 parent 12990e8 commit 9bf5fea
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions tools/cr/cr/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,16 @@ def _Import(name):
return import_module(name, None)


def _TryImport(name):
"""Try to import a module or package if it is not already imported."""
try:
return _Import(name)
except ImportError:
if cr.context.verbose:
print 'Warning: Failed to load module', name
return None


def _ScanModule(module):
"""Runs all the scan_hooks for a module."""
scanner_tags = getattr(module, _MODULE_SCANNED_TAG, None)
Expand Down Expand Up @@ -76,12 +86,14 @@ def _ScanPackage(package):
packages.append(name)
elif basename.endswith('.py') and not basename.startswith('_'):
name = '.'.join([package.__name__, basename[:-3]])
module = _Import(name)
_ScanModule(module)
modules.append(module)
module = _TryImport(name)
if module:
_ScanModule(module)
modules.append(module)
for name in packages:
child = _Import(name)
modules.extend(_ScanPackage(child))
child = _TryImport(name)
if child:
modules.extend(_ScanPackage(child))
return modules


Expand Down

0 comments on commit 9bf5fea

Please sign in to comment.