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

Remove pkg resources from scripts #3749

Merged
merged 4 commits into from
Feb 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
---------

Expand Down
14 changes: 7 additions & 7 deletions src/pyramid/scripts/pdistreport.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import argparse
import importlib.metadata
from operator import itemgetter
import pkg_resources
import platform
import sys

Expand All @@ -21,33 +21,33 @@ def get_parser():

def main(
argv=sys.argv,
pkg_resources=pkg_resources,
importlib_metadata=importlib.metadata,
platform=platform.platform,
out=out,
):
# all args except argv are for unit testing purposes only
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
Expand Down
14 changes: 10 additions & 4 deletions src/pyramid/scripts/pshell.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -228,10 +228,16 @@ def show_shells(self):
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
Expand Down
20 changes: 0 additions & 20 deletions tests/test_scripts/dummy.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,26 +154,6 @@ def __call__(self, *a, **kw):
}


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
Expand Down
34 changes: 17 additions & 17 deletions tests/test_scripts/test_pdistreport.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import email.message
import unittest


Expand All @@ -12,14 +13,14 @@ def test_no_dists(self):
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(
Expand All @@ -32,14 +33,14 @@ def platform():
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(
Expand All @@ -54,31 +55,30 @@ def out(*args):
'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=}'
38 changes: 37 additions & 1 deletion tests/test_scripts/test_pshell.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import sys
import unittest

from . import dummy
Expand Down Expand Up @@ -50,7 +51,10 @@ class Options:
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()
Expand Down Expand Up @@ -430,3 +434,35 @@ def _callFUT(self, argv):
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