def _load_eggs(ch, search_path): logger.debug("Loading eggs...") # Note that the following doesn't seem to support unicode search_path distributions, errors = working_set.find_plugins( pkg_resources.Environment(search_path) ) logger.debug("Found distributions: %s", str(distributions)) for dist in distributions: if dist not in working_set: logger.debug('Adding plugin %s from %s', dist, dist.location) working_set.add(dist) for dist, e in errors.iteritems(): logger.error("Error in distribution %s: %s", str(dist), str(e)) for entry in sorted(working_set.iter_entry_points(entry_point_name), key=lambda entry: entry.name): logger.debug('Loading %s from %s', entry.name, entry.dist.location) try: entry.load(require=True) except Exception, e: logger.exception("Error loading: %s", entry) else: logger.debug("Loaded module %s from %s:", entry.module_name, entry.dist.location)
def test_setup_requires_overrides_version_conflict(self): """ Regression test for issue #323. Ensures that a distribution's setup_requires requirements can still be installed and used locally even if a conflicting version of that requirement is already on the path. """ pr_state = pkg_resources.__getstate__() fake_dist = PRDistribution('does-not-matter', project_name='foobar', version='0.0') working_set.add(fake_dist) try: with contexts.tempdir() as temp_dir: test_pkg = create_setup_requires_package(temp_dir) test_setup_py = os.path.join(test_pkg, 'setup.py') with contexts.quiet() as (stdout, stderr): # Don't even need to install the package, just # running the setup.py at all is sufficient run_setup(test_setup_py, ['--name']) lines = stdout.readlines() assert len(lines) > 0 assert lines[-1].strip(), 'test_pkg' finally: pkg_resources.__setstate__(pr_state)
def __init__(self, buildout, name, options): if 'config_uri' not in options: raise BuildoutError( 'PasteDeploy config URI not set in the "config_uri" option', ) # Loading the distribution: distribution = options.pop('factory_distribution', None) if not distribution: raise BuildoutError( '"factory_distribution" option not set in [%s] to the package ' 'containing the PasteDeploy application factory' % name, ) if 'eggs' not in options: # If we don't set the "eggs" option, it will take the part name: options['eggs'] = '' ws = Eggs(buildout, name, options).working_set([distribution])[1] for dist in ws: working_set.add(dist) # Loading the variables in the PasteDeploy config: buildout_dir = buildout['buildout']['directory'] config_variables = self._get_config_variables( options['config_uri'], buildout_dir, name, ) options.update(config_variables)
def search(apps_paths=None, installed_apps=None): """ Searches in the given apps directories for Django apps with the entry point ``'django.apps'`` and adds them to the python path, if necesary. Returns a tuple with all installed and reusable applications. """ if Environment is not None and apps_paths is not None: # find every "distributions" in the given paths for reusable apps and # add them to the "working_set", effectively setting PYTHONPATH distributions, errors = working_set.find_plugins( Environment(apps_paths)) for dist in distributions: working_set.add(dist) for dist, e in errors.iteritems(): if isinstance(e, DistributionNotFound): raise ReusableAppsError('"%s": ("%s" not found)', dist, e) elif isinstance(e, VersionConflict): raise ReusableAppsError('"%s": (version conflict "%s")', dist, e) elif isinstance(e, UnknownExtra): raise ReusableAppsError('"%s": (unknown extra "%s")', dist, e) elif isinstance(e, ImportError): raise ReusableAppsError('"%s": (can\'t import "%s")', dist, e) else: raise ReusableAppsError('"%s": (error "%s")', dist, e) # look for entry points in all distributions of the current working set # (on the PYTHONPATH) and add matching modules to INSTALLED_APPS for entry in working_set.iter_entry_points('django.apps'): app_name = entry.module_name if app_name not in installed_apps and app_name not in REUSEABLE_APPS: REUSEABLE_APPS.append(entry.module_name) return installed_apps + tuple(REUSEABLE_APPS)
def test_setup_requires_overrides_version_conflict(self, use_setup_cfg): """ Regression test for distribution issue 323: https://bitbucket.org/tarek/distribute/issues/323 Ensures that a distribution's setup_requires requirements can still be installed and used locally even if a conflicting version of that requirement is already on the path. """ fake_dist = PRDistribution('does-not-matter', project_name='foobar', version='0.0') working_set.add(fake_dist) with contexts.save_pkg_resources_state(): with contexts.tempdir() as temp_dir: test_pkg = create_setup_requires_package( temp_dir, use_setup_cfg=use_setup_cfg) test_setup_py = os.path.join(test_pkg, 'setup.py') with contexts.quiet() as (stdout, stderr): # Don't even need to install the package, just # running the setup.py at all is sufficient run_setup(test_setup_py, [str('--name')]) lines = stdout.readlines() assert len(lines) > 0 assert lines[-1].strip() == 'test_pkg'
def _load_eggs(env, search_path, auto_enable=None): # Note that the following doesn't seem to support unicode search_path distributions, errors = working_set.find_plugins( pkg_resources.Environment(search_path)) for dist in distributions: env.log.debug('Adding plugin %s from %s', dist, dist.location) working_set.add(dist) def _log_error(item, e): if isinstance(e, DistributionNotFound): env.log.debug('Skipping "%s": ("%s" not found)', item, e) elif isinstance(e, VersionConflict): env.log.error('Skipping "%s": (version conflict "%s")', item, e) elif isinstance(e, UnknownExtra): env.log.error('Skipping "%s": (unknown extra "%s")', item, e) elif isinstance(e, ImportError): env.log.error('Skipping "%s": (can\'t import "%s")', item, e) else: env.log.error('Skipping "%s": (error "%s")', item, e) for dist, e in errors.iteritems(): _log_error(dist, e) for entry in working_set.iter_entry_points(entry_point_name): env.log.debug('Loading %s from %s', entry.name, entry.dist.location) try: entry.load(require=True) except (ImportError, DistributionNotFound, VersionConflict, UnknownExtra), e: _log_error(entry, e) else: if os.path.dirname(entry.dist.location) == auto_enable: _enable_plugin(env, entry.module_name)
def add_plugin_dir(plugin_dir): if os.path.isdir(plugin_dir): return None LOGGER.debug("Adding plugin directory: %r", plugin_dir) env = Environment([plugin_dir]) dists, errors = working_set.find_plugins(env) for dist in dists: LOGGER.debug("Adding distribution: %r", dist) working_set.add(dist) if errors: for dist, error in list(errors.items()): errmsg = None if isinstance(error, DistributionNotFound): req, = error.args errmsg = "%r not found" % req.project_name elif isinstance(error, VersionConflict): dist, req = error.args errmsg = "Version Conflict. Requested %s Found %s" % (req, dist) else: # FIXME: Are there other types of failures? errmsg = repr(error) LOGGER.error("Failed to load %s: %r", dist, errmsg) global plugin_directories plugin_directories.append(plugin_dir)
def search(apps_paths=None, installed_apps=None): """ Searches in the given apps directories for Django apps with the entry point ``'django.apps'`` and adds them to the python path, if necesary. Returns a tuple with all installed and reusable applications. """ if Environment is not None and apps_paths is not None: # find every "distributions" in the given paths for reusable apps and # add them to the "working_set", effectively setting PYTHONPATH distributions, errors = working_set.find_plugins(Environment(apps_paths)) for dist in distributions: working_set.add(dist) for dist, e in errors.iteritems(): if isinstance(e, DistributionNotFound): raise ReusableAppsError('"%s": ("%s" not found)', dist, e) elif isinstance(e, VersionConflict): raise ReusableAppsError('"%s": (version conflict "%s")', dist, e) elif isinstance(e, UnknownExtra): raise ReusableAppsError('"%s": (unknown extra "%s")', dist, e) elif isinstance(e, ImportError): raise ReusableAppsError('"%s": (can\'t import "%s")', dist, e) else: raise ReusableAppsError('"%s": (error "%s")', dist, e) # look for entry points in all distributions of the current working set # (on the PYTHONPATH) and add matching modules to INSTALLED_APPS for entry in working_set.iter_entry_points('django.apps'): app_name = entry.module_name if app_name not in installed_apps and app_name not in REUSEABLE_APPS: REUSEABLE_APPS.append(entry.module_name) return installed_apps + tuple(REUSEABLE_APPS)
def _load_eggs(ch, search_path): logger.debug("Loading eggs...") # Note that the following doesn't seem to support unicode search_path distributions, errors = working_set.find_plugins( pkg_resources.Environment(search_path)) logger.debug("Found distributions: %s", str(distributions)) for dist in distributions: if dist not in working_set: logger.debug('Adding plugin %s from %s', dist, dist.location) working_set.add(dist) for dist, e in errors.iteritems(): logger.error("Error in distribution %s: %s", str(dist), str(e)) for entry in sorted(working_set.iter_entry_points(entry_point_name), key=lambda entry: entry.name): logger.debug('Loading %s from %s', entry.name, entry.dist.location) try: entry.load(require=True) except Exception, e: logger.exception("Error loading: %s", entry) else: logger.debug("Loaded module %s from %s:", entry.module_name, entry.dist.location)
def add_plugin_dir(plugin_dir): """ Find available plugins """ if not os.path.isdir(plugin_dir): LOG.debug("Adding plugin directory: %r", plugin_dir) env = Environment([plugin_dir]) dists, errors = working_set.find_plugins(env) for dist in dists: LOG.debug("Adding distribution: %r", dist) working_set.add(dist) if errors: for dist, error in list(errors.items()): errmsg = None if isinstance(error, DistributionNotFound): req, = error.args errmsg = "%r not found" % req.project_name elif isinstance(error, VersionConflict): dist, req = error.args errmsg = "Version Conflict. Requested %s Found %s" % (req, dist) else: errmsg = repr(error) LOG.error("Failed to load %s: %r", dist, errmsg) PLUGIN_DIRECTORIES.append(plugin_dir)
def _load_eggs(env, search_path, auto_enable=None): # Note that the following doesn't seem to support unicode search_path distributions, errors = working_set.find_plugins( pkg_resources.Environment(search_path) ) for dist in distributions: if dist not in working_set: env.log.debug('Adding plugin "%s" from "%s"', dist, dist.location) working_set.add(dist) def _log_error(item, e): ue = exception_to_unicode(e) if isinstance(e, DistributionNotFound): env.log.debug('Skipping "%s": %s', item, ue) elif isinstance(e, (ImportError, UnknownExtra, VersionConflict)): env.log.error('Skipping "%s": %s', item, ue) else: env.log.error('Skipping "%s": %s', item, exception_to_unicode(e, traceback=True)) for dist, e in errors.iteritems(): _log_error(dist, e) for entry in sorted(working_set.iter_entry_points(entry_point_name), key=lambda entry: entry.name): env.log.debug('Loading plugin "%s" from "%s"', entry.name, entry.dist.location) try: entry.load(require=True) except Exception as e: _log_error(entry, e) else: if os.path.dirname(entry.dist.location) == auto_enable: _enable_plugin(env, entry.module_name)
def load_eggs(self, search_path, auto_enable=None): # Note that the following doesn't seem to support unicode search_path distributions, errors = working_set.find_plugins( pkg_resources.Environment(search_path)) for dist in distributions: if dist not in working_set: working_set.add(dist) def _log_error(item, e): log.error("[plugins] error loading %s %s", item, e) log.error('[plugins] %s', traceback.format_exc()) for dist, e in errors.iteritems(): # ignore version conflict of modules which are not OMS plugins if self.ENTRY_POINT_NAME in dist.get_entry_map(): _log_error(dist, e) for entry in sorted(working_set.iter_entry_points( self.ENTRY_POINT_NAME), key=lambda entry: entry.name): log.debug('[plugins] Loading %s from %s' % (entry.name, entry.dist.location)) try: entry.load(require=True) except Exception, e: _log_error(entry, e) else: yield entry
def _load_eggs(env, search_path, auto_enable=None): # Note that the following doesn't seem to support unicode search_path distributions, errors = working_set.find_plugins(pkg_resources.Environment(search_path)) for dist in distributions: env.log.debug("Adding plugin %s from %s", dist, dist.location) working_set.add(dist) def _log_error(item, e): ue = exception_to_unicode(e) if isinstance(e, DistributionNotFound): env.log.debug('Skipping "%s": ("%s" not found)', item, ue) elif isinstance(e, VersionConflict): env.log.error('Skipping "%s": (version conflict "%s")', item, ue) elif isinstance(e, UnknownExtra): env.log.error('Skipping "%s": (unknown extra "%s")', item, ue) elif isinstance(e, ImportError): env.log.error('Skipping "%s": (can\'t import "%s")', item, ue) else: env.log.error('Skipping "%s": (error "%s")', item, ue) for dist, e in errors.iteritems(): _log_error(dist, e) for entry in working_set.iter_entry_points(entry_point_name): env.log.debug("Loading %s from %s", entry.name, entry.dist.location) try: entry.load(require=True) except (ImportError, DistributionNotFound, VersionConflict, UnknownExtra), e: _log_error(entry, e) else: if os.path.dirname(entry.dist.location) == auto_enable: _enable_plugin(env, entry.module_name)
def _load_eggs(env): distributions, errors = working_set.find_plugins( pkg_resources.Environment()) for dist in distributions: if dist not in working_set: env.log.debug('Adding plugin %s from %s', dist, dist.location) working_set.add(dist) def _log_error(item, e): ue = exception_to_unicode(e) if isinstance(e, DistributionNotFound): env.log.debug('Skipping "%s": ("%s" not found)', item, ue) elif isinstance(e, VersionConflict): env.log.error('Skipping "%s": (version conflict "%s")', item, ue) elif isinstance(e, UnknownExtra): env.log.error('Skipping "%s": (unknown extra "%s")', item, ue) else: env.log.error('Skipping "%s": %s', item, exception_to_unicode(e, traceback=True)) for dist, e in errors.iteritems(): _log_error(dist, e) for entry in sorted(working_set.iter_entry_points(entry_point_name), key=lambda entry: entry.name): env.log.debug('Loading %s from %s', entry.name, entry.dist.location) try: entry.load(require=True) except Exception as exc: _log_error(entry, exc) else: _enable_plugin(env, entry.module_name)
def load_eggs(self, search_path, auto_enable=None): # Note that the following doesn't seem to support unicode search_path distributions, errors = working_set.find_plugins( pkg_resources.Environment(search_path) ) for dist in distributions: if dist not in working_set: working_set.add(dist) def _log_error(item, e): log.error("[plugins] error loading %s %s", item, e) log.error('[plugins] %s', traceback.format_exc()) for dist, e in errors.iteritems(): # ignore version conflict of modules which are not OMS plugins if self.ENTRY_POINT_NAME in dist.get_entry_map(): _log_error(dist, e) for entry in sorted(working_set.iter_entry_points(self.ENTRY_POINT_NAME), key=lambda entry: entry.name): log.debug('[plugins] Loading %s from %s' % (entry.name, entry.dist.location)) try: entry.load(require=True) except Exception, e: _log_error(entry, e) else: yield entry
def load(self, env, search_path, disable_re, name_re): generate_debug_messages = __debug__ and env.log.isEnabledFor( logging.DEBUG) if not pkg_resources_avail: if generate_debug_messages: env.log.debug( 'The EggLoader service is terminating early because the pkg_resources package is not available on this machine.' ) return env.log.info('BEGIN - Loading plugins with an EggLoader service') distributions, errors = working_set.find_plugins( pkg_environment(search_path)) for dist in distributions: if name_re.match(str(dist)): if generate_debug_messages: env.log.debug('Adding plugin %r from %r', dist, dist.location) working_set.add(dist) else: if generate_debug_messages: env.log.debug('Ignoring plugin %r from %r', dist, dist.location) def _log_error(item, e): gen_debug = __debug__ and env.log.isEnabledFor(logging.DEBUG) if isinstance(e, DistributionNotFound): if gen_debug: env.log.debug('Skipping "%s": ("%s" not found)', item, e) elif isinstance(e, VersionConflict): if gen_debug: env.log.debug('Skipping "%s": (version conflict "%s")', item, e) elif isinstance(e, UnknownExtra): env.log.error('Skipping "%s": (unknown extra "%s")', item, e) elif isinstance(e, ImportError): env.log.error('Skipping "%s": (can\'t import "%s")', item, e) else: env.log.error('Skipping "%s": (error "%s")', item, e) for dist, e in errors.items(): _log_error(dist, e) for entry in working_set.iter_entry_points(self.entry_point_name): if generate_debug_messages: env.log.debug('Loading %r from %r', entry.name, entry.dist.location) try: entry.load(require=True) except (ImportError, DistributionNotFound, VersionConflict, UnknownExtra): e = sys.exc_info()[1] _log_error(entry, e) else: if not disable_re.match(os.path.dirname( entry.module_name)) is None: #_enable_plugin(env, entry.module_name) pass env.log.info('END - Loading plugins with an EggLoader service')
def test_setup_requires_overrides_version_conflict(self): """ Regression test for issue #323. Ensures that a distribution's setup_requires requirements can still be installed and used locally even if a conflicting version of that requirement is already on the path. """ pr_state = pkg_resources.__getstate__() fake_dist = PRDistribution('does-not-matter', project_name='foobar', version='0.0') working_set.add(fake_dist) def setup_and_run(temp_dir): test_pkg = create_setup_requires_package(temp_dir) test_setup_py = os.path.join(test_pkg, 'setup.py') try: stdout, stderr = quiet_context(lambda: reset_setup_stop_context( # Don't even need to install the package, just running # the setup.py at all is sufficient lambda: run_setup(test_setup_py, ['--name']))) except VersionConflict: self.fail('Installing setup.py requirements caused ' 'VersionConflict') lines = stdout.splitlines() self.assertGreater(len(lines), 0) self.assert_(lines[-1].strip(), 'test_pkg') try: tempdir_context(setup_and_run) finally: pkg_resources.__setstate__(pr_state)
def fetch_build_eggs(self, requires): """Resolve pre-setup requirements""" from pkg_resources import working_set, parse_requirements for dist in working_set.resolve( parse_requirements(requires), installer=self.fetch_build_egg ): working_set.add(dist)
def satisfy(self): self._missing_distribs = [] self.search() for requirement in self.requirements: try: # distrib disponible directement ? distrib = get_distribution(requirement) self.environment.add(distrib) except DistributionNotFound as e: # meilleur candidat pour cette distrib aux emplacements désignés distrib = self.environment.best_match(requirement, working_set) if distrib is not None: working_set.add(distrib) else: self._missing_distribs.append(requirement) if len(self.missing_distribs) > 0: print('Missing distributions : ', list(map(str, self.missing_distribs)), file=sys.stderr) return False return True
def load(self, env, search_path, disable_re, name_re): generate_debug_messages = __debug__ and env.log.isEnabledFor( logging.DEBUG) if not pkg_resources_avail: if generate_debug_messages: env.log.debug( 'The EggLoader service is terminating early because the pkg_resources package is not available on this machine.') return env.log.info('BEGIN - Loading plugins with an EggLoader service') distributions, errors = working_set.find_plugins( pkg_environment(search_path)) for dist in distributions: if name_re.match(str(dist)): if generate_debug_messages: env.log.debug('Adding plugin %r from %r', dist, dist.location) working_set.add(dist) else: if generate_debug_messages: env.log.debug('Ignoring plugin %r from %r', dist, dist.location) def _log_error(item, e): gen_debug = __debug__ and env.log.isEnabledFor(logging.DEBUG) if isinstance(e, DistributionNotFound): if gen_debug: env.log.debug('Skipping "%s": ("%s" not found)', item, e) elif isinstance(e, VersionConflict): if gen_debug: env.log.debug('Skipping "%s": (version conflict "%s")', item, e) elif isinstance(e, UnknownExtra): env.log.error('Skipping "%s": (unknown extra "%s")', item, e) elif isinstance(e, ImportError): env.log.error('Skipping "%s": (can\'t import "%s")', item, e) else: env.log.error('Skipping "%s": (error "%s")', item, e) for dist, e in errors.items(): _log_error(dist, e) for entry in working_set.iter_entry_points(self.entry_point_name): if generate_debug_messages: env.log.debug('Loading %r from %r', entry.name, entry.dist.location) try: entry.load(require=True) except (ImportError, DistributionNotFound, VersionConflict, UnknownExtra): e = sys.exc_info()[1] _log_error(entry, e) else: if not disable_re.match(os.path.dirname( entry.module_name)) is None: #_enable_plugin(env, entry.module_name) pass env.log.info('END - Loading plugins with an EggLoader service')
def configure(self, section): env = Environment([]) self._eggs,errors = working_set.find_plugins(env) # load plugin eggs for p in self._eggs: working_set.add(p) for e in errors: logger.info("failed to load plugin egg '%s'" % e)
def test_setup_requires_override_nspkg(self, use_setup_cfg): """ Like ``test_setup_requires_overrides_version_conflict`` but where the ``setup_requires`` package is part of a namespace package that has *already* been imported. """ with contexts.save_pkg_resources_state(): with contexts.tempdir() as temp_dir: foobar_1_archive = os.path.join(temp_dir, 'foo.bar-0.1.tar.gz') make_nspkg_sdist(foobar_1_archive, 'foo.bar', '0.1') # Now actually go ahead an extract to the temp dir and add the # extracted path to sys.path so foo.bar v0.1 is importable foobar_1_dir = os.path.join(temp_dir, 'foo.bar-0.1') os.mkdir(foobar_1_dir) with tarfile.open(foobar_1_archive) as tf: tf.extractall(foobar_1_dir) sys.path.insert(1, foobar_1_dir) dist = PRDistribution(foobar_1_dir, project_name='foo.bar', version='0.1') working_set.add(dist) template = DALS("""\ import foo # Even with foo imported first the # setup_requires package should override import setuptools setuptools.setup(**%r) if not (hasattr(foo, '__path__') and len(foo.__path__) == 2): print('FAIL') if 'foo.bar-0.2' not in foo.__path__[0]: print('FAIL') """) test_pkg = create_setup_requires_package( temp_dir, 'foo.bar', '0.2', make_nspkg_sdist, template, use_setup_cfg=use_setup_cfg) test_setup_py = os.path.join(test_pkg, 'setup.py') with contexts.quiet() as (stdout, stderr): try: # Don't even need to install the package, just # running the setup.py at all is sufficient run_setup(test_setup_py, [str('--name')]) except pkg_resources.VersionConflict: self.fail( 'Installing setup.py requirements ' 'caused a VersionConflict') assert 'FAIL' not in stdout.getvalue() lines = stdout.readlines() assert len(lines) > 0 assert lines[-1].strip() == 'test_pkg'
def load_eggs(search_path): distributions, errors = working_set.find_plugins( pkg_resources.Environment(search_path)) for distribution in distributions: working_set.add(distribution) if errors: import warnings warnings.warn("Error loading eggs: %s" % (errors, ))
def install_ckantestplugin(): # Create the ckantestplugin setuptools distribution mydir = os.path.dirname(__file__) egg_info = os.path.join(mydir, 'ckantestplugin', 'ckantestplugin.egg-info') base_dir = os.path.dirname(egg_info) metadata = PathMetadata(base_dir, egg_info) dist_name = os.path.splitext(os.path.basename(egg_info))[0] ckantestplugin_dist = Distribution( base_dir, project_name=dist_name, metadata=metadata) working_set.add(ckantestplugin_dist)
def load_eggs(search_path): distributions, errors = working_set.find_plugins( pkg_resources.Environment(search_path) ) for distribution in distributions: working_set.add(distribution) if errors: import warnings warnings.warn("Error loading eggs: %s" % (errors,))
def install_ckantestplugin(): # Create the ckantestplugin setuptools distribution mydir = os.path.dirname(__file__) egg_info = os.path.join(mydir, 'ckantestplugin', 'ckantestplugin.egg-info') base_dir = os.path.dirname(egg_info) metadata = PathMetadata(base_dir, egg_info) dist_name = os.path.splitext(os.path.basename(egg_info))[0] ckantestplugin_dist = Distribution(base_dir, project_name=dist_name, metadata=metadata) working_set.add(ckantestplugin_dist)
def __init__(self, buildout, name, options): if "eggs" not in options: # If we don't set the "eggs" option, it will take the part name: options['eggs'] = "" _LOGGER.debug("Set the 'eggs' option to an empty string") ws = Eggs(buildout, name, options).working_set(["django >= 1.0"])[1] for dist in ws: working_set.add(dist) # Finding the path to the media in Django Admin: from django import __file__ as django_file django_root = path.dirname(django_file) admin_media = path.join(django_root, "contrib", "admin", "media") # We found it, let's make it available to other recipes/parts: options['admin_media_root'] = admin_media
def __init__(self, buildout, name, options): if "eggs" not in options: # If we don't set the "eggs" option, it will take the part name: options['eggs'] = "" _LOGGER.debug("Set the 'eggs' option to an empty string") ws = Eggs(buildout, name, options).working_set(["django >= 1.4"])[1] for dist in ws: working_set.add(dist) # Finding the path to the media in Django Admin: from django import __file__ as django_file django_root = path.dirname(django_file) admin_media = \ path.join(django_root, "contrib", "admin", "static", "admin") # We found it, let's make it available to other recipes/parts: options['admin_media_root'] = admin_media
def test_loading_external_storage_implementation(): try: distribution = Distribution(os.path.join( os.path.dirname(__file__), "fixtures")) entry_point = EntryPoint.parse( 'scheduling = fake_storage:MySchedulingStorage', dist=distribution) distribution._ep_map = { 'chaosplatform.storage': {'scheduling': entry_point}} working_set.add(distribution) storage = initialize_storage(config={"db":{"uri": "sqlite:///"}}) assert storage.__class__.__name__ == "MySchedulingStorage" assert storage.some_flag == True shutdown_storage(storage) assert storage.some_flag == False finally: distribution._ep_map.clear()
async def get(self, suggestion_type: SuggestionType) -> Figmentator: """ Get a Figmentator by it's type. If there are valid settings for a Figmentator it will instatiate it (and install any needed requirements. """ async with self.lock: if suggestion_type not in self.figmentators_by_type: if (suggestion_type not in self.settings.figmentators # pylint:disable=unsupported-membership-test ): raise ValueError( f"Cannot create Figmentator of type {suggestion_type}") settings = self.settings.figmentators[ # pylint:disable=unsubscriptable-object suggestion_type] requirements = await self.loop.run_in_executor( None, # use default executor working_set.resolve, # resolve model requirements settings.requires, # list of requirements self.env, # environment self.installer, # installer for missing requirements True, # whether to replace conflicting requirements ) for requirement in requirements: working_set.add(requirement, replace=True) try: model_cls = settings.cls.resolve() except ImportError as e: raise e if not issubclass(model_cls, Figmentator): raise ValueError( "model_cls must be a subclass of Figmentator") figmentator = model_cls(suggestion_type) await self.loop.run_in_executor(None, figmentator.startup, settings.properties) self.figmentators_by_type[suggestion_type] = figmentator return self.figmentators_by_type[suggestion_type]
def load_eggs(ch, entry_point, search_path, only_enabled=True): """Loader that loads any eggs on the search path and `sys.path`.""" logger.debug("Loading eggs...") # Note that the following doesn't seem to support unicode search_path distributions, errors = working_set.find_plugins( pkg_resources.Environment(search_path)) logger.debug("Found distributions: %s", str(distributions)) for dist in distributions: if dist not in working_set: logger.debug('Adding plugin %s from %s', dist, dist.location) working_set.add(dist) for dist, e in errors.iteritems(): logger.error("Error in distribution %s: %s", str(dist), str(e)) for entry in sorted(working_set.iter_entry_points(entry_point), key=lambda entry: entry.name): logger.debug('Loading %s from %s', entry.name, entry.dist.location) # If we are only loading enabled components, consult the config and skip if # not found if only_enabled and not any( map(lambda x: x.startswith(entry.name), ch.config.get('components', {}))): logger.debug('Skipping component %s since it is not enabled' % entry.name) continue try: entry.load(require=True) except ImportError, e: ch.failed_components[entry.name] = e logger.warn( "Loading %s failed, probably because of unmet dependencies: %s", entry.name, str(e)) except Exception, e: ch.failed_components[entry.name] = e logger.exception("Error loading: %s", entry)
def _load_eggs(search_path, auto_enable=None): # Note that the following doesn't seem to support unicode search_path distributions, errors = working_set.find_plugins( pkg_resources.Environment(search_path) ) for dist in distributions: if dist not in working_set: working_set.add(dist) def _log_error(item, e): log.error("[plugins] error loading %s %s", item, e) for dist, e in errors.iteritems(): # ignore version conflict of modules which are not OMS plugins if ENTRY_POINT_NAME in dist.get_entry_map(): _log_error(dist, e) for entry in sorted(working_set.iter_entry_points(ENTRY_POINT_NAME), key=lambda entry: entry.name): yield entry
def _run_tests(self): old_path = sys.path[:] ei_cmd = self.get_finalized_command("egg_info") path_item = normalize_path(ei_cmd.egg_base) metadata = PathMetadata(path_item, normalize_path(ei_cmd.egg_info)) dist = Distribution(path_item, metadata, project_name=ei_cmd.egg_name) working_set.add(dist) require(str(dist.as_requirement())) loader_ep = EntryPoint.parse("x=" + self.test_loader) loader_class = loader_ep.load(require=False) try: import unittest unittest.main(None, None, [unittest.__file__] + self.test_args, testRunner=XMLTestRunner( stream=sys.stdout, xml_stream=self.xml_output_file), testLoader=loader_class()) except SystemExit, e: return e.code
def _load_eggs(env): distributions, errors = working_set.find_plugins( pkg_resources.Environment() ) for dist in distributions: if dist not in working_set: env.log.debug('Adding plugin %s from %s', dist, dist.location) working_set.add(dist) def _log_error(item, e): ue = exception_to_unicode(e) if isinstance(e, DistributionNotFound): env.log.debug('Skipping "%s": ("%s" not found)', item, ue) elif isinstance(e, VersionConflict): env.log.error('Skipping "%s": (version conflict "%s")', item, ue) elif isinstance(e, UnknownExtra): env.log.error('Skipping "%s": (unknown extra "%s")', item, ue) else: env.log.error('Skipping "%s": %s', item, exception_to_unicode(e, traceback=True)) for dist, e in errors.iteritems(): _log_error(dist, e) for entry in sorted(working_set.iter_entry_points(entry_point_name), key=lambda entry: entry.name): env.log.debug( 'Loading %s from %s', entry.name, entry.dist.location ) try: entry.load(require=True) except Exception as exc: _log_error(entry, exc) else: _enable_plugin(env, entry.module_name)
def add_plugin_dir(plugin_dir): LOGGER.debug("Adding plugin directory: %r", plugin_dir) env = Environment([plugin_dir]) dists, errors = working_set.find_plugins(env) for dist in dists: LOGGER.debug("Adding distribution: %r", dist) working_set.add(dist) if errors: for dist, error in errors.items(): errmsg = None if isinstance(error, DistributionNotFound): req, = error.args errmsg = "%r not found" % req.project_name elif isinstance(error, VersionConflict): dist, req = error.args errmsg = "Version Conflict. Requested %s Found %s" % (req, dist) else: # FIXME: Are there other types of failures? errmsg = repr(error) LOGGER.error("Failed to load %s: %r", dist, errmsg) global plugin_directories plugin_directories.append(plugin_dir)
def _run_tests(self): old_path = sys.path[:] ei_cmd = self.get_finalized_command("egg_info") path_item = normalize_path(ei_cmd.egg_base) metadata = PathMetadata( path_item, normalize_path(ei_cmd.egg_info) ) dist = Distribution(path_item, metadata, project_name=ei_cmd.egg_name) working_set.add(dist) require(str(dist.as_requirement())) loader_ep = EntryPoint.parse("x=" + self.test_loader) loader_class = loader_ep.load(require=False) try: import unittest unittest.main( None, None, [unittest.__file__] + self.test_args, testRunner=XMLTestRunner(stream=sys.stdout, xml_stream=self.xml_output_file), testLoader=loader_class() ) except SystemExit, e: return e.code
def load_eggs(ch, entry_point, search_path, only_enabled=True): """Loader that loads any eggs on the search path and `sys.path`.""" logger.debug("Loading eggs...") # Note that the following doesn't seem to support unicode search_path distributions, errors = working_set.find_plugins( pkg_resources.Environment(search_path) ) logger.debug("Found distributions: %s", str(distributions)) for dist in distributions: if dist not in working_set: logger.debug('Adding plugin %s from %s', dist, dist.location) working_set.add(dist) for dist, e in errors.iteritems(): logger.error("Error in distribution %s: %s", str(dist), str(e)) for entry in sorted(working_set.iter_entry_points(entry_point), key=lambda entry: entry.name): logger.debug('Loading %s from %s', entry.name, entry.dist.location) # If we are only loading enabled components, consult the config and skip if # not found if only_enabled and not any(map(lambda x: x.startswith(entry.name), ch.config.get('components', {}))): logger.debug('Skipping component %s since it is not enabled' % entry.name) continue try: entry.load(require=True) except ImportError, e: ch.failed_components[entry.name] = e logger.warn("Loading %s failed, probably because of unmet dependencies: %s", entry.name, str(e)) except Exception, e: ch.failed_components[entry.name] = e logger.exception("Error loading: %s", entry)
def initPluginEnv(options, path): from pkg_resources import working_set, Environment # if options is passed in, use prefs to determine what to bypass # otherwise all plugins are added to the working_set if options is not None: prefs = loadPrefs(options) pluginPrefs = prefs.get('plugins', None) else: prefs = None pluginPrefs = None plugin_env = Environment(path) plugin_eggs = [] # remove uninstalled plugins from prefs if pluginPrefs is not None: for project_name in pluginPrefs.keys(): if project_name not in plugin_env: del prefs['plugins'][project_name] prefs.write() # add active plugins and l10n eggs to working set for project_name in sorted(plugin_env): for egg in plugin_env[project_name]: if egg.has_metadata('resources.ini'): working_set.add(egg) # possible l10n egg elif (pluginPrefs is None or pluginPrefs.get(project_name) != 'inactive'): working_set.add(egg) # possible plugin egg plugin_eggs.append(egg) break return plugin_env, plugin_eggs
def __init__(self): self._plugins = {} plugins_dir = os.path.join(site_settings["HIGGINS_DIR"], "plugins") self.log_info("added '%s' to plugin search path" % plugins_dir) working_set.add_entry(plugins_dir) env = Environment([plugins_dir,]) self._eggs,errors = working_set.find_plugins(env) # load plugin eggs for p in self._eggs: working_set.add(p) self.log_info("loaded plugin egg '%s'" % p) for e in errors: self.log_error("failed to load plugin egg '%s'" % e) # load all discovered plugins for ep in working_set.iter_entry_points('higgins.plugin'): try: factory = ep.load() if issubclass(factory, Service): self.log_info("found service plugin '%s'" % ep.name) self._plugins[ep.name] = factory else: self.log_warning("ignoring plugin '%s': unknown plugin type" % ep.name) except Exception, e: self.log_error("failed to load plugin '%s': %s" % (ep.name, e))
def installer(self, requirement: Requirement) -> Optional[Distribution]: """ A method for using easy_install to install a requirement """ for dist in self.env[requirement.key]: if dist not in requirement: self.env.remove(dist) # Use easy_install despite being deprecated as it is the only way to # have multi-version package support. See: # https://packaging.python.org/guides/multi-version-installs/ # https://packaging.python.org/discussions/pip-vs-easy-install/ with shadow_argv([ "", "easy_install", "--install-dir", self.settings.install_dir, str(requirement), ]): setup() distribution = self.get_distribution(requirement) if distribution: working_set.add(distribution, replace=True) return distribution
def test_setup_requires_overrides_version_conflict(self): """ Regression test for issue #323. Ensures that a distribution's setup_requires requirements can still be installed and used locally even if a conflicting version of that requirement is already on the path. """ pr_state = pkg_resources.__getstate__() fake_dist = PRDistribution('does-not-matter', project_name='foobar', version='0.0') working_set.add(fake_dist) def setup_and_run(temp_dir): test_pkg = create_setup_requires_package(temp_dir) test_setup_py = os.path.join(test_pkg, 'setup.py') try: stdout, stderr = quiet_context( lambda: reset_setup_stop_context( # Don't even need to install the package, just running # the setup.py at all is sufficient lambda: run_setup(test_setup_py, ['--name']) )) except VersionConflict: self.fail('Installing setup.py requirements caused ' 'VersionConflict') lines = stdout.splitlines() self.assertGreater(len(lines), 0) self.assert_(lines[-1].strip(), 'test_pkg') try: tempdir_context(setup_and_run) finally: pkg_resources.__setstate__(pr_state)
def configure(self, settings): """ Finds all discoverable plugins and configures them. Plugins are discoverable if they are in the normal python module path, or in the path specified by 'plugin directory'. """ # load plugins section = settings.section('server') self.pluginsdir = os.path.join(section.getPath("plugin directory")) if self.pluginsdir: logger.debug("loading plugins from %s" % self.pluginsdir) working_set.add_entry(self.pluginsdir) env = Environment([self.pluginsdir,]) else: env = Environment([]) self._eggs,errors = working_set.find_plugins(env) # load plugin eggs for p in self._eggs: working_set.add(p) logger.info("loaded plugin egg '%s'" % p) for e in errors: logger.info("failed to load plugin egg '%s'" % e) # load all discovered plugins for each type for ep in working_set.iter_entry_points("terane.plugin"): # if no config section exists, then don't load the plugin if not settings.hasSection("plugin:%s" % ep.name): continue try: # load and configure the plugin _Plugin = ep.load() if not IPlugin.implementedBy(_Plugin): raise Exception("plugin '%s' doesn't implement IPlugin" % ep.name) plugin = _Plugin() plugin.setName(ep.name) plugin.setServiceParent(self) section = settings.section("plugin:%s" % ep.name) plugin.configure(section) logger.info("loaded plugin '%s'" % ep.name) # find all plugin components for impl,spec,name in plugin.listComponents(): if not ILoadable.implementedBy(impl): raise Exception("component %s:%s in plugin %s doesn't implement ILoadable" % (spec.__name__, name, ep.name)) if not isinstance(spec, InterfaceClass): raise TypeError("spec must be an Interface") if (spec,name) in self._components: raise KeyError("component %s:%s already exists" % (spec.__name__,name)) # a little extra syntax here to make sure the lambda expression # passed as the factory function has the appropriate variables bound # in its scope def _makeTrampoline(impl=impl, plugin=plugin): def _trampoline(*args, **kwds): logger.trace("allocating new %s from plugin %s" % (impl.__name__,plugin.name)) return impl(plugin, *args, **kwds) return _trampoline self._components[(spec,name)] = _makeTrampoline(impl, plugin) logger.trace("added component %s:%s" % (spec.__name__, name)) except ConfigureError: raise except Exception, e: logger.exception(e) logger.warning("failed to load plugin '%s'" % ep.name)
def configure(self, settings): """ Finds all discoverable plugins and configures them. Plugins are discoverable if they are in the normal python module path, or in the path specified by 'plugin directory'. """ # load plugins section = settings.section('server') self.pluginsdir = os.path.join(section.getPath("plugin directory")) if self.pluginsdir: logger.debug("loading plugins from %s" % self.pluginsdir) working_set.add_entry(self.pluginsdir) env = Environment([ self.pluginsdir, ]) else: env = Environment([]) self._eggs, errors = working_set.find_plugins(env) # load plugin eggs for p in self._eggs: working_set.add(p) logger.info("loaded plugin egg '%s'" % p) for e in errors: logger.info("failed to load plugin egg '%s'" % e) # load all discovered plugins for each type for ep in working_set.iter_entry_points("terane.plugin"): # if no config section exists, then don't load the plugin if not settings.hasSection("plugin:%s" % ep.name): continue try: # load and configure the plugin _Plugin = ep.load() if not IPlugin.implementedBy(_Plugin): raise Exception("plugin '%s' doesn't implement IPlugin" % ep.name) plugin = _Plugin() plugin.setName(ep.name) plugin.setServiceParent(self) section = settings.section("plugin:%s" % ep.name) plugin.configure(section) logger.info("loaded plugin '%s'" % ep.name) # find all plugin components for impl, spec, name in plugin.listComponents(): if not ILoadable.implementedBy(impl): raise Exception( "component %s:%s in plugin %s doesn't implement ILoadable" % (spec.__name__, name, ep.name)) if not isinstance(spec, InterfaceClass): raise TypeError("spec must be an Interface") if (spec, name) in self._components: raise KeyError("component %s:%s already exists" % (spec.__name__, name)) # a little extra syntax here to make sure the lambda expression # passed as the factory function has the appropriate variables bound # in its scope def _makeTrampoline(impl=impl, plugin=plugin): def _trampoline(*args, **kwds): logger.trace("allocating new %s from plugin %s" % (impl.__name__, plugin.name)) return impl(plugin, *args, **kwds) return _trampoline self._components[(spec, name)] = _makeTrampoline(impl, plugin) logger.trace("added component %s:%s" % (spec.__name__, name)) except ConfigureError: raise except Exception, e: logger.exception(e) logger.warning("failed to load plugin '%s'" % ep.name)
def setUp(self): self._saved_plugins_config = config.get('ckan.plugins', '') config['ckan.plugins'] = '' plugins.reset() working_set.add(ckantestplugin_dist)
import pluginlib._loader as loader from pluginlib import BlacklistEntry, PluginImportError, EntryPointWarning from tests import TestCase, OUTPUT, mock import tests.testdata import tests.testdata.parents try: from importlib import reload except ImportError: pass DATAPATH = os.path.dirname(tests.testdata.__file__) DIST = Distribution.from_filename(tests.testdata.__file__) working_set.add(DIST) DIST._ep_map = {} # pylint: disable=protected-access class TestPluginLoaderInit(TestCase): """Tests for initialization of PluginLoader""" def test_bad_arguments(self): """Error is raised when argument type is wrong""" # Expect iterables for arg in ('modules', 'paths', 'blacklist', 'type_filter'): with self.assertRaises(TypeError): loader.PluginLoader(**{arg: 'string'})
"""Resolve pre-setup requirements""" <<<<<<< HEAD resolved_dists = pkg_resources.working_set.resolve( pkg_resources.parse_requirements(requires), installer=self.fetch_build_egg, replace_conflicting=True, ) for dist in resolved_dists: pkg_resources.working_set.add(dist, replace=True) ======= from pkg_resources import working_set, parse_requirements for dist in working_set.resolve( parse_requirements(requires), installer=self.fetch_build_egg, replace_conflicting=True ): working_set.add(dist, replace=True) >>>>>>> e4baf504ede925f4f1e07d823c9b20b3d0dbe14c def finalize_options(self): _Distribution.finalize_options(self) if self.features: self._set_global_opts_from_features() for ep in pkg_resources.iter_entry_points('distutils.setup_keywords'): value = getattr(self,ep.name,None) if value is not None: ep.require(installer=self.fetch_build_egg) ep.load()(self, ep.name, value) if getattr(self, 'convert_2to3_doctests', None): # XXX may convert to set here when we can rely on set being builtin self.convert_2to3_doctests = [os.path.abspath(p) for p in self.convert_2to3_doctests]