예제 #1
0
def _initialize_entry_point_group(entrypoint_group):
    global _WS

    installed = _installed_versions()

    if _WS is None:
        _initialize_master_working_set()
        _WS = WorkingSet()

    cache = {}
    result = {}
    for ep in _WS.iter_entry_points(entrypoint_group):
        egg_name = ep.dist.egg_name()
        conflicts = cache.get(egg_name, None)
        if conflicts is None:
            conflicts = _conflicts(ep.dist.requires(), installed)
            cache[egg_name] = conflicts

        if len(conflicts) != 0:
            LOG.error('{} not loadable: {}'.format(ep.name,
                                                   ', '.join(conflicts)))
        result[ep.name] = MMEntryPoint(ep=ep,
                                       name=ep.name,
                                       conflicts=conflicts,
                                       loadable=(len(conflicts) == 0))

    _ENTRYPOINT_GROUPS[entrypoint_group] = result
예제 #2
0
    def __init__(self, user_dir):
        self.user_dir = user_dir
        self.initialize_user_dir(self.user_dir)

        # XXX: Doing this here is a bit of a hack; pkg_resources needs to be
        # re-initialized whenever a module is installed or removed.
        pkg_resources._initialize_master_working_set()
예제 #3
0
    def _cmd_get_active_distributions(self, cmd):
        # if it is called after first installation to user site packages
        # this dir is not yet in sys.path
        if (site.ENABLE_USER_SITE and site.getusersitepackages()
                and os.path.exists(site.getusersitepackages())
                and site.getusersitepackages() not in sys.path):
            # insert before first site packages item
            for i, item in enumerate(sys.path):
                if "site-packages" in item or "dist-packages" in item:
                    sys.path.insert(i, site.getusersitepackages())
                    break
            else:
                sys.path.append(site.getusersitepackages())

        import pkg_resources

        pkg_resources._initialize_master_working_set()
        dists = {
            dist.key: {
                "project_name": dist.project_name,
                "key": dist.key,
                "location": dist.location,
                "version": dist.version,
            }
            for dist in pkg_resources.working_set  # pylint: disable=not-an-iterable
        }

        return dict(
            distributions=dists,
            usersitepackages=site.getusersitepackages()
            if site.ENABLE_USER_SITE else None,
        )
예제 #4
0
파일: pyenv.py 프로젝트: ohad182/pyenv
def get_installed():
    """
    Gets the installed pacakages from pip pointed by current PATH
    :param local_only:
    :return:
    """
    pkg_resources._initialize_master_working_set(
    )  # refresh installed packages TODO: find replacement
    return [d
            for d in pkg_resources.working_set]  # return working set as array
예제 #5
0
    def _start_update_list(self, name_to_show=None):
        assert self._get_state() in [None, "idle"]
        import pkg_resources

        pkg_resources._initialize_master_working_set()

        self._active_distributions = {
            dist.key: {
                "project_name": dist.project_name,
                "key": dist.key,
                "location": dist.location,
                "version": dist.version,
            }
            for dist in pkg_resources.working_set  # pylint: disable=not-an-iterable
        }

        self._update_list(name_to_show)
예제 #6
0
def execute_setup_py(project_dir, setup_args, disable_languages_test=False):
    """Context manager executing ``setup.py`` with the given arguments.

    It yields after changing the current working directory
    to ``project_dir``.
    """

    # See https://stackoverflow.com/questions/9160227/dir-util-copy-tree-fails-after-shutil-rmtree
    distutils.dir_util._path_created = {}

    # Clear _PYTHON_HOST_PLATFORM to ensure value sets in skbuild.setuptools_wrap.setup() does not
    # influence other tests.
    if '_PYTHON_HOST_PLATFORM' in os.environ:
        del os.environ['_PYTHON_HOST_PLATFORM']

    with push_dir(str(project_dir)), push_argv(["setup.py"] +
                                               setup_args), prepend_sys_path(
                                                   [str(project_dir)]):

        # Restore master working set that is reset following call to "python setup.py test"
        # See function "project_on_sys_path()" in setuptools.command.test
        pkg_resources._initialize_master_working_set()

        with open("setup.py", "r") as fp:
            setup_code = compile(fp.read(), "setup.py", mode="exec")

            if setup_code is not None:

                if disable_languages_test:

                    platform = get_platform()
                    original_write_test_cmakelist = platform.write_test_cmakelist

                    def write_test_cmakelist_no_languages(_self, _languages):
                        original_write_test_cmakelist([])

                    with patch.object(type(platform),
                                      'write_test_cmakelist',
                                      new=write_test_cmakelist_no_languages):
                        six.exec_(setup_code)

                else:
                    six.exec_(setup_code)

        yield
