Author s0undt3ch
Recipients jaraco, s0undt3ch
Date 2020-11-17.06:56:20
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1605596181.46.0.7808505308.issue42382@roundup.psfhosted.org>
In-reply-to
Content
With `pkg_resources` an `EntryPoint` has a dist attribute which allows you to get the distribution that provided that specific entry-point, however, with `importlib.metafata` and `importlib_metadata` that's not an east task.

```python
USE_IMPORTLIB_METADATA_STDLIB = USE_IMPORTLIB_METADATA = False
try:
    # Py3.8+
    import importlib.metadata

    USE_IMPORTLIB_METADATA_STDLIB = True
except ImportError:
    # < Py3.8 backport package
    import importlib_metadata

    USE_IMPORTLIB_METADATA = True


def get_distribution_from_entry_point(entry_point):
    loaded_entry_point = entry_point.load()
    if isinstance(loaded_entry_point, types.ModuleType):
        module_path = loaded_entry_point.__file__
    else:
        module_path = sys.modules[loaded_entry_point.__module__].__file__
    if USE_IMPORTLIB_METADATA_STDLIB:
        distributions = importlib.metadata.distributions
    else:
        distributions = importlib_metadata.distributions

    for distribution in distributions():
        try:
            relative = pathlib.Path(module_path).relative_to(
                distribution.locate_file("")
            )
        except ValueError:
            pass
        else:
            if relative in distribution.files:
                return distribution
```

The above solution has the additional drawback that you're iterating the distributions list, once per EntryPoint, which, was already iterated to get the entry-points listing.

I propose we attach the Distribution instance to each of the found EntryPoint to avoid this low performance workaround.

I don't have an issue with providing a pull-request, but should that pull request be done against `importlib_metadata` or `importlib.metadata`?
History
Date User Action Args
2020-11-17 06:56:21s0undt3chsetrecipients: + s0undt3ch, jaraco
2020-11-17 06:56:21s0undt3chsetmessageid: <1605596181.46.0.7808505308.issue42382@roundup.psfhosted.org>
2020-11-17 06:56:21s0undt3chlinkissue42382 messages
2020-11-17 06:56:20s0undt3chcreate