Skip to content

Commit

Permalink
Remove deprecated pkg_resources
Browse files Browse the repository at this point in the history
  • Loading branch information
elacuesta committed Nov 29, 2024
1 parent 3a18d1d commit 27566a8
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 38 deletions.
22 changes: 5 additions & 17 deletions sh_scrapy/crawl.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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.
Expand Down
42 changes: 21 additions & 21 deletions tests/test_crawl.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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")
Expand Down

0 comments on commit 27566a8

Please sign in to comment.