master: make is_stdlib_path() a free function

For Ansible module loader work.
pull/245/head
David Wilson 7 years ago
parent 2ad0d0521d
commit e615265ebd

@ -319,21 +319,19 @@ class LogForwarder(object):
return 'LogForwarder(%r)' % (self._router,)
class ModuleFinder(object):
_STDLIB_PATHS = _stdlib_paths()
def __init__(self):
#: Import machinery is expensive, keep :py:meth`:get_module_source`
#: results around.
self._found_cache = {}
#: Avoid repeated dependency scanning, which is expensive.
self._related_cache = {}
def is_stdlib_path(path):
return any(
os.path.commonprefix((libpath, path)) == libpath
and 'site-packages' not in path
and 'dist-packages' not in path
for libpath in _STDLIB_PATHS
)
def __repr__(self):
return 'ModuleFinder()'
def is_stdlib_name(self, modname):
def is_stdlib_name(modname):
"""Return ``True`` if `modname` appears to come from the standard
library."""
if imp.is_builtin(modname) != 0:
@ -345,14 +343,20 @@ class ModuleFinder(object):
# six installs crap with no __file__
modpath = os.path.abspath(getattr(module, '__file__', ''))
if 'site-packages' in modpath:
return False
return is_stdlib_path(modpath)
for dirname in self._STDLIB_PATHS:
if os.path.commonprefix((dirname, modpath)) == dirname:
return True
return False
class ModuleFinder(object):
def __init__(self):
#: Import machinery is expensive, keep :py:meth`:get_module_source`
#: results around.
self._found_cache = {}
#: Avoid repeated dependency scanning, which is expensive.
self._related_cache = {}
def __repr__(self):
return 'ModuleFinder()'
def _looks_like_script(self, path):
"""
@ -515,7 +519,7 @@ class ModuleFinder(object):
name
for name in maybe_names
if sys.modules.get(name) is not None
and not self.is_stdlib_name(name)
and not is_stdlib_name(name)
and 'six.moves' not in name # TODO: crap
)
))

Loading…
Cancel
Save