From 27566a81f6a904a606b1f8f4e31173063b3b92d7 Mon Sep 17 00:00:00 2001 From: Eugenio Lacuesta Date: Fri, 29 Nov 2024 09:36:11 -0300 Subject: [PATCH] Remove deprecated pkg_resources --- sh_scrapy/crawl.py | 22 +++++----------------- tests/test_crawl.py | 42 +++++++++++++++++++++--------------------- 2 files changed, 26 insertions(+), 38 deletions(-) diff --git a/sh_scrapy/crawl.py b/sh_scrapy/crawl.py index 7d75067..c34fd7e 100755 --- a/sh_scrapy/crawl.py +++ b/sh_scrapy/crawl.py @@ -3,6 +3,7 @@ # Add them below so that any import errors are caught and sent to sentry # ----------------------------------------------------------------------- from __future__ import print_function +import importlib.metadata import os import sys import socket @@ -124,19 +125,9 @@ def _run_pkgscript(argv): scriptname = argv[0] sys.argv = argv - try: - import importlib.metadata - has_importlib = True - except ImportError: - import pkg_resources - has_importlib = False - - def get_distribution(): - if has_importlib: - eps = importlib.metadata.entry_points(group='scrapy') - else: - eps = pkg_resources.WorkingSet().iter_entry_points('scrapy') + def get_distribution() -> importlib.metadata.Distribution: + eps = importlib.metadata.entry_points(group='scrapy') for ep in eps: if ep.name == 'settings': return ep.dist @@ -145,13 +136,10 @@ def get_distribution(): if not d: raise ValueError(SCRAPY_SETTINGS_ENTRYPOINT_NOT_FOUND) ns = {"__name__": "__main__"} - if has_importlib: - _run_script(d, scriptname, ns) - else: - d.run_script(scriptname, ns) + _run_script(d, scriptname, ns) -def _run_script(dist, script_name, namespace): +def _run_script(dist: importlib.metadata.Distribution, script_name: str, namespace: dict): # An importlib-based replacement for pkg_resources.NullProvider.run_script(). # It's possible that this doesn't support all cases that pkg_resources does, # so it may need to be improved when those are discovered. diff --git a/tests/test_crawl.py b/tests/test_crawl.py index 0d9f7af..e7b9fc6 100644 --- a/tests/test_crawl.py +++ b/tests/test_crawl.py @@ -121,14 +121,14 @@ def test_run_pkg_script(run_pkg_mock): assert run_pkg_mock.call_args[0] == (['py:script.py'],) -@unittest.skipIf(sys.version_info > (3,7), "Requires Python 3.7 or lower") -@mock.patch('pkg_resources.WorkingSet') -def test_run_pkg_script_distribution_not_found(working_set_class): - fake_set = mock.Mock() - fake_set.iter_entry_points.return_value = iter(()) - working_set_class.return_value = fake_set - with pytest.raises(ValueError): - _run(['py:script.py'], {'SETTING': 'VALUE'}) +# @unittest.skipIf(sys.version_info > (3,7), "Requires Python 3.7 or lower") +# @mock.patch('pkg_resources.WorkingSet') +# def test_run_pkg_script_distribution_not_found(working_set_class): +# fake_set = mock.Mock() +# fake_set.iter_entry_points.return_value = iter(()) +# working_set_class.return_value = fake_set +# with pytest.raises(ValueError): +# _run(['py:script.py'], {'SETTING': 'VALUE'}) @unittest.skipIf(sys.version_info < (3,8), "Requires Python 3.8 or higher") @mock.patch('importlib.metadata.entry_points') @@ -165,24 +165,24 @@ def get_working_set(working_set_class): return working_set -@unittest.skipIf(sys.version_info > (3,7), "Requires Python 3.7 or lower") -@mock.patch('pkg_resources.WorkingSet') -def test_run_pkgscript_base_usage(working_set_class): - working_set = get_working_set(working_set_class) - _run_pkgscript(['py:script.py', 'arg1', 'arg2']) - assert working_set.iter_entry_points.called - assert working_set.iter_entry_points.call_args[0] == ('scrapy',) - ep = working_set.iter_entry_points.return_value[0] - assert ep.dist.run_script.called - assert ep.dist.run_script.call_args[0] == ( - 'script.py', {'__name__': '__main__'}) - assert sys.argv == ['script.py', 'arg1', 'arg2'] +# @unittest.skipIf(sys.version_info > (3,7), "Requires Python 3.7 or lower") +# @mock.patch('pkg_resources.WorkingSet') +# def test_run_pkgscript_base_usage(working_set_class): +# working_set = get_working_set(working_set_class) +# _run_pkgscript(['py:script.py', 'arg1', 'arg2']) +# assert working_set.iter_entry_points.called +# assert working_set.iter_entry_points.call_args[0] == ('scrapy',) +# ep = working_set.iter_entry_points.return_value[0] +# assert ep.dist.run_script.called +# assert ep.dist.run_script.call_args[0] == ( +# 'script.py', {'__name__': '__main__'}) +# assert sys.argv == ['script.py', 'arg1', 'arg2'] def get_entry_points_mock(): """Helper to configure a fake entry point""" ep = mock.Mock() ep.name = 'settings' - ep.dist.run_script = mock.Mock() # only for the pkg_resources code path + # ep.dist.run_script = mock.Mock() # only for the pkg_resources code path return [ep] @unittest.skipIf(sys.version_info < (3,8), "Requires Python 3.8 or higher")