From e02853fed546838f1bc1d1a9b6ff68adf8e63eb1 Mon Sep 17 00:00:00 2001 From: Michael Merickel Date: Sun, 15 May 2022 22:44:24 -0500 Subject: initial setup removing importlib-metadata --- docs/conf.py | 7 +++++-- setup.cfg | 4 ++-- src/paste/deploy/config.py | 3 --- src/paste/deploy/util.py | 12 ++++++++++++ tests/__init__.py | 7 +++---- tests/fixture.py | 19 ------------------- tests/test_load_package.py | 5 ++--- 7 files changed, 24 insertions(+), 33 deletions(-) delete mode 100644 tests/fixture.py diff --git a/docs/conf.py b/docs/conf.py index bf4faa8..da36975 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -12,7 +12,10 @@ # serve to show the default value. import datetime -import pkg_resources +try: + import importlib.metadata as importlib_metadata +except ImportError: + import importlib_metadata import pylons_sphinx_themes # If your extensions are in another directory, add it here. @@ -51,7 +54,7 @@ copyright = '2011-%s, Ian Bicking and contributors' % thisyear # other places throughout the built documents. # # The short X.Y version. -version = pkg_resources.get_distribution('pastedeploy').version +version = importlib_metadata.distribution('pastedeploy').version # The full version, including alpha/beta/rc tags. release = version diff --git a/setup.cfg b/setup.cfg index 4d81c86..2b896a7 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,6 +1,6 @@ [metadata] name = PasteDeploy -version = 2.1.1 +version = 3.0.dev0 author = Ian Bicking author_email = pylons-discuss@googlegroups.com maintainer = Chris Dent @@ -44,7 +44,7 @@ package_dir = packages = find: zip_safe = False install_requires = - setuptools + importlib-metadata; python_version<"3.8" include_package_data = True namespace_packages = paste diff --git a/src/paste/deploy/config.py b/src/paste/deploy/config.py index b174fa0..c9714fe 100644 --- a/src/paste/deploy/config.py +++ b/src/paste/deploy/config.py @@ -155,9 +155,6 @@ class ConfigMiddleware: def __call__(self, environ, start_response): global wsgilib if wsgilib is None: - import pkg_resources - - pkg_resources.require('Paste') from paste import wsgilib popped_config = None if 'paste.config' in environ: diff --git a/src/paste/deploy/util.py b/src/paste/deploy/util.py index 6bd6132..1e9c08f 100644 --- a/src/paste/deploy/util.py +++ b/src/paste/deploy/util.py @@ -3,6 +3,18 @@ import inspect import sys +try: + import importlib.metadata as importlib_metadata +except ImportError: + # bw-compat shim for py37 + import importlib_metadata + + +def find_entry_point(dist, group, name): + for entry in dist.entry_points: + if entry.name == name and entry.group == group: + return entry + def fix_type_error(exc_info, callable, varargs, kwargs): """ diff --git a/tests/__init__.py b/tests/__init__.py index 555181c..8ba6060 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -17,12 +17,11 @@ if not os.path.exists(egg_info_dir): sys.path.append(os.path.dirname(egg_info_dir)) -import pkg_resources # noqa E402 +from paste.deploy.util import importlib_metadata # noqa E402 # Make absolutely sure we're testing *this* package, not # some other installed package -pkg_resources.require('PasteDeploy') +importlib_metadata.distribution('PasteDeploy') # ensure FakeApp is available for use by tests -pkg_resources.working_set.add_entry(os.path.dirname(egg_info_dir)) -pkg_resources.require('FakeApp') +importlib_metadata.distribution('FakeApp') diff --git a/tests/fixture.py b/tests/fixture.py deleted file mode 100644 index 6d92260..0000000 --- a/tests/fixture.py +++ /dev/null @@ -1,19 +0,0 @@ -import os -import shutil -import sys - -test_dir = os.path.dirname(__file__) -egg_info_dir = os.path.join(test_dir, 'fake_packages', 'FakeApp.egg', 'EGG-INFO') -info_dir = os.path.join(test_dir, 'fake_packages', 'FakeApp.egg', 'FakeApp.egg-info') -if not os.path.exists(egg_info_dir): - try: - os.symlink(info_dir, egg_info_dir) - except Exception: - shutil.copytree(info_dir, egg_info_dir) - -sys.path.append(os.path.dirname(egg_info_dir)) - -import pkg_resources # noqa E402 - -pkg_resources.working_set.add_entry(os.path.dirname(egg_info_dir)) -pkg_resources.require('FakeApp') diff --git a/tests/test_load_package.py b/tests/test_load_package.py index bdd6e3f..bf919cc 100644 --- a/tests/test_load_package.py +++ b/tests/test_load_package.py @@ -1,10 +1,9 @@ from pprint import pprint import sys -import pkg_resources - def test_load_package(): + from paste.deploy.util import importlib_metadata print('Path:') pprint(sys.path) - print(pkg_resources.require('FakeApp')) + importlib_metadata.distribution('FakeApp') -- cgit v1.2.1 From ef58c20961072e4f60c26e94564180c304af7144 Mon Sep 17 00:00:00 2001 From: Michael Merickel Date: Sun, 15 May 2022 23:11:21 -0500 Subject: replace pkg_resources uses with importlib_metadata --- docs/conf.py | 1 + src/paste/deploy/loadwsgi.py | 54 +++++++++++++++----------------------------- src/paste/deploy/util.py | 6 ----- tests/test_load_package.py | 1 + 4 files changed, 20 insertions(+), 42 deletions(-) diff --git a/docs/conf.py b/docs/conf.py index da36975..9e5e04f 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -12,6 +12,7 @@ # serve to show the default value. import datetime + try: import importlib.metadata as importlib_metadata except ImportError: diff --git a/src/paste/deploy/loadwsgi.py b/src/paste/deploy/loadwsgi.py index 482b339..bde8a08 100644 --- a/src/paste/deploy/loadwsgi.py +++ b/src/paste/deploy/loadwsgi.py @@ -6,9 +6,7 @@ import re import sys from urllib.parse import unquote -import pkg_resources - -from paste.deploy.util import fix_call, lookup_object +from paste.deploy.util import fix_call, importlib_metadata, lookup_object __all__ = ['loadapp', 'loadserver', 'loadfilter', 'appconfig'] @@ -18,14 +16,10 @@ __all__ = ['loadapp', 'loadserver', 'loadfilter', 'appconfig'] ############################################################ -def import_string(s): - ep = pkg_resources.EntryPoint.parse("x=" + s) - if hasattr(ep, 'resolve'): - # this is available on setuptools >= 10.2 - return ep.resolve() - else: - # this causes a DeprecationWarning on setuptools >= 11.3 - return ep.load(False) +def find_entry_point(dist, group, name): + for entry in dist.entry_points: + if entry.name == name and entry.group == group: + return entry def _aslist(obj): @@ -443,7 +437,7 @@ class ConfigLoader(_Loader): filter_with = None if 'require' in local_conf: for spec in local_conf['require'].split(): - pkg_resources.require(spec) + importlib_metadata.distribution(spec) del local_conf['require'] if section.startswith('filter-app:'): context = self._filter_app_context( @@ -532,7 +526,9 @@ class ConfigLoader(_Loader): raise LookupError("No loader given in section %r" % section) found_protocol, found_expr = possible[0] del local_conf[found_protocol] - value = import_string(found_expr) + value = importlib_metadata.EntryPoint( + name=None, group=None, value=found_expr + ).load() context = LoaderContext( value, object_type, found_protocol, global_conf, local_conf, self ) @@ -649,51 +645,37 @@ class EggLoader(_Loader): global_conf or {}, {}, self, - distribution=pkg_resources.get_distribution(self.spec), + distribution=importlib_metadata.distribution(self.spec), entry_point_name=ep_name, ) def find_egg_entry_point(self, object_type, name=None): """ - Returns the (entry_point, protocol) for the with the given - ``name``. + Returns the (entry_point, protocol) for with the given ``name``. """ if name is None: name = 'main' + dist = importlib_metadata.distribution(self.spec) possible = [] for protocol_options in object_type.egg_protocols: for protocol in protocol_options: - pkg_resources.require(self.spec) - entry = pkg_resources.get_entry_info(self.spec, protocol, name) + entry = find_entry_point(dist, protocol, name) if entry is not None: possible.append((entry.load(), protocol, entry.name)) break if not possible: # Better exception - dist = pkg_resources.get_distribution(self.spec) raise LookupError( - "Entry point %r not found in egg %r (dir: %s; protocols: %s; " - "entry_points: %s)" + "Entry point %r not found in egg %r (protocols: %s; entry_points: %s)" % ( name, self.spec, - dist.location, ', '.join(_flatten(object_type.egg_protocols)), ', '.join( - _flatten( - [ - list( - ( - pkg_resources.get_entry_info( - self.spec, prot, name - ) - or {} - ).keys() - ) - for prot in protocol_options - ] - or '(no entry points)' - ) + str(entry) + for prot in protocol_options + for entry in [find_entry_point(dist, prot, name)] + if entry ), ) ) diff --git a/src/paste/deploy/util.py b/src/paste/deploy/util.py index 1e9c08f..f8ce2aa 100644 --- a/src/paste/deploy/util.py +++ b/src/paste/deploy/util.py @@ -10,12 +10,6 @@ except ImportError: import importlib_metadata -def find_entry_point(dist, group, name): - for entry in dist.entry_points: - if entry.name == name and entry.group == group: - return entry - - def fix_type_error(exc_info, callable, varargs, kwargs): """ Given an exception, this will test if the exception was due to a diff --git a/tests/test_load_package.py b/tests/test_load_package.py index bf919cc..b0d5031 100644 --- a/tests/test_load_package.py +++ b/tests/test_load_package.py @@ -4,6 +4,7 @@ import sys def test_load_package(): from paste.deploy.util import importlib_metadata + print('Path:') pprint(sys.path) importlib_metadata.distribution('FakeApp') -- cgit v1.2.1 From d5cf916571cb66d6e1e29c30550c8b8aa7718c56 Mon Sep 17 00:00:00 2001 From: Michael Merickel Date: Sun, 15 May 2022 23:45:41 -0500 Subject: fix lint and enable coverage reports --- .github/workflows/ci-tests.yml | 24 ++++++++++++------------ src/paste/deploy/util.py | 6 +++--- tox.ini | 3 +-- 3 files changed, 16 insertions(+), 17 deletions(-) diff --git a/.github/workflows/ci-tests.yml b/.github/workflows/ci-tests.yml index 7ada44c..a7a717d 100644 --- a/.github/workflows/ci-tests.yml +++ b/.github/workflows/ci-tests.yml @@ -45,19 +45,19 @@ jobs: - run: pip install tox - name: Running tox run: tox -e py - # coverage: - # runs-on: ubuntu-latest - # name: Validate coverage - # steps: - # - uses: actions/checkout@v2 - # - name: Setup python 3.10 - # uses: actions/setup-python@v2 - # with: - # python-version: "3.10" - # architecture: x64 + coverage: + runs-on: ubuntu-latest + name: Validate coverage + steps: + - uses: actions/checkout@v2 + - name: Setup python 3.10 + uses: actions/setup-python@v2 + with: + python-version: "3.10" + architecture: x64 - # - run: pip install tox - # - run: tox -e py310,coverage + - run: pip install tox + - run: tox -e py310,coverage docs: runs-on: ubuntu-latest name: Build the documentation diff --git a/src/paste/deploy/util.py b/src/paste/deploy/util.py index f8ce2aa..a9c20d6 100644 --- a/src/paste/deploy/util.py +++ b/src/paste/deploy/util.py @@ -4,10 +4,10 @@ import inspect import sys try: - import importlib.metadata as importlib_metadata -except ImportError: + import importlib.metadata as importlib_metadata # noqa F401 +except ImportError: # pragma: no cover # bw-compat shim for py37 - import importlib_metadata + import importlib_metadata # noqa F401 def fix_type_error(exc_info, callable, varargs, kwargs): diff --git a/tox.ini b/tox.ini index 18f23f6..4b9996b 100644 --- a/tox.ini +++ b/tox.ini @@ -17,8 +17,7 @@ setenv = [testenv:coverage] commands = coverage combine - coverage xml - coverage report --fail-under=100 + coverage report deps = coverage setenv = -- cgit v1.2.1 From 02f3609cb65280842b06eeeb31096445ee7a9eab Mon Sep 17 00:00:00 2001 From: Michael Merickel Date: Sun, 15 May 2022 23:47:10 -0500 Subject: add a note to news.rst --- docs/news.rst | 2 ++ 1 file changed, 2 insertions(+) diff --git a/docs/news.rst b/docs/news.rst index 05d8f9f..94deead 100644 --- a/docs/news.rst +++ b/docs/news.rst @@ -9,6 +9,8 @@ unreleased * Fix a broken compatibility shim that would cause the ConfigParser to fail on Python 3.12 when ``ConfigParser.readfp`` is removed. +* Drop setuptools dependency and start using ``importlib.metadata`` instead. + * Refactor repository into a src folder layout. 2.1.1 (2020-10-12) -- cgit v1.2.1