Skip to content

Commit a2037ed

Browse files
committed
Partial changes from adamtheturtle#93
1 parent 9f6a653 commit a2037ed

File tree

7 files changed

+70
-59
lines changed

7 files changed

+70
-59
lines changed

pip_check_reqs/common.py

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,11 @@
1313

1414
from . import __version__
1515

16-
from pip._internal.network.session import PipSession
17-
from pip._internal.req.constructors import install_req_from_line
16+
# Between different versions of pip the location of PipSession has changed.
17+
try:
18+
from pip._internal.network.session import PipSession
19+
except ImportError: # pragma: no cover
20+
from pip._internal.download import PipSession
1821
from pip._internal.req.req_file import parse_requirements
1922

2023

@@ -134,9 +137,16 @@ def find_required_modules(options, requirements_filename: str):
134137
explicit = set()
135138
for requirement in parse_requirements(requirements_filename,
136139
session=PipSession()):
137-
requirement_name = install_req_from_line(
138-
requirement.requirement,
139-
).name
140+
try:
141+
requirement_name = requirement.name
142+
# The type of "requirement" changed between pip versions.
143+
# We exclude the "except" from coverage so that on any pip version we
144+
# can report 100% coverage.
145+
except AttributeError: # pragma: no cover
146+
from pip._internal.req.constructors import install_req_from_line
147+
requirement_name = install_req_from_line(
148+
requirement.requirement,
149+
).name
140150

141151
if options.ignore_reqs(requirement):
142152
log.debug('ignoring requirement: %s', requirement_name)
@@ -183,10 +193,13 @@ def ignorer(ignore_cfg):
183193
def f(candidate, ignore_cfg=ignore_cfg):
184194
for ignore in ignore_cfg:
185195
try:
186-
candidate_path = install_req_from_line(
196+
from pip._internal.req.constructors import (
197+
install_req_from_line,
198+
)
199+
candidate_path = install_req_from_line( # pragma: no cover
187200
candidate.requirement,
188201
).name
189-
except AttributeError:
202+
except (ImportError, AttributeError):
190203
try:
191204
candidate_path = candidate.name
192205
except AttributeError:

pip_check_reqs/find_extra_reqs.py

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -27,21 +27,26 @@ def find_extra_reqs(options, requirements_filename):
2727
)
2828

2929
for package in search_packages_info(all_pkgs):
30-
package_name = package.name
31-
package_location = package.location
32-
package_files = []
33-
for item in (package.files or []):
34-
here = pathlib.Path('.').resolve()
35-
item_location_rel = (pathlib.Path(package_location) / item)
36-
item_location = item_location_rel.resolve()
37-
try:
38-
relative_item_location = item_location.relative_to(here)
39-
except ValueError:
40-
# Ideally we would use Pathlib.is_relative_to rather than
41-
# checking for a ValueError, but that is only available in
42-
# Python 3.9+.
43-
relative_item_location = item_location
44-
package_files.append(str(relative_item_location))
30+
if isinstance(package, dict): # pragma: no cover
31+
package_name = package['name']
32+
package_location = package['location']
33+
package_files = package.get('files', []) or []
34+
else: # pragma: no cover
35+
package_name = package.name
36+
package_location = package.location
37+
package_files = []
38+
for item in (package.files or []):
39+
here = pathlib.Path('.').resolve()
40+
item_location_rel = (pathlib.Path(package_location) / item)
41+
item_location = item_location_rel.resolve()
42+
try:
43+
relative_item_location = item_location.relative_to(here)
44+
except ValueError:
45+
# Ideally we would use Pathlib.is_relative_to rather than
46+
# checking for a ValueError, but that is only available in
47+
# Python 3.9+.
48+
relative_item_location = item_location
49+
package_files.append(str(relative_item_location))
4550

4651
log.debug('installed package: %s (at %s)', package_name,
4752
package_location)

pip_check_reqs/find_missing_reqs.py

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31,21 +31,26 @@ def find_missing_reqs(options, requirements_filename):
3131
)
3232

