Python pkg_resources.working_set() Examples

The following are 30 code examples of pkg_resources.working_set(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module pkg_resources , or try the search function .
Example #1
Source File: base.py    From calmjs with GNU General Public License v2.0 6 votes vote down vote up
def __init__(self, registry_name, *a, **kw):
        # TODO whenever there is time to move the BaseRegistry to a
        # bootstrap module of some sort (that will break tests that
        # override calmjs.base.working_set if done naively), and have
        # the calmjs.registry.Registry inherit from that (and this
        # module also to maintain the BaseRegistry import location,
        # this import should be moved to the top
        from calmjs.registry import get
        # resolve parent before the parent class, as the construction
        # should be able to reference this parent.
        parent_name = self.resolve_parent_registry_name(registry_name)
        _parent = kw.pop('_parent', NotImplemented)
        if _parent is NotImplemented:
            self.parent = get(parent_name)
        else:
            self.parent = _parent

        if not self.parent:
            raise ValueError(
                "could not construct child module registry '%s' as its "
                "parent registry '%s' could not be found" % (
                    registry_name, parent_name)
            )
        super(BaseChildModuleRegistry, self).__init__(registry_name, *a, **kw) 
Example #2
Source File: utils.py    From calmjs with GNU General Public License v2.0 6 votes vote down vote up
def setup_class_integration_environment(cls, **kw):
    import calmjs.registry
    from calmjs import dist as calmjs_dist
    from calmjs import base
    from calmjs.loaderplugin import MODULE_LOADER_SUFFIX
    cls.dist_dir = mkdtemp_realpath()
    working_set, inst = generate_root_integration_environment(
        cls.dist_dir, **kw)

    cls.root_registry = inst
    cls.registry_name = kw.get('registry_id', 'calmjs.module.simulated')
    cls.test_registry_name = cls.registry_name + tests_suffix
    cls.loader_registry_name = cls.registry_name + MODULE_LOADER_SUFFIX
    cls.working_set = working_set

    # stubs
    cls._old_root_registry, calmjs.registry._inst = calmjs.registry._inst, inst
    cls.root_working_set, calmjs_dist.default_working_set = (
        calmjs_dist.default_working_set, working_set)
    base.working_set = working_set 
Example #3
Source File: environment.py    From recipy with Apache License 2.0 6 votes vote down vote up
def get_packages():
    """
    Get list of installed packages and their versions.

    :return: installed packages and versions, keyed by package name
    :rtype: dict of str or unicode => str or unicode
    """
    packages = pkg_resources.working_set
    packages_dict = {}
    for package in packages:
        # Some packages are imported using their `package.key` (keys do not
        # contain capitals), e.g., gdal. Others are imported using their
        # `package.project_name`, e.g., netCDF4. So, both the `key` and
        # `project_name` are added to the `packages_dict`.
        modules_from_package = package._get_metadata('top_level.txt')
        for mod in modules_from_package:
            packages_dict[mod] = package.version

        packages_dict[package.key] = package.version
        packages_dict[package.project_name] = package.version
    return packages_dict 
Example #4
Source File: package.py    From django-leonardo with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def check_package_exists(package, lib_dir):
    """Check if a package is installed globally or in lib_dir.

    Returns True when the requirement is met.
    Returns False when the package is not installed or doesn't meet req.
    """
    try:
        req = pkg_resources.Requirement.parse(package)
    except ValueError:
        # This is a zip file
        req = pkg_resources.Requirement.parse(urlparse(package).fragment)

    # Check packages from lib dir
    if lib_dir is not None:
        if any(dist in req for dist in
               pkg_resources.find_distributions(lib_dir)):
            return True

    # Check packages from global + virtual environment
    # pylint: disable=not-an-iterable
    return any(dist in req for dist in pkg_resources.working_set) 
Example #5
Source File: pef.py    From pef with Apache License 2.0 5 votes vote down vote up
def cli(packages, yes):
    """Uninstall packages with all its dependencies."""
    if not _is_venv():
        click.secho(
            click.style("Warning! You are not in an active virtual environment. This may purge system-level packages!",
                        fg='red'))
        sys.exit(1)
    if not packages:
        click.secho(click.style("Packages can't be empty, please run `pef --help` for more details.", fg='yellow'))
        sys.exit(0)
    prune = []
    pkg = pkg_resources.working_set
    df = DistInfo(pkg)
    for p in packages:
        if p not in df.keys:
            click.secho(click.style('Cannot uninstall requirement {0}, not installed.'.format(p), fg='yellow'))
            continue
        df.purge(_encode(p))
        prune.extend(df.rm)

    if df.kp:
        click.secho(click.style(
            'Module {0} is referenced by more than one other modules, to remain unchanged.'.format(', '.join(df.kp)),
            fg='yellow'))
    if prune:
        cmd = [sys.executable, '-m', 'pip', 'uninstall']
        if yes:
            cmd.append('-y')
        cmd.extend(list(set(prune)))
        subprocess.check_call(cmd) 
Example #6
Source File: utils.py    From pipenv with MIT License 5 votes vote down vote up
def get_pipenv_sitedir():
    # type: () -> Optional[str]
    import pkg_resources
    site_dir = next(
        iter(d for d in pkg_resources.working_set if d.key.lower() == "pipenv"), None
    )
    if site_dir is not None:
        return site_dir.location
    return None 
Example #7
Source File: libraryversions.py    From recipy with Apache License 2.0 5 votes vote down vote up
def _get_version_from_pkg_resources(modulename):
    ws = pkg_resources.working_set
    package = ws.find(pkg_resources.Requirement(modulename))
    try:
        version = package.version
    except AttributeError:
        version = '?'
    return version 
Example #8
Source File: command.py    From cliff with Apache License 2.0 5 votes vote down vote up
def _get_distributions_by_modules():
    """Return dict mapping module name to distribution names.

    The python package name (the name used for importing) and the
    distribution name (the name used with pip and PyPI) do not
    always match. We want to report which distribution caused the
    command to be installed, so we need to look up the values.

    """
    global _dists_by_mods
    if _dists_by_mods is None:
        results = {}
        for dist in pkg_resources.working_set:
            try:
                mod_names = dist.get_metadata('top_level.txt').strip()
            except Exception:
                # Could not retrieve metadata. Either the file is not
                # present or we cannot read it. Ignore the
                # distribution.
                pass
            else:
                # Distributions may include multiple top-level
                # packages (see setuptools for an example).
                for mod_name in mod_names.splitlines():
                    results[mod_name] = dist.project_name
        _dists_by_mods = results
    return _dists_by_mods 
Example #9
Source File: session_info.py    From reprexpy with MIT License 5 votes vote down vote up
def _get_pkg_info_sectn(self):
        pmods = self._get_potential_mods()
        all_dist_info = [
            self._get_dist_info(i) for i in pkg_resources.working_set
        ]
        libs = self._get_stdlib_list()
        return {
            i: self._get_version_info(i, all_dist_info)
            for i in pmods if i in sys.modules and i not in libs
        } 
Example #10
Source File: deps.py    From httprunner with Apache License 2.0 5 votes vote down vote up
def get_installed_dependenies():
    resp = {"code": 0, "message": "success", "result": {}}
    for p in pkg_resources.working_set:
        resp["result"][p.project_name] = p.version

    return resp 
Example #11
Source File: setup.py    From setuptools_scm with MIT License 5 votes vote down vote up
def scm_config():
    here = os.path.dirname(os.path.abspath(__file__))
    src = os.path.join(here, "src")
    egg_info = os.path.join(src, "setuptools_scm.egg-info")
    has_entrypoints = os.path.isdir(egg_info)
    import pkg_resources

    sys.path.insert(0, src)
    pkg_resources.working_set.add_entry(src)
    # FIXME: remove debug
    print(src)
    print(pkg_resources.working_set)
    from setuptools_scm.hacks import parse_pkginfo
    from setuptools_scm.git import parse as parse_git
    from setuptools_scm.version import guess_next_dev_version, get_local_node_and_date

    def parse(root):
        try:
            return parse_pkginfo(root)
        except IOError:
            return parse_git(root)

    config = dict(
        version_scheme=guess_next_dev_version, local_scheme=get_local_node_and_date
    )

    if has_entrypoints:
        return dict(use_scm_version=config)
    else:
        from setuptools_scm import get_version

        return dict(version=get_version(root=here, parse=parse, **config)) 
Example #12
Source File: synchronizers.py    From pipenv with MIT License 5 votes vote down vote up
def _group_installed_names(packages):
    """Group locally installed packages based on given specifications.

    `packages` is a name-package mapping that are used as baseline to
    determine how the installed package should be grouped.

    Returns a 3-tuple of disjoint sets, all containing names of installed
    packages:

    * `uptodate`: These match the specifications.
    * `outdated`: These installations are specified, but don't match the
        specifications in `packages`.
    * `unneeded`: These are installed, but not specified in `packages`.
    """
    groupcoll = GroupCollection(set(), set(), set(), set())

    for distro in pkg_resources.working_set:
        name = distro.key
        try:
            package = packages[name]
        except KeyError:
            groupcoll.unneeded.add(name)
            continue

        r = requirementslib.Requirement.from_pipfile(name, package)
        if not r.is_named:
            # Always mark non-named. I think pip does something similar?
            groupcoll.outdated.add(name)
        elif not _is_up_to_date(distro, r.get_version()):
            groupcoll.outdated.add(name)
        else:
            groupcoll.uptodate.add(name)

    return groupcoll 
Example #13
Source File: dependencies.py    From sacred with MIT License 5 votes vote down vote up
def fill_missing_version(self):
        if self.version is not None:
            return
        dist = pkg_resources.working_set.by_key.get(self.name)
        self.version = dist.version if dist else None 
Example #14
Source File: runtime.py    From calmjs with GNU General Public License v2.0 5 votes vote down vote up
def __init__(
            self, logger='calmjs', action_key=DEST_RUNTIME,
            working_set=default_working_set, package_name=None,
            description=None, *a, **kw):
        """
        Keyword Arguments:

        logger
            The logger to enable for pretty logging.

            Default: the calmjs root logger

        action_key
            The destination key where the command will be stored.  Under
            this key the target driver runtime will be stored, and it
            will be popped off first before passing rest of kwargs to
            it.

        working_set
            The working_set to use for this instance.

            Default: pkg_resources.working_set

        package_name
            The package name that this instance of runtime is for.  Used
            for the version flag.

        description
            The description for this runtime.
        """

        self.logger = logger
        self.action_key = action_key
        self.working_set = working_set
        self.description = description or self.__doc__
        self.package_name = package_name
        super(BaseRuntime, self).__init__(*a, **kw) 
Example #15
Source File: utils.py    From calmjs with GNU General Public License v2.0 5 votes vote down vote up
def stub_mod_working_set(testcase_inst, modules, working_set):
    """
    Replace the working_set for the target modules
    """

    def restore(module, working_set):
        module.working_set = working_set

    for module in modules:
        testcase_inst.addCleanup(restore, module, module.working_set)
        module.working_set = working_set 
Example #16
Source File: loader.py    From minemeld-core with Apache License 2.0 5 votes vote down vote up
def _initialize_entry_point_group(entrypoint_group):
    global _WS

    installed = {d.project_name: d for d in working_set}

    if _WS is None:
        _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 
Example #17
Source File: dependencies.py    From sacred with MIT License 5 votes vote down vote up
def create(cls, mod):
        if not cls.modname_to_dist:
            # some packagenames don't match the module names (e.g. PyYAML)
            # so we set up a dict to map from module name to package name
            for dist in pkg_resources.working_set:
                try:
                    toplevel_names = dist._get_metadata("top_level.txt")
                    for tln in toplevel_names:
                        cls.modname_to_dist[tln] = dist.project_name, dist.version
                except Exception:
                    pass

        name, version = cls.modname_to_dist.get(mod.__name__, (mod.__name__, None))

        return PackageDependency(name, version) 
Example #18
Source File: synchronizers.py    From pipenv with MIT License 5 votes vote down vote up
def _is_installation_local(name):
    """Check whether the distribution is in the current Python installation.

    This is used to distinguish packages seen by a virtual environment. A venv
    may be able to see global packages, but we don't want to mess with them.
    """
    loc = os.path.normcase(pkg_resources.working_set.by_key[name].location)
    pre = os.path.normcase(sys.prefix)
    return os.path.commonprefix([loc, pre]) == pre 
Example #19
Source File: base.py    From calmjs with GNU General Public License v2.0 5 votes vote down vote up
def __init__(self, registry_name, *a, **kw):
        """
        Arguments:

        registry_name
            The name of this registry.
        """

        # The container for the resolved item.
        self.records = OrderedDict()
        self.registry_name = registry_name
        _working_set = kw.pop('_working_set', working_set)
        self.raw_entry_points = [] if _working_set is None else list(
            _working_set.iter_entry_points(self.registry_name))
        self._init(*a, **kw) 
Example #20
Source File: entrypoints.py    From udata with GNU Affero General Public License v3.0 5 votes vote down vote up
def known_dists():
    '''Return a list of all Distributions exporting udata.* entrypoints'''
    return (
        dist for dist in pkg_resources.working_set
        if any(k in ENTRYPOINTS for k in dist.get_entry_map().keys())
    ) 
Example #21
Source File: ah_bootstrap.py    From grizli with MIT License 5 votes vote down vote up
def _directory_import(self):
        """
        Import astropy_helpers from the given path, which will be added to
        sys.path.

        Must return True if the import succeeded, and False otherwise.
        """

        # Return True on success, False on failure but download is allowed, and
        # otherwise raise SystemExit
        path = os.path.abspath(self.path)

        # Use an empty WorkingSet rather than the man
        # pkg_resources.working_set, since on older versions of setuptools this
        # will invoke a VersionConflict when trying to install an upgrade
        ws = pkg_resources.WorkingSet([])
        ws.add_entry(path)
        dist = ws.by_key.get(DIST_NAME)

        if dist is None:
            # We didn't find an egg-info/dist-info in the given path, but if a
            # setup.py exists we can generate it
            setup_py = os.path.join(path, 'setup.py')
            if os.path.isfile(setup_py):
                # We use subprocess instead of run_setup from setuptools to
                # avoid segmentation faults - see the following for more details:
                # https://github.com/cython/cython/issues/2104
                sp.check_output([sys.executable, 'setup.py', 'egg_info'], cwd=path)

                for dist in pkg_resources.find_distributions(path, True):
                    # There should be only one...
                    return dist

        return dist 
Example #22
Source File: test_android.py    From chaquopy with MIT License 5 votes vote down vote up
def test_pr_distributions(self):
        import pkg_resources as pr
        self.assertCountEqual(["chaquopy-libcxx", "murmurhash", "Pygments"],
                              [dist.project_name for dist in pr.working_set])
        self.assertEqual("0.28.0", pr.get_distribution("murmurhash").version) 
Example #23
Source File: sync_requirements.py    From rally-openstack with Apache License 2.0 5 votes vote down vote up
def update_upper_constraints():
    """Obtains latest version of packages and put them to upper-constraints."""
    LOG.info("Obtaining upper-constrains from OpenStack...")
    raw_g_uc = _fetch_from_gr("upper-constraints.txt")
    # NOTE(andreykurilin): global OpenStack upper-constraints file includes
    #   comments which can be unrelated to Rally project, so let's just ignore
    #   them.
    global_uc = parse_data(raw_g_uc,
                           include_comments=False,
                           dependency_cls=UpperConstraint)

    our_uc = [UpperConstraint(package_name=p.project_name, version=p.version)
              for p in pkg_resources.working_set
              # do not include the current package at u-c
              if p.project_name != "rally-openstack"]

    for package in our_uc:
        if package.package_name in global_uc:
            # we cannot use whatever we want versions in CI. OpenStack CI
            # ignores versions listed in requirements of
            # particular project and use versions from global u-c file.
            # It means that we need to suggest to use the same versions
            package.update(global_uc[package.package_name].version)

    our_uc = sorted(our_uc, key=lambda o: o.package_name.upper())
    _write_requirements("upper-constraints.txt", our_uc) 
Example #24
Source File: ah_bootstrap.py    From specidentify with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def _directory_import(self):
        """
        Import astropy_helpers from the given path, which will be added to
        sys.path.

        Must return True if the import succeeded, and False otherwise.
        """

        # Return True on success, False on failure but download is allowed, and
        # otherwise raise SystemExit
        path = os.path.abspath(self.path)

        # Use an empty WorkingSet rather than the man
        # pkg_resources.working_set, since on older versions of setuptools this
        # will invoke a VersionConflict when trying to install an upgrade
        ws = pkg_resources.WorkingSet([])
        ws.add_entry(path)
        dist = ws.by_key.get(DIST_NAME)

        if dist is None:
            # We didn't find an egg-info/dist-info in the given path, but if a
            # setup.py exists we can generate it
            setup_py = os.path.join(path, 'setup.py')
            if os.path.isfile(setup_py):
                with _silence():
                    run_setup(os.path.join(path, 'setup.py'),
                              ['egg_info'])

                for dist in pkg_resources.find_distributions(path, True):
                    # There should be only one...
                    return dist

        return dist 
Example #25
Source File: pip_pkg.py    From BentoML with Apache License 2.0 5 votes vote down vote up
def __init__(self):
        super(ModuleManager, self).__init__()
        self.pip_pkg_map = {}
        self.pip_module_map = {}
        self.setuptools_module_set = set()
        self.nonlocal_package_path = set()
        for dist in pkg_resources.working_set:  # pylint: disable=not-an-iterable
            self.nonlocal_package_path.add(dist.module_path)
            self.pip_pkg_map[dist._key] = dist._version
            for mn in dist._get_metadata("top_level.txt"):
                if dist._key != "setuptools":
                    self.pip_module_map.setdefault(mn, []).append(
                        (dist._key, dist._version)
                    )
                else:
                    self.setuptools_module_set.add(mn)

        self.searched_modules = {}
        for m in pkgutil.iter_modules():
            if isinstance(m.module_finder, zipimport.zipimporter):
                logger.warning(f"Skipped unsupported zipimporter {m.module_finder}")
                continue
            if m.name not in self.searched_modules:
                path = m.module_finder.path
                is_local = self.is_local_path(path)
                self.searched_modules[m.name] = ModuleInfo(
                    m.name, path, is_local, m.ispkg
                ) 
Example #26
Source File: ah_bootstrap.py    From banzai with GNU General Public License v3.0 5 votes vote down vote up
def _directory_import(self):
        """
        Import astropy_helpers from the given path, which will be added to
        sys.path.

        Must return True if the import succeeded, and False otherwise.
        """

        # Return True on success, False on failure but download is allowed, and
        # otherwise raise SystemExit
        path = os.path.abspath(self.path)

        # Use an empty WorkingSet rather than the man
        # pkg_resources.working_set, since on older versions of setuptools this
        # will invoke a VersionConflict when trying to install an upgrade
        ws = pkg_resources.WorkingSet([])
        ws.add_entry(path)
        dist = ws.by_key.get(DIST_NAME)

        if dist is None:
            # We didn't find an egg-info/dist-info in the given path, but if a
            # setup.py exists we can generate it
            setup_py = os.path.join(path, 'setup.py')
            if os.path.isfile(setup_py):
                # We use subprocess instead of run_setup from setuptools to
                # avoid segmentation faults - see the following for more details:
                # https://github.com/cython/cython/issues/2104
                sp.check_output([sys.executable, 'setup.py', 'egg_info'], cwd=path)

                for dist in pkg_resources.find_distributions(path, True):
                    # There should be only one...
                    return dist

        return dist 
Example #27
Source File: __init__.py    From ops-cli with Apache License 2.0 5 votes vote down vote up
def validate_ops_version(min_ops_version):
    current_ops_version = [
        x.version for x in pkg_resources.working_set if x.project_name == "ops-cli"][0]
    if StrictVersion(current_ops_version) < StrictVersion(min_ops_version):
        raise Exception("The current ops version {0} is lower than the minimum required version {1}. "
                        "Please upgrade by following the instructions seen here: "
                        "https://github.com/adobe/ops-cli#installing".format(current_ops_version, min_ops_version)) 
Example #28
Source File: urls.py    From simonwillisonblog with Apache License 2.0 5 votes vote down vote up
def versions(request):
    installed_packages = [
        (d.project_name, d.version) for d in sorted(pkg_resources.working_set, key=lambda d: d.project_name.lower())
    ]
    return HttpResponse(
        json.dumps(installed_packages, indent=4), content_type="text/plain"
    ) 
Example #29
Source File: setup.py    From snet-cli with MIT License 5 votes vote down vote up
def is_package_installed(package_name):
    installed_modules = [p.project_name for p in pkg_resources.working_set]
    print("Installed modules:")
    print(installed_modules)
    return package_name in installed_modules 
Example #30
Source File: common.py    From experiment-impact-tracker with MIT License 5 votes vote down vote up
def get_python_packages_and_versions(*args, **kwargs):
    return list(pkg_resources.working_set)