本文整理汇总了Python中modulefinder.ModuleFinder方法的典型用法代码示例。如果您正苦于以下问题:Python modulefinder.ModuleFinder方法的具体用法?Python modulefinder.ModuleFinder怎么用?Python modulefinder.ModuleFinder使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类modulefinder
的用法示例。
在下文中一共展示了modulefinder.ModuleFinder方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: import modulefinder [as 别名]
# 或者: from modulefinder import ModuleFinder [as 别名]
def __init__(self, *args, **kwargs):
# In the Python 2 stdlib, ModuleFinder is an old-style class that
# does not inherit from object, therefore we cannot use super().
modulefinder.ModuleFinder.__init__(self, *args, **kwargs)
logger.Loggable.__init__(self) # Enable logging via self.logger
# Mapping of module object to a set of its dependencies. We store the
# dependencies in a list instead of a set to preserve ordering. While
# not strictly necessary, it makes testing easier when the ordering
# of nodes in the graph can be relied on. However, we must ensure that
# each dependency is unique, so we check before adding each new
# dependency below. This should be a significant performance issue,
# as we only expect each module to have a relatively small number
# of dependencies (around 10s) so O(n) adding is acceptable.
self._module_deps = collections.defaultdict(list)
self._curr_caller = None
self._module_nodes = {}
示例2: import_hook
# 需要导入模块: import modulefinder [as 别名]
# 或者: from modulefinder import ModuleFinder [as 别名]
def import_hook(self, name, caller=None, *args, **kwargs):
"""
Hook called when ``caller`` imports module ``name``. Store off the
caller so we can use it later.
:param name: Name of the module being imported.
:type name: ``str``
:param caller: Module object that is importing ``name``.
:type caller: ``modulefinder.Module``
:param args: Other args are passed down to
``modulefinder.ModuleFinder.import_hook``
:param kwargs: Other kwargs are passed down to
``modulefinder.ModuleFinder.import_hook``
:return: Return value from ``modulefinder.ModuleFinder.import_hook``
"""
self._curr_caller = caller
return modulefinder.ModuleFinder.import_hook(
self, name, caller, *args, **kwargs
)
示例3: add_dependencies
# 需要导入模块: import modulefinder [as 别名]
# 或者: from modulefinder import ModuleFinder [as 别名]
def add_dependencies(module_dir, filename, modules):
finder = modulefinder.ModuleFinder()
finder.run_script(os.path.join(module_dir, filename))
for module in finder.modules.values():
# if the module comes from the module_dir then process it to get
# its dependencies.
if os.path.dirname(str(module.__file__)) == module_dir:
mod_filename = os.path.basename(module.__file__)
if mod_filename == filename:
continue
# if this module has not already been added then add it.
if (mod_filename.endswith('_types.py') or
(mod_filename.find('_types_v') != -1 and
mod_filename.endswith('.py'))):
if mod_filename not in modules:
# add the modules that this module is dependent on
add_dependencies(module_dir, mod_filename, modules)
modules.append(mod_filename)
示例4: _get_modules
# 需要导入模块: import modulefinder [as 别名]
# 或者: from modulefinder import ModuleFinder [as 别名]
def _get_modules(fn):
finder = modulefinder.ModuleFinder()
finder.run_script(fn)
all = []
for m in finder.modules.values():
if not isinstance(m, modulefinder.Module):
continue
if not m.__file__:
continue
# skip shared object files
if m.__file__.endswith('.so'):
continue
# skip mac system stuff...
# FIXME: would need to augment with other OS's system stuff
if m.__file__.startswith('/Library/Frameworks'):
continue
all.append(m)
return all
示例5: _do_test
# 需要导入模块: import modulefinder [as 别名]
# 或者: from modulefinder import ModuleFinder [as 别名]
def _do_test(self, info, report=False):
import_this, modules, missing, maybe_missing, source = info
create_package(source)
try:
mf = modulefinder.ModuleFinder(path=TEST_PATH)
mf.import_hook(import_this)
if report:
mf.report()
## # This wouldn't work in general when executed several times:
## opath = sys.path[:]
## sys.path = TEST_PATH
## try:
## __import__(import_this)
## except:
## import traceback; traceback.print_exc()
## sys.path = opath
## return
modules = set(modules)
found = set(mf.modules.keys())
more = list(found - modules)
less = list(modules - found)
# check if we found what we expected, not more, not less
self.assertEqual((more, less), ([], []))
# check for missing and maybe missing modules
bad, maybe = mf.any_missing_maybe()
self.assertEqual(bad, missing)
self.assertEqual(maybe, maybe_missing)
finally:
distutils.dir_util.remove_tree(TEST_DIR)
示例6: import_module
# 需要导入模块: import modulefinder [as 别名]
# 或者: from modulefinder import ModuleFinder [as 别名]
def import_module(self, *args, **kwargs):
"""
Called a bit further down the stack when a module is imported. Update
the internal mapping of module dependencies.
:param args: all args are passed down to
``modulefinder.ModuleFinder.import_module``
:param kwargs: all kwargs are passed down to
``modulefinder.ModuleFinder.import_module``
:return: Imported module object.
:rtype: ``modulefinder.Module``
"""
caller = self._curr_caller
self._curr_caller = None
mod = modulefinder.ModuleFinder.import_module(self, *args, **kwargs)
if (
(caller is not None)
and (mod is not None)
and _has_file(mod)
and mod not in self._module_deps[caller]
):
self.logger.debug(
"Adding %(mod)s to %(caller)s's dependencies",
{"mod": mod.__name__, "caller": caller.__name__},
)
self._module_deps[caller].append(mod)
return mod
示例7: mock_reload_env
# 需要导入模块: import modulefinder [as 别名]
# 或者: from modulefinder import ModuleFinder [as 别名]
def mock_reload_env():
"""Set up the mock environment for unit-testing the reloader module."""
# For unmodified files, just return a fake stat result with everything set
# to 0.
unmodified_stat_result = os.stat_result(0 for _ in range(10))
# For modified files, set the st_mtime to 1 day in the future. It doesn't
# matter that the modification time is in the future - just so long as it
# is greater than the current time. The reloader only checks st_mtime,
# which is the value at the 8th index passed to os.stat_result.
one_day_hence = time.time() + 24 * 60 * 60
modified_stat_result = os.stat_result(
0 if i != 8 else one_day_hence for i in range(10)
)
def mock_stat(filepath):
if filepath in mock_stat.modified_files:
return modified_stat_result
else:
return unmodified_stat_result
mock_stat.modified_files = set()
reloader_patch = "testplan.runnable.interactive.reloader.reload_module"
with mock.patch("modulefinder.ModuleFinder", new=MockModuleFinder), (
mock.patch(reloader_patch, side_effect=lambda module: module)
) as mock_reload, (mock.patch("os.stat", new=mock_stat)), (
mock.patch("sys.modules", new=MOCK_SYSMODULES)
):
# Despite mocking modulefinder.ModuleFinder above, we also need to
# swap out the real ModuleFinder with our mock one in the list of
# bases for the GraphModuleFinder.
reloader._GraphModuleFinder.__bases__ = (
MockModuleFinder,
logger.Loggable,
)
reload_obj = reloader.ModuleReloader()
yield reload_obj, mock_reload, mock_stat
示例8: add_mocks
# 需要导入模块: import modulefinder [as 别名]
# 或者: from modulefinder import ModuleFinder [as 别名]
def add_mocks(filename):
gp = mock.patch('ansible.module_utils.basic.get_platform').start()
gp.return_value = 'linux'
module_mock = mock.MagicMock()
mocks = []
for module_class in MODULE_CLASSES:
mocks.append(
mock.patch('ansible.module_utils.basic.AnsibleModule',
new=module_mock)
)
for m in mocks:
p = m.start()
p.side_effect = AnsibleModuleCallError()
finder = ModuleFinder()
try:
finder.run_script(filename)
except:
pass
sys_mock = mock.MagicMock()
sys_mock.__version__ = '0.0.0'
sys_mocks = []
for module, sources in finder.badmodules.items():
if module in sys.modules:
continue
if [s for s in sources if s[:7] in ['ansible', '__main_']]:
parts = module.split('.')
for i in range(len(parts)):
dotted = '.'.join(parts[:i+1])
sys.modules[dotted] = sys_mock
sys_mocks.append(dotted)
return module_mock, mocks, sys_mocks
示例9: _do_test
# 需要导入模块: import modulefinder [as 别名]
# 或者: from modulefinder import ModuleFinder [as 别名]
def _do_test(self, info, report=False, debug=0, replace_paths=[]):
import_this, modules, missing, maybe_missing, source = info
create_package(source)
try:
mf = modulefinder.ModuleFinder(path=TEST_PATH, debug=debug,
replace_paths=replace_paths)
mf.import_hook(import_this)
if report:
mf.report()
## # This wouldn't work in general when executed several times:
## opath = sys.path[:]
## sys.path = TEST_PATH
## try:
## __import__(import_this)
## except:
## import traceback; traceback.print_exc()
## sys.path = opath
## return
modules = sorted(set(modules))
found = sorted(mf.modules)
# check if we found what we expected, not more, not less
self.assertEqual(found, modules)
# check for missing and maybe missing modules
bad, maybe = mf.any_missing_maybe()
self.assertEqual(bad, missing)
self.assertEqual(maybe, maybe_missing)
finally:
shutil.rmtree(TEST_DIR)
示例10: _
# 需要导入模块: import modulefinder [as 别名]
# 或者: from modulefinder import ModuleFinder [as 别名]
def _(
module_name=each("test_apples", "apples"), rv=each(True, False),
):
module = ModuleInfo(ModuleFinder(), module_name, False)
assert is_test_module(module) == rv
示例11: patched
# 需要导入模块: import modulefinder [as 别名]
# 或者: from modulefinder import ModuleFinder [as 别名]
def patched(self, project_dir, spec, sack):
""" Find python dependencies """
for item in list(project_dir.glob('**/*.py')):
try:
mod = ModuleFinder()
mod.run_script(str(item))
for _, mod in mod.modules.items():
if mod.__file__ and mod.__file__.startswith("/usr/lib"):
spec.required_files.add(mod.__file__)
except ImportError as ie:
logging.warn("Exception was raised by ModuleFinder:\n" +
str(ie) + "\nOn file: " + str(item))
示例12: __init__
# 需要导入模块: import modulefinder [as 别名]
# 或者: from modulefinder import ModuleFinder [as 别名]
def __init__(self, baseDir, *args, **kw):
self.caller = None
self.deps = {}
self.missing = {}
self.baseDir = baseDir
modulefinder.ModuleFinder.__init__(self, *args, **kw)
示例13: scan_code
# 需要导入模块: import modulefinder [as 别名]
# 或者: from modulefinder import ModuleFinder [as 别名]
def scan_code(self, co, m):
# caller is the module doing the importing; remember it for when
# import_module is called further down the stack.
oldCaller = self.caller
self.caller = m.__file__
try:
return modulefinder.ModuleFinder.scan_code(self, co, m)
finally:
self.caller = oldCaller
示例14: import_module
# 需要导入模块: import modulefinder [as 别名]
# 或者: from modulefinder import ModuleFinder [as 别名]
def import_module(self, partname, fqname, parent):
m = modulefinder.ModuleFinder.import_module(self, partname, fqname,
parent)
if self.caller and m and m.__file__:
self.deps.setdefault(self.caller, set()).add(m.__file__)
return m
示例15: find_imported_packages
# 需要导入模块: import modulefinder [as 别名]
# 或者: from modulefinder import ModuleFinder [as 别名]
def find_imported_packages(filename, executable_path, debug=0, exclude_stdlib=True):
"""
Find all imported top-level packages for a given Python file.
We cannot assume that the version of Python being used to run Sumatra is the
same as that used to run the simulation/analysis. Therefore we need to run
all the dependency finding and version checking in a subprocess with the
correct version of Python.
"""
# Actually, we could check whether executable_path matches sys.executable, and
# then do it in this process. On the other hand, the dependency finding
# could run in parallel with the simulation (good for multicore): we could
# move setting of dependencies to after the simulation, rather than having it
# in record.register()
# HACK Added excludes=['jinja2.asyncsupport']
script = textwrap.dedent("""
from modulefinder import ModuleFinder
import sys, os
import distutils.sysconfig
stdlib_path = distutils.sysconfig.get_python_lib(standard_lib=True)
stdlib_paths = (stdlib_path,
os.path.join(stdlib_path, "plat-mac"),
os.path.join(stdlib_path, "plat-mac", "lib-scriptpackages"))
exclude_stdlib = %s
finder = ModuleFinder(path=sys.path, debug=%d, excludes=['jinja2.asyncsupport'])
try:
finder.run_script("%s")
except Exception as ex:
sys.stdout.write("Determining dependencies failed for some Python modules.")
top_level_packages = {}
for name, module in finder.modules.items():
if module.__path__ and "." not in name:
if not(exclude_stdlib and os.path.dirname(module.__path__[0]) in stdlib_paths):
top_level_packages[name] = module
sys.stdout.write("%s" + str(top_level_packages))""" % (exclude_stdlib, int(debug), filename, SENTINEL))
return run_script(executable_path, script)