3333
for package in search_packages_info(all_pkgs):
34-
package_name = package.name
35-
package_location = package.location
36-
package_files = []
37-
for item in (package.files or []):
38-
here = pathlib.Path('.').resolve()
39-
item_location_rel = (pathlib.Path(package_location) / item)
40-
item_location = item_location_rel.resolve()
41-
try:
42-
relative_item_location = item_location.relative_to(here)
43-
except ValueError:
44-
# Ideally we would use Pathlib.is_relative_to rather than
45-
# checking for a ValueError, but that is only available in
46-
# Python 3.9+.
47-
relative_item_location = item_location
48-
package_files.append(str(relative_item_location))
34+
if isinstance(package, dict): # pragma: no cover
35+
package_name = package['name']
36+
package_location = package['location']
37+
package_files = package.get('files', []) or []
38+
else: # pragma: no cover
39+
package_name = package.name
40+
package_location = package.location
41+
package_files = []
42+
for item in (package.files or []):
43+
here = pathlib.Path('.').resolve()
44+
item_location_rel = (pathlib.Path(package_location) / item)
45+
item_location = item_location_rel.resolve()
46+
try:
47+
relative_item_location = item_location.relative_to(here)
48+
except ValueError:
49+
# Ideally we would use Pathlib.is_relative_to rather than
50+
# checking for a ValueError, but that is only available in
51+
# Python 3.9+.
52+
relative_item_location = item_location
53+
package_files.append(str(relative_item_location))
4954

5055
log.debug('installed package: %s (at %s)', package_name,
5156
package_location)

tests/__init__.py

Whitespace-only changes.

tests/package_info_mock.py

Lines changed: 0 additions & 9 deletions
This file was deleted.

tests/test_find_extra_reqs.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212

1313
from pip_check_reqs import find_extra_reqs, common
1414

15-
from .package_info_mock import _PackageInfo
16-
1715

1816
@pytest.fixture
1917
def fake_opts():
@@ -70,10 +68,11 @@ class FakePathDistribution:
7068
pretend.call_recorder(lambda **kwargs: installed_distributions),
7169
)
7270
packages_info = [
73-
_PackageInfo(name='spam', location='site-spam',
74-
files=['spam/__init__.py', 'spam/shrub.py']),
75-
_PackageInfo(name='shrub', location='site-spam', files=['shrub.py']),
76-
_PackageInfo(name='pass', location='site-spam', files=['pass.py']),
71+
dict(name='spam',
72+
location='site-spam',
73+
files=['spam/__init__.py', 'spam/shrub.py']),
74+
dict(name='shrub', location='site-spam', files=['shrub.py']),
75+
dict(name='pass', location='site-spam', files=['pass.py']),
7776
]
7877

7978
monkeypatch.setattr(find_extra_reqs, 'search_packages_info',

tests/test_find_missing_reqs.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212

1313
from pip_check_reqs import find_missing_reqs, common
1414

15-
from .package_info_mock import _PackageInfo
16-
1715

1816
@pytest.fixture
1917
def fake_opts():
@@ -69,11 +67,11 @@ class FakePathDistribution:
6967
pretend.call_recorder(lambda **kwargs: installed_distributions),
7068
)
7169
packages_info = [
72-
_PackageInfo(name='spam',
73-
location='site-spam',
74-
files=['spam/__init__.py', 'spam/shrub.py']),
75-
_PackageInfo(name='shrub', location='site-spam', files=['shrub.py']),
76-
_PackageInfo(name='pass', location='site-spam', files=['pass.py']),
70+
dict(name='spam',
71+
location='site-spam',
72+
files=['spam/__init__.py', 'spam/shrub.py']),
73+
dict(name='shrub', location='site-spam', files=['shrub.py']),
74+
dict(name='pass', location='site-spam', files=['pass.py']),
7775
]
7876

7977
monkeypatch.setattr(find_missing_reqs, 'search_packages_info',

0 commit comments

Comments
 (0)