예제 #7
0
def uninstall(name: str):
    # Note: pkg_resources keeps a "working_set" that won't normally be updated when venv changes...
    # An update may need to be forcefully triggered in the future
    pkg_resources._initialize_master_working_set()
    failure = True
    if name in pkg_registry:
        scheme = pkg_registry[name]
        if scheme in ["pipgit", "pip"]:
            failure = pipmain(["uninstall", "-y", name])
        elif scheme == "conda":
            raise NotImplementedError
    else:
        # TODO: Handle if name is not in the registry
        pass

    if not failure:
        del pkg_registry[name]

    return not failure
예제 #8
0
def execute_setup_py(project_dir, setup_args, disable_languages_test=False):
    """Context manager executing ``setup.py`` with the given arguments.

    It yields after changing the current working directory
    to ``project_dir``.
    """

    # See https://stackoverflow.com/questions/9160227/dir-util-copy-tree-fails-after-shutil-rmtree
    distutils.dir_util._path_created = {}

    # Clear _PYTHON_HOST_PLATFORM to ensure value sets in skbuild.setuptools_wrap.setup() does not
    # influence other tests.
    if '_PYTHON_HOST_PLATFORM' in os.environ:
        del os.environ['_PYTHON_HOST_PLATFORM']

    with push_dir(str(project_dir)), push_argv(["setup.py"] + setup_args), prepend_sys_path([str(project_dir)]):

        # Restore master working set that is reset following call to "python setup.py test"
        # See function "project_on_sys_path()" in setuptools.command.test
        pkg_resources._initialize_master_working_set()

        with open("setup.py", "r") as fp:
            setup_code = compile(fp.read(), "setup.py", mode="exec")

            if setup_code is not None:

                if disable_languages_test:

                    platform = get_platform()
                    original_write_test_cmakelist = platform.write_test_cmakelist

                    def write_test_cmakelist_no_languages(_self, _languages):
                        original_write_test_cmakelist([])

                    with patch.object(type(platform), 'write_test_cmakelist', new=write_test_cmakelist_no_languages):
                        six.exec_(setup_code)

                else:
                    six.exec_(setup_code)

        yield
예제 #9
0
import json
import pkg_resources

pkg_resources._initialize_master_working_set()  # reload plugins


class Drivers:

    _loaded = {}

    @classmethod
    def get(cls, group: str, cfg: dict, **kwargs) -> object:
        _hash = hash(json.dumps(cfg, sort_keys=True))
        if _hash not in cls._loaded:
            for entry_point in pkg_resources.iter_entry_points(
                    group=f'vmshepherd.driver.{group}'):
                if entry_point.name == cfg['driver']:
                    _class = entry_point.load()
                    try:
                        cls._loaded[_hash] = _class(config=cfg, **kwargs)
                    except Exception as exc:
                        raise RuntimeError(
                            f"Cannot load driver {cfg['driver']} for {group}."
                        ) from exc
                    break
            else:
                raise RuntimeError(
                    f"Cannot find driver {cfg['driver']} for {group}.")
        return cls._loaded[_hash]

    @classmethod
예제 #10
0
def _install(package) -> pkg_resources.Distribution:
    pip.main(['install', package.project_name])
    pkg_resources._initialize_master_working_set()
    return pkg_resources.get_distribution(package.project_name)