summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Merickel <github@m.merickel.org>2024-02-04 23:55:45 -0700
committerGitHub <noreply@github.com>2024-02-04 23:55:45 -0700
commit151ebdc003a3a372017fdf73c14fbebcc550535d (patch)
treeff2b210b87aff4715450acc38d695a6c6a8abf61
parenta6fe1a1e38d6a3fb955fa950b671fc736ac9f4d0 (diff)
parent13dbb8507e9f1acaaad8caf74a1441e1f723a346 (diff)
downloadpyramid-151ebdc003a3a372017fdf73c14fbebcc550535d.tar.gz
pyramid-151ebdc003a3a372017fdf73c14fbebcc550535d.tar.bz2
pyramid-151ebdc003a3a372017fdf73c14fbebcc550535d.zip
Merge pull request #3749 from Pylons/remove-pkg-resources-from-scripts
Remove pkg resources from scripts
-rw-r--r--CHANGES.rst3
-rw-r--r--src/pyramid/scripts/pdistreport.py14
-rw-r--r--src/pyramid/scripts/pshell.py14
-rw-r--r--tests/test_scripts/dummy.py20
-rw-r--r--tests/test_scripts/test_pdistreport.py34
-rw-r--r--tests/test_scripts/test_pshell.py38
6 files changed, 74 insertions, 49 deletions
diff --git a/CHANGES.rst b/CHANGES.rst
index fd0b8e22f..96d496044 100644
--- a/CHANGES.rst
+++ b/CHANGES.rst
@@ -31,6 +31,9 @@ Features
- Replace usage of ``pkg_resources`` in ``pyramid.path.DottedNameResolver``.
See https://github.com/Pylons/pyramid/pull/3748
+- Replace usage of ``pkg_resources`` in ``pdistreport`` and ``pshell`` CLI
+ commands. See https://github.com/Pylons/pyramid/pull/3749
+
Bug Fixes
---------
diff --git a/src/pyramid/scripts/pdistreport.py b/src/pyramid/scripts/pdistreport.py
index 99615c1d5..eb41c17e7 100644
--- a/src/pyramid/scripts/pdistreport.py
+++ b/src/pyramid/scripts/pdistreport.py
@@ -1,6 +1,6 @@
import argparse
+import importlib.metadata
from operator import itemgetter
-import pkg_resources
import platform
import sys
@@ -21,7 +21,7 @@ def get_parser():
def main(
argv=sys.argv,
- pkg_resources=pkg_resources,
+ importlib_metadata=importlib.metadata,
platform=platform.platform,
out=out,
):
@@ -29,25 +29,25 @@ def main(
parser = get_parser()
parser.parse_args(argv[1:])
packages = []
- for distribution in pkg_resources.working_set:
- name = distribution.project_name
+ for distribution in importlib_metadata.distributions():
+ name = distribution.metadata['Name']
packages.append(
{
'version': distribution.version,
'lowername': name.lower(),
'name': name,
- 'location': distribution.location,
+ 'summary': distribution.metadata.get('Summary'),
}
)
packages = sorted(packages, key=itemgetter('lowername'))
- pyramid_version = pkg_resources.get_distribution('pyramid').version
+ pyramid_version = importlib_metadata.distribution('pyramid').version
plat = platform()
out('Pyramid version:', pyramid_version)
out('Platform:', plat)
out('Packages:')
for package in packages:
out(' ', package['name'], package['version'])
- out(' ', package['location'])
+ out(' ', package['summary'])
if __name__ == '__main__': # pragma: no cover
diff --git a/src/pyramid/scripts/pshell.py b/src/pyramid/scripts/pshell.py
index 63600abaf..4d3817ffb 100644
--- a/src/pyramid/scripts/pshell.py
+++ b/src/pyramid/scripts/pshell.py
@@ -1,8 +1,8 @@
import argparse
from code import interact
from contextlib import contextmanager
+import importlib.metadata
import os
-import pkg_resources
import sys
import textwrap
@@ -41,7 +41,7 @@ class PShellCommand:
script_name = 'pshell'
bootstrap = staticmethod(bootstrap) # for testing
get_config_loader = staticmethod(get_config_loader) # for testing
- pkg_resources = pkg_resources # for testing
+ importlib_metadata = importlib.metadata # for testing
parser = argparse.ArgumentParser(
description=textwrap.dedent(description),
@@ -228,10 +228,16 @@ class PShellCommand:
return 0
def find_all_shells(self):
- pkg_resources = self.pkg_resources
+ importlib_metadata = self.importlib_metadata
shells = {}
- for ep in pkg_resources.iter_entry_points('pyramid.pshell_runner'):
+ eps = importlib_metadata.entry_points()
+ if hasattr(eps, 'select'):
+ eps = eps.select(group='pyramid.pshell_runner')
+ else: # pragma: no cover
+ # fallback for py38 and py39
+ eps = eps.get('pyramid.pshell_runner')
+ for ep in eps:
name = ep.name
shell_factory = ep.load()
shells[name] = shell_factory
diff --git a/tests/test_scripts/dummy.py b/tests/test_scripts/dummy.py
index 5768f071e..758d3c9fd 100644
--- a/tests/test_scripts/dummy.py
+++ b/tests/test_scripts/dummy.py
@@ -154,26 +154,6 @@ class DummyBootstrap:
}
-class DummyEntryPoint:
- def __init__(self, name, module):
- self.name = name
- self.module = module
-
- def load(self):
- return self.module
-
-
-class DummyPkgResources:
- def __init__(self, entry_point_values):
- self.entry_points = []
-
- for name, module in entry_point_values.items():
- self.entry_points.append(DummyEntryPoint(name, module))
-
- def iter_entry_points(self, name):
- return self.entry_points
-
-
class dummy_setup_logging:
def __call__(self, config_uri, global_conf):
self.config_uri = config_uri
diff --git a/tests/test_scripts/test_pdistreport.py b/tests/test_scripts/test_pdistreport.py
index 079722734..2bcaad0b3 100644
--- a/tests/test_scripts/test_pdistreport.py
+++ b/tests/test_scripts/test_pdistreport.py
@@ -1,3 +1,4 @@
+import email.message
import unittest
@@ -12,14 +13,14 @@ class TestPDistReportCommand(unittest.TestCase):
def platform():
return 'myplatform'
- pkg_resources = DummyPkgResources()
+ importlib_metadata = DummyImportlibMetadata()
L = []
def out(*args):
L.extend(args)
result = self._callFUT(
- pkg_resources=pkg_resources, platform=platform, out=out
+ importlib_metadata=importlib_metadata, platform=platform, out=out
)
self.assertEqual(result, None)
self.assertEqual(
@@ -32,14 +33,14 @@ class TestPDistReportCommand(unittest.TestCase):
return 'myplatform'
working_set = (DummyDistribution('abc'), DummyDistribution('def'))
- pkg_resources = DummyPkgResources(working_set)
+ importlib_metadata = DummyImportlibMetadata(working_set)
L = []
def out(*args):
L.extend(args)
result = self._callFUT(
- pkg_resources=pkg_resources, platform=platform, out=out
+ importlib_metadata=importlib_metadata, platform=platform, out=out
)
self.assertEqual(result, None)
self.assertEqual(
@@ -54,31 +55,30 @@ class TestPDistReportCommand(unittest.TestCase):
'abc',
'1',
' ',
- '/projects/abc',
+ 'summary for name=\'abc\'',
' ',
'def',
'1',
' ',
- '/projects/def',
+ 'summary for name=\'def\'',
],
)
-class DummyPkgResources:
- def __init__(self, working_set=()):
- self.working_set = working_set
+class DummyImportlibMetadata:
+ def __init__(self, distributions=()):
+ self._distributions = distributions
- def get_distribution(self, name):
- return Version('1')
+ def distribution(self, name):
+ return DummyDistribution(name)
-
-class Version:
- def __init__(self, version):
- self.version = version
+ def distributions(self):
+ return iter(self._distributions)
class DummyDistribution:
def __init__(self, name):
- self.project_name = name
self.version = '1'
- self.location = '/projects/%s' % name
+ self.metadata = email.message.Message()
+ self.metadata['Name'] = name
+ self.metadata['Summary'] = f'summary for {name=}'
diff --git a/tests/test_scripts/test_pshell.py b/tests/test_scripts/test_pshell.py
index de7e17496..a5eeee045 100644
--- a/tests/test_scripts/test_pshell.py
+++ b/tests/test_scripts/test_pshell.py
@@ -1,4 +1,5 @@
import os
+import sys
import unittest
from . import dummy
@@ -50,7 +51,10 @@ class TestPShellCommand(unittest.TestCase):
return cmd
def _makeEntryPoints(self, command, shells):
- command.pkg_resources = dummy.DummyPkgResources(shells)
+ entry_points = [
+ DummyEntryPoint(name, value) for name, value in shells.items()
+ ]
+ command.importlib_metadata = DummyImportlibMetadata(entry_points)
def test_command_loads_default_shell(self):
command = self._makeOne()
@@ -430,3 +434,35 @@ class Test_main(unittest.TestCase):
def test_it(self):
result = self._callFUT(['pshell'])
self.assertEqual(result, 2)
+
+
+class DummyImportlibMetadata:
+ def __init__(self, entry_points):
+ self._entry_points = entry_points
+
+ def entry_points(self):
+ return DummyEntryPoints(self._entry_points)
+
+
+class DummyEntryPoints:
+ def __init__(self, entry_points):
+ self._entry_points = entry_points
+
+ if sys.version_info >= (3, 10):
+
+ def select(self, **kw):
+ return iter(self._entry_points)
+
+ else: # pragma no cover
+
+ def get(self, key):
+ return list(self._entry_points)
+
+
+class DummyEntryPoint:
+ def __init__(self, name, value):
+ self.name = name
+ self.value = value
+
+ def load(self):
+ return self.value