本文整理汇总了Python中modulefinder.ModuleFinder类的典型用法代码示例。如果您正苦于以下问题:Python ModuleFinder类的具体用法?Python ModuleFinder怎么用?Python ModuleFinder使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了ModuleFinder类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: add_mocks
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
示例2: basic_module_find
def basic_module_find(files):
'''Do a basic module find on all files'''
mf = ModuleFinder()
for fname in files:
mf.run_script(fname)
return mf
示例3: __init__
def __init__(self, path=None, debug=0, excludes=[], replace_paths=[], **kwargs):
ModuleFinder.__init__(self, path, debug, excludes, replace_paths)
self.loadedModules = []
self.moduleTypes = kwargs.pop("types", (imp.PY_SOURCE, imp.PY_COMPILED))
示例4: spider
def spider(self):
print '-' * 40
while len(self.full_path) != 0:
full_path = self.full_path.pop()
self.full_path_discard.append(full_path)
finder = ModuleFinder()
finder.run_script(full_path)
for name, mod in finder.modules.iteritems():
full_path = str(mod.__file__).replace('.', '/')
if full_path not in self.full_path_discard:
if full_path.startswith('/home/alfred/pipline/code/'):
self.result.append(name.replace('.', '/'))
if not full_path.startswith('/home/alfred/pipline/code/U'):
self.full_path.append(full_path)
else:
self.full_path_discard.append(full_path)
names = finder.any_missing()
for name in names:
for folder in self.all_path:
try:
full_path = folder + name + '.py'
if os.path.isfile(full_path):
if full_path not in self.full_path_discard:
self.full_path.append(full_path)
self.result.append(full_path)
except:
pass
for item in sorted(set(self.result)):
print item
print '='*100
示例5: GetImports
def GetImports(cls, directory, out_filters=[]):
'''
Lists imports made by python files found in the given directory.
Directory will be scanned recursively
:param str directory:
Path to a directory
:param list(str) out_filters:
List of filename filters
.. see:: FindFiles
:rtype: list(str)
:returns:
List of module imported by python
'''
from ben10.filesystem import FindFiles
from modulefinder import ModuleFinder as Finder
finder = Finder(directory)
for py_filename in FindFiles(directory, in_filters='*.py', out_filters=out_filters):
finder.run_script(py_filename)
return sorted(finder.modules.keys() + finder.badmodules.keys())
示例6: find_failed_imports
def find_failed_imports(module_path):
"""Returns all modules that failed to import when loading a particular
module."""
finder = ModuleFinder()
finder.run_script(module_path)
bad_modules = dict(finder.badmodules)
return return_missing_base_modules(bad_modules)
示例7: get_python_module_partials
def get_python_module_partials(
pathToModuleFile,
log):
"""
*Get the names of the _partials imported into dryx python modules.*
**Key Arguments:**
- ``pathToModuleFile`` -- the path to the python module we wish to extract the _partial names for
- ``log`` -- logger
**Return:**
- ``partialsDictionary`` -- a dictionary of the _partial names imported into the dryx python module, and a list of their functions
.. todo::
- [ ] when complete, clean get_python_module_partials function & add logging
"""
################ > IMPORTS ################
## STANDARD LIB ##
from modulefinder import ModuleFinder
import re
import os
import sys
## THIRD PARTY ##
## LOCAL APPLICATION ##
log.debug('starting the ``get_python_module_partials`` function')
## VARIABLES ##
partialsDictionary = {}
finder = ModuleFinder()
finder.run_script(pathToModuleFile)
baseName = os.path.basename(pathToModuleFile).replace(".py", "")
if baseName == "__init__":
pathToModuleFile = pathToModuleFile.replace("__init__.py", "")
baseName = os.path.basename(pathToModuleFile)
reBaseName = re.compile(r"%s" % (baseName,))
for name, mod in finder.modules.iteritems():
if reBaseName.search(name):
importList = []
for key in mod.globalnames.keys():
# if ("/Library/Frameworks/Python.framework/" in mod.__file__ or
# "macports" in mod.__file__) and "site-packages" not in
# mod.__file__:
if "/Users/" not in mod.__file__:
# print "skipping %s" % (mod.__file__,)
continue
importList.append(key)
if len(importList):
# print mod.__file__, importList
partialsDictionary[name] = importList
log.debug('completed the ``get_python_module_partials`` function')
return partialsDictionary
示例8: zip_std_lib
def zip_std_lib(src_module, dst_file):
"""Compiles the Python standard library modules used by the source module
and outputs to zip file."""
finder = ModuleFinder()
finder.run_script(src_module)
modules = set()
print('Writing dependencies to "%s"...' % DEP_OUT)
with open(DEP_OUT, "w") as f:
for name, mod in finder.modules.items():
print("%s: " % name, end="", file=f)
print(mod.__file__, file=f)
if mod.__file__ is None:
continue
path = os.path.realpath(mod.__file__)
if not path.startswith(os.path.normpath(STD_LIB)):
continue
while os.path.dirname(path) != os.path.normpath(STD_LIB):
path = os.path.dirname(path)
if os.path.isfile(path):
modules.add(path)
elif os.path.isdir(path):
for root, dirs, files in os.walk(path):
for i in files:
modules.add(os.path.join(root, i))
print("-" * 50, file=f)
print("### Modules NOT imported ###", file=f)
print("\n".join(finder.badmodules.keys()), file=f)
modules = sorted(
[i for i in modules if i.endswith((".py", ".pyc")) and not os.path.dirname(i).endswith("__pycache__")]
)
print('Writing standard library to "%s"...' % dst_file)
with zipfile.ZipFile(dst_file, "w", compression=zipfile.ZIP_DEFLATED) as z:
for i in modules:
root, ext = os.path.splitext(i)
if ext == ".py":
arcname = os.path.relpath(root, STD_LIB) + ".pyc"
pyc = create_pyc(i)
else:
arcname = os.path.relpath(i, STD_LIB)
with open(i, "rb") as f:
pyc = f.read()
z.writestr(arcname, pyc)
示例9: find_failed_imports_by_directory
def find_failed_imports_by_directory(directory):
"""Returns all modules that failed to import when loading all modules below
a directory."""
finder = ModuleFinder()
py_files = _find_all_python_modules(directory)
for f in py_files:
finder.run_script(f)
bad_modules = dict(finder.badmodules)
return return_missing_base_modules(bad_modules)
示例10: get_modules
def get_modules(filename):
finder = ModuleFinder()
finder.run_script(filename)
list_modules = {}
for name,mod in finder.modules.iteritems():
print name,mod
return list_modules
示例11: patched
def patched(self, project_dir, spec, sack):
files = list(project_dir.glob('*.py'))
files.extend(list(project_dir.glob('*/*.py')))
mod = ModuleFinder()
for item in files:
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__)
示例12: patched
def patched(self, project_dir, spec, sack):
mod = ModuleFinder()
for item in list(project_dir.glob('**/*.py')):
try:
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))
示例13: main
def main(argv):
path = sys.path[:]
path[0] = os.path.dirname(argv[0])
mf = ModuleFinder(path)
for f in argv:
mf.run_script(f)
paths = sorted(list(set([os.path.abspath(x.__file__) for x in mf.modules.values() if x.__file__])))
cwd = os.getcwd()
paths = [x for x in paths if x.startswith(cwd)]
m = len(cwd) + 1
paths = argv + [x[m:] for x in paths]
print ' '.join(paths)
示例14: get_dependencies
def get_dependencies(module):
from modulefinder import ModuleFinder
import os
finder = ModuleFinder()
finder.run_script(module)
all_module_paths = [os.path.abspath(m.__file__) for
m in finder.modules.values() if m.__file__ is not None]
def is_in_same_path(p):
return p and os.path.dirname(p).startswith(os.path.dirname(module))
return [x for x in all_module_paths if is_in_same_path(x) and x != module]
示例15: myMethod
def myMethod(self):
finder = ModuleFinder()
myDrive = "g:\\"
mySrcPath = "git\\6700Spring16\\CA05\\submissions\\danieljames_1246453_74826857_jhd0008\\softwareProcess\\SoftwareProcess\\Assignment\\prod"
myPath = "git\\6700Spring16\\CA05\\submissions\\danieljames_1246453_74826857_jhd0008\\softwareProcess\\SoftwareProcess\\Assignment\\test"
#myPath = "git\\6700Spring16\\CA05\\submissions"
scriptname = myDrive+myPath+'\\FixTest.py'
with io.open(scriptname) as scriptfile:
code = compile(scriptfile.readall(),scriptname,'exec')
mod = importlib.import_module('FixTest',package="Assignment")
print 'Inspect output:'
for i in inspect.getmembers((mod, inspect.ismodule)):
print i[0]
for root, myDir, files in os.walk(myDrive + myPath):
for myFile in files:
print myFile
sys.path.insert(0,os.path.join(myDrive+myPath))
print sys.path
finder.run_script(os.path.join(myDrive+mySrcPath+'\\Fix.py'))
print 'Loaded modules:'
for name, mod in finder.modules.iteritems():
print '%s: ' % name,
print ','.join(mod.globalnames.keys()[:3])
runpy.run_path(os.path.join(myDrive,myPath,'FixTest.py'))
__all__ = []
module_name = None
for loader, module_name, is_pkg in pkgutil.walk_packages("g:\\"):
print 'in for loop'
#__all__.append(module_name)
#module = loader.find_module(module_name).load_module(module_name)
print 'Found submodule %s ' % module_name
print module_name
'''
src_data = pkgutil.get_data('Assignment.prod',"Fix.py")
if src_data == None:
print "No source data"
else:
print src_data
#mod = loader.load_module(myDrive+myPath+"Fix.py")
myString = "diff --git a/rtb0006/CA01/prod/__init__.pyc b/rtb0006/CA01/prod/__init__.pyc"
testSuite = TestLoader().discover("g:\\git\\6700Spring16\\CA05\\submissions\\danieljames_1246453_74826857_jhd0008\\softwareProcess\\SoftwareProcess\\Assignment\\test", pattern="*.py")
ImpImporter("g:\\git\\6700Spring16\\CA05\\submissions\\danieljames_1246453_74826857_jhd00085\\softwareProcess\\SoftwareProcess\\Assignment\\prod")
print testSuite.countTestCases()
for p in sys.path:
print p
result = TextTestRunner().run(testSuite)
print result.testsRun
'''
'''