def find_packages(setup_py): packages = [] # All this horrid hackery to recover the install_requires parameter from # a setup() call in setup.py. # # https://stackoverflow.com/questions/24236266/how-to-extract-dependencies-information-from-a-setup-py try: # Patch setuptools to intercept the setup() call with mock.patch.object(setuptools, 'setup') as setup_mock: # Get an open file handle and a description of the # setup file. setup_file, setup_filename, setup_description = imp.find_module('setup', [os.path.dirname(setup_py)]) # Load setup.py as the module setup. We have to # intercept calls to find_packages as well since # find_packages will run a 'find'-like operation from # the current working directory - which is Bad if the # CWD is the root directory... with mock.patch.object(setuptools, 'find_packages'): imp.load_module('setup', setup_file, setup_filename, setup_description) # Grab the call args to setup _, setup_kwargs = setup_mock.call_args # ...and recover the install_requires parameter. Fun, eh? # Don't forget to remove trailing version specifiers that # lack version numbers. packages = ['{}'.format(p.rstrip('<>=')) for p in setup_kwargs['install_requires']] finally: # As warned in the docs, we have to close the setup file # ourselves. if setup_file is not None: setup_file.close() return packages
def ge_sources(): plugins = [] possible_sources = os.listdir(sources_folder) for i in possible_sources: location = os.path.join(sources_folder, i) info = importlib.find_module(main_module, [location]) plugins.append({"name": i, "info": info}) return plugins
def import_module(name): # attempt to fix #326 - load modules dot by dot path = sys.path for part in name.split('.'): fp, pathname, description = importlib.find_module(part, path) module = importlib.load_module(name, fp, pathname, description) path = module.__path__ return module
def _runPlugin(self, filepath): log.debug("RUN plugin from %s" % filepath) module_name = os.path.splitext(os.path.basename(filepath))[0] f, filename, description = importlib.find_module( module_name, [os.path.dirname(filepath)]) print(f, filename, description) mod = importlib.load_module(module_name, f, filename, description) mod.PluginEntry(self.session)
def _runPlugin(self, filepath): androconf.debug("RUN plugin from %s" % filepath) module_name = os.path.splitext(os.path.basename(filepath))[0] f, filename, description = importlib.find_module( module_name, [os.path.dirname(filepath)]) print(f, filename, description) mod = importlib.load_module(module_name, f, filename, description) mod.PluginEntry(self.session)
def _get_modpkg_path(dotted_name, pathlist=None): """Get the filesystem path for a module or a package. Return the file system path to a file for a module, and to a directory for a package. Return None if the name is not found, or is a builtin or extension module. """ # split off top-most name parts = dotted_name.split('.', 1) if len(parts) > 1: # we have a dotted path, import top-level package try: file, pathname, description = importlib.util.find_spec( parts[0], pathlist) if file: file.close() except ImportError: return None # check if it's indeed a package if description[2] == importlib.PKG_DIRECTORY: # recursively handle the remaining name parts pathname = _get_modpkg_path(parts[1], [pathname]) else: pathname = None else: # plain name try: file, pathname, description = importlib.find_module( dotted_name, pathlist) if file: file.close() if description[2] not in [ importlib.PY_SOURCE, importlib.PKG_DIRECTORY ]: pathname = None except ImportError: pathname = None return pathname
def add_routes(self, app, module_name): logging.info('开始加载Router') n = module_name.find('.') if n == (-1): mifo = importlib.machinery.PathFinder.find_module(module_name) _tpmod = mifo.load_module() else: mifo = importlib.find_module(module_name[n:]) _tpmod = mifo.load_module() # 找出继承自ControllerBase的类。 # 文件必须以Controller结尾。 controllerDir = os.path.abspath(_tpmod.__file__) if not os.path.isfile( os.path.abspath(_tpmod.__file__)) else os.path.dirname( os.path.abspath(_tpmod.__file__)) controllers = [sf for p, sd, sf in os.walk(controllerDir)] filter = lambda x: not isinstance( x, list) and x != 'BaseController.py' and x.endswith( "Controller.py") while len([p for p in controllers if isinstance(p, list)]) > 0: controllers = UtilKit.des_array(controllers, filter) controllers = map(lambda o: o[0:-3], controllers) logging.info(controllers) for cm in controllers: logging.info("开始加载Module:%s" % cm) _mdp = importlib.machinery.PathFinder.find_module(cm) if not _mdp: raise Exception("该Module不存在或不在工作目录!") md = _mdp.load_module() for cl in dir(md): _ctr = getattr(md, cl, None) if inspect.isclass(_ctr) and BaseController in _ctr.__bases__: ctr = _ctr() logging.info('发现Controller:%s' % cl) for ac in dir(ctr): oac = getattr(ctr, ac, None) method = getattr(oac, '__method__', None) path = getattr(oac, '__route__', None) if method and path: logging.info('记录action:%s' % oac.__name__) logging.info('path:%s' % path) app.router.add_route(method, path, oac)
def make_python2_module(path_to_python): try: return sys.modules[name] except KeyError: try: fp, pathname, description = importlib.find_module( name, [path_to_python.parent]) except ImportError: try: fp, pathname, description = ( open(name), path_to_python, ('', 'r', importlib.PY_SOURCE)) except IOError: raise ImportError('Could not find a module for %r' % str(path_to_python)) # If any of the following calls raises an exception, # there's a problem we can't handle -- let the caller handle it. try: return importlib.load_module(name, fp, pathname, description) finally: if fp: fp.close()
def make_python2_module(path_to_python): try: return sys.modules[name] except KeyError: try: fp, pathname, description = importlib.find_module( name, [path_to_python.parent]) except ImportError: try: fp, pathname, description = (open(name), path_to_python, ('', 'r', importlib.PY_SOURCE)) except IOError: raise ImportError('Could not find a module for %r' % str(path_to_python)) # If any of the following calls raises an exception, # there's a problem we can't handle -- let the caller handle it. try: return importlib.load_module(name, fp, pathname, description) finally: if fp: fp.close()
def import_module(name): fp, pathname, description = importlib.find_module(name.replace('.', '/')) return importlib.load_module(name, fp, pathname, description)
print("Didnt got the OS Exiting .....") exit(1) if sys.version_info < (3.2): import imp try: imp.find_module(pxpect) from pexpect import * except ImportError: print("No pexpect Module is found") else: import importlib try: importlib.find_module(pxpect) from pexpect import * except ImportError: print("No pexpect Module is found") def process_arg(): from optparse import OptionParser usage = "Password_change.py -f Filename" parser = OptionParser(usage) parser.add_option("-f", dest="filename", help="File Name") return parser.parseargs(sys.argv) def exceptionCheck(row):
def import_module(name): fp, pathname, description = importlib.find_module(name) return importlib.load_module(name, fp, pathname, description)
def import_module(name): fp, pathname, description = importlib.find_module( name.replace('.', '/')) return importlib.load_module(name, fp, pathname, description)