Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow importlib_metadata.distributions() to enumerate distributions under a given path #85

Closed
jaraco opened this issue Oct 22, 2020 · 8 comments
Milestone

Comments

@jaraco
Copy link
Member

jaraco commented Oct 22, 2020

In GitLab by @yan12125 on Sep 6, 2019, 08:27

I'm working on porting a script from pkg_resources to importlib_metadata. A missing feature is that I cannot enumerate distributions under a given path easily.

With pkg_resources, I do this:

for dist in pkg_resources.WorkingSet([some_path]):
    analyze_disttribution(dist)

importlib_metadata.distributions() appears to be the replacement. However, I need to manipulate sys.path manually:

orig_sys_path = sys.path
sys.path = [some_path]
for dist in importlib_metadata.distributions():
    analyze_disttribution(dist)
sys.path = orig_sys_path

Could you consider adding a parameter to importlib_metadata.distributions()? Here is a proof-of-concept patch:

diff --git a/importlib_metadata/__init__.py b/importlib_metadata/__init__.py
index 2583c3f..cf535bc 100644
--- a/importlib_metadata/__init__.py
+++ b/importlib_metadata/__init__.py
@@ -184,13 +184,13 @@ class Distribution:
             raise PackageNotFoundError(name)
 
     @classmethod
-    def discover(cls):
+    def discover(cls, path=None):
         """Return an iterable of Distribution objects for all packages.
 
         :return: Iterable of Distribution objects for all packages.
         """
         return itertools.chain.from_iterable(
-            resolver()
+            resolver(path=path)
             for resolver in cls._discover_resolvers()
             )
 
@@ -430,12 +430,12 @@ def distribution(package):
     return Distribution.from_name(package)
 
 
-def distributions():
+def distributions(path=None):
     """Get all ``Distribution`` instances in the current environment.
 
     :return: An iterable of ``Distribution`` instances.
     """
-    return Distribution.discover()
+    return Distribution.discover(path)
 
 
 def metadata(package):
@jaraco jaraco added this to the 0.21 milestone Oct 22, 2020
@jaraco
Copy link
Member Author

jaraco commented Oct 22, 2020

In GitLab by @yan12125 on Sep 6, 2019, 09:23

mentioned in commit yan12125/importlib_metadata@228e4447cfb9df12f2f636f96e3f8c0037aa75af

@jaraco
Copy link
Member Author

jaraco commented Oct 22, 2020

In GitLab by @jaraco on Sep 9, 2019, 09:25

Thanks for reporting this. This use-case is similar to #80, but distinct in that you're wishing to discover distributions not on sys.path. Perhaps the solution to one should be the solution to the other.

@jaraco
Copy link
Member Author

jaraco commented Oct 22, 2020

In GitLab by @jaraco on Sep 9, 2019, 09:50

I agree, the patch suggested is a good one and serves the purpose. I worry a little bit about the proliferation of the name and path parameters being passed through these various functions, so I'm tempted to do something like:

def distributions(**kwargs):
    ...

But even that would only work for whatever parameters the find_distributions method accepts, which is defined by the DistributionFinder metaclass. In 827575ffd146f581fdf9b24d474f1eec6367cc5b (!88), I've made the definition of that metaclass part of the public, documented interface for better clarity, but I worry that the two parameters of that function may be insufficient for some future use-case. Perhaps better that function parameters would be for that DistributionFinder.find_distributions to accept a single parameter, something like context, which is itself an instance of an ABC. It would have optional .name and .path properties, but could also have other arbitrary properties that other DistributionFinders might require.

@jaraco
Copy link
Member Author

jaraco commented Oct 22, 2020

In GitLab by @jaraco on Sep 9, 2019, 11:06

mentioned in merge request !89

@jaraco
Copy link
Member Author

jaraco commented Oct 22, 2020

In GitLab by @jaraco on Sep 9, 2019, 13:18

mentioned in commit c5f09ad

@jaraco
Copy link
Member Author

jaraco commented Oct 22, 2020

In GitLab by @jaraco on Sep 10, 2019, 11:18

closed via commit 4b86813

@jaraco
Copy link
Member Author

jaraco commented Oct 22, 2020

In GitLab by @jaraco on Sep 10, 2019, 11:29

mentioned in commit 4b86813

@jaraco
Copy link
Member Author

jaraco commented Oct 22, 2020

In GitLab by @yan12125 on Sep 14, 2019, 13:27

Thank you! Your design is great and it suits my needs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant