Python importlib_metadata.distributions() Examples

The following are 11 code examples of importlib_metadata.distributions(). 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 importlib_metadata , or try the search function .
Example #1
Source File: __init__.py    From python-netsurv with MIT License 6 votes vote down vote up
def _mark_plugins_for_rewrite(self, hook):
        """
        Given an importhook, mark for rewrite any top-level
        modules or packages in the distribution package for
        all pytest plugins.
        """
        self.pluginmanager.rewrite_hook = hook

        if os.environ.get("PYTEST_DISABLE_PLUGIN_AUTOLOAD"):
            # We don't autoload from setuptools entry points, no need to continue.
            return

        package_files = (
            str(file)
            for dist in importlib_metadata.distributions()
            if any(ep.group == "pytest11" for ep in dist.entry_points)
            for file in dist.files or []
        )

        for name in _iter_rewritable_modules(package_files):
            hook.mark_rewrite(name) 
Example #2
Source File: manager.py    From python-netsurv with MIT License 6 votes vote down vote up
def load_setuptools_entrypoints(self, group, name=None):
        """ Load modules from querying the specified setuptools ``group``.

        :param str group: entry point group to load plugins
        :param str name: if given, loads only plugins with the given ``name``.
        :rtype: int
        :return: return the number of loaded plugins by this call.
        """
        count = 0
        for dist in importlib_metadata.distributions():
            for ep in dist.entry_points:
                if (
                    ep.group != group
                    or (name is not None and ep.name != name)
                    # already registered
                    or self.get_plugin(ep.name)
                    or self.is_blocked(ep.name)
                ):
                    continue
                plugin = ep.load()
                self.register(plugin, name=ep.name)
                self._plugin_distinfo.append((plugin, DistFacade(dist)))
                count += 1
        return count 
Example #3
Source File: __init__.py    From python-netsurv with MIT License 6 votes vote down vote up
def _mark_plugins_for_rewrite(self, hook):
        """
        Given an importhook, mark for rewrite any top-level
        modules or packages in the distribution package for
        all pytest plugins.
        """
        self.pluginmanager.rewrite_hook = hook

        if os.environ.get("PYTEST_DISABLE_PLUGIN_AUTOLOAD"):
            # We don't autoload from setuptools entry points, no need to continue.
            return

        package_files = (
            str(file)
            for dist in importlib_metadata.distributions()
            if any(ep.group == "pytest11" for ep in dist.entry_points)
            for file in dist.files or []
        )

        for name in _iter_rewritable_modules(package_files):
            hook.mark_rewrite(name) 
Example #4
Source File: manager.py    From python-netsurv with MIT License 6 votes vote down vote up
def load_setuptools_entrypoints(self, group, name=None):
        """ Load modules from querying the specified setuptools ``group``.

        :param str group: entry point group to load plugins
        :param str name: if given, loads only plugins with the given ``name``.
        :rtype: int
        :return: return the number of loaded plugins by this call.
        """
        count = 0
        for dist in importlib_metadata.distributions():
            for ep in dist.entry_points:
                if (
                    ep.group != group
                    or (name is not None and ep.name != name)
                    # already registered
                    or self.get_plugin(ep.name)
                    or self.is_blocked(ep.name)
                ):
                    continue
                plugin = ep.load()
                self.register(plugin, name=ep.name)
                self._plugin_distinfo.append((plugin, DistFacade(dist)))
                count += 1
        return count 
Example #5
Source File: entry_points.py    From ros2cli with Apache License 2.0 6 votes vote down vote up
def get_all_entry_points():
    """
    Get all entry points related to ``ros2cli`` and any of its extensions.

    :returns: mapping of entry point names to ``EntryPoint`` instances
    :rtype: dict
    """
    extension_points = get_entry_points(EXTENSION_POINT_GROUP_NAME)

    entry_points = defaultdict(dict)

    for dist in importlib_metadata.distributions():
        for ep in dist.entry_points:
            # skip groups which are not registered as extension points
            if ep.group not in extension_points:
                continue

            entry_points[ep.group][ep.name] = (dist, ep)
    return entry_points 
Example #6
Source File: manager.py    From pluggy with MIT License 6 votes vote down vote up
def load_setuptools_entrypoints(self, group, name=None):
        """ Load modules from querying the specified setuptools ``group``.

        :param str group: entry point group to load plugins
        :param str name: if given, loads only plugins with the given ``name``.
        :rtype: int
        :return: return the number of loaded plugins by this call.
        """
        count = 0
        for dist in importlib_metadata.distributions():
            for ep in dist.entry_points:
                if (
                    ep.group != group
                    or (name is not None and ep.name != name)
                    # already registered
                    or self.get_plugin(ep.name)
                    or self.is_blocked(ep.name)
                ):
                    continue
                plugin = ep.load()
                self.register(plugin, name=ep.name)
                self._plugin_distinfo.append((plugin, DistFacade(dist)))
                count += 1
        return count 
Example #7
Source File: test_main.py    From pipenv with MIT License 5 votes vote down vote up
def test_package_discovery(self):
        dists = list(distributions())
        assert all(
            isinstance(dist, Distribution)
            for dist in dists
            )
        assert any(
            dist.metadata['Name'] == 'egginfo-pkg'
            for dist in dists
            )
        assert any(
            dist.metadata['Name'] == 'distinfo-pkg'
            for dist in dists
            ) 
Example #8
Source File: test_main.py    From pipenv with MIT License 5 votes vote down vote up
def test_invalid_usage(self):
        with self.assertRaises(ValueError):
            list(distributions(context='something', name='else')) 
Example #9
Source File: test_main.py    From pipenv with MIT License 5 votes vote down vote up
def test_discovery(self):
        """
        Discovering distributions should succeed even if
        there is an invalid path on sys.path.
        """
        importlib_metadata.distributions() 
Example #10
Source File: test_main.py    From pipenv with MIT License 5 votes vote down vote up
def test_discovery(self):
        """
        Discovering distributions should succeed even if
        there is an invalid path on sys.path.
        """
        list(importlib_metadata.distributions()) 
Example #11
Source File: info.py    From chaostoolkit-lib with Apache License 2.0 5 votes vote down vote up
def list_extensions() -> List[ExtensionInfo]:
    """
    List all installed Chaos Toolkit extensions in the current environment.

    Notice, for now we can only list extensions that start with `chaostoolkit-`
    in their package name.

    This is not as powerful and solid as we want it to be. The trick is that we
    can't rely on any metadata inside extensions to tell us they exist and
    what functionnality they provide either. Python has the concept of trove
    classifiers on packages but we can't extend them yet so they are of no use
    to us.

    In a future version, we will provide a mechanism from packages to support
    a better detection.
    """
    infos = []
    distros = importlib_metadata.distributions()
    seen = []
    for dist in distros:
        info = dist.metadata
        name = info['Name']
        if name == "chaostoolkit-lib":
            continue
        if name in seen:
            continue
        seen.append(name)
        if name.startswith("chaostoolkit-"):
            ext = ExtensionInfo(
                name=name,
                version=info["Version"],
                summary=info["Summary"],
                license=info["License"],
                author=info["Author"],
                url=info["Home-page"])
            infos.append(ext)
    return infos