Skip to content

Commit

Permalink
replace pkg_resources with importlib.metadata in pdistreport script
Browse files Browse the repository at this point in the history
  • Loading branch information
mmerickel committed Feb 5, 2024
1 parent a6fe1a1 commit 510dd7a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 24 deletions.
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
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=}'

0 comments on commit 510dd7a

Please sign in to comment.