Skip to content

Commit 8ca7f95

Browse files
Merge pull request #65 from r1chardj0n3s/support-latest-pip-gte-21.2.1
Fix commands on pip 21.2.1
2 parents 498b9e0 + cb62b3b commit 8ca7f95

File tree

2 files changed

+57
-12
lines changed

2 files changed

+57
-12
lines changed

pip_check_reqs/find_extra_reqs.py

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import collections
22
import logging
3+
import pathlib
34
import optparse
45
import os
56
import sys
@@ -21,22 +22,44 @@ def find_extra_reqs(options, requirements_filename):
2122
installed_files = {}
2223
all_pkgs = (pkg.project_name for pkg in get_installed_distributions())
2324
for package in search_packages_info(all_pkgs):
24-
log.debug('installed package: %s (at %s)', package['name'],
25-
package['location'])
26-
for package_file in package.get('files', []) or []:
25+
if isinstance(package, dict): # pragma: no cover
26+
package_name = package['name']
27+
package_location = package['location']
28+
package_files = package.get('files', []) or []
29+
else: # pragma: no cover
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))
45+
46+
log.debug('installed package: %s (at %s)', package_name,
47+
package_location)
48+
for package_file in package_files:
2749
path = os.path.realpath(
28-
os.path.join(package['location'], package_file),
50+
os.path.join(package_location, package_file),
2951
)
30-
installed_files[path] = package['name']
52+
installed_files[path] = package_name
3153
package_path = common.is_package_file(path)
3254
if package_path:
3355
# we've seen a package file so add the bare package directory
3456
# to the installed list as well as we might want to look up
3557
# a package by its directory path later
36-
installed_files[package_path] = package['name']
58+
installed_files[package_path] = package_name
3759

3860
# 3. match imported modules against those packages
3961
used = collections.defaultdict(list)
62+
4063
for modname, info in used_modules.items():
4164
# probably standard library if it's not in the files list
4265
if info.filename in installed_files:

pip_check_reqs/find_missing_reqs.py

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import logging
33
import optparse
44
import os
5+
import pathlib
56
import sys
67

78
from packaging.utils import canonicalize_name
@@ -28,19 +29,40 @@ def find_missing_reqs(options, requirements_filename):
2829
installed_files = {}
2930
all_pkgs = (pkg.project_name for pkg in get_installed_distributions())
3031
for package in search_packages_info(all_pkgs):
31-
log.debug('installed package: %s (at %s)', package['name'],
32-
package['location'])
33-
for package_file in package.get('files', []) or []:
32+
if isinstance(package, dict): # pragma: no cover
33+
package_name = package['name']
34+
package_location = package['location']
35+
package_files = package.get('files', []) or []
36+
else: # pragma: no cover
37+
package_name = package.name
38+
package_location = package.location
39+
package_files = []
40+
for item in (package.files or []):
41+
here = pathlib.Path('.').resolve()
42+
item_location_rel = (pathlib.Path(package_location) / item)
43+
item_location = item_location_rel.resolve()
44+
try:
45+
relative_item_location = item_location.relative_to(here)
46+
except ValueError:
47+
# Ideally we would use Pathlib.is_relative_to rather than
48+
# checking for a ValueError, but that is only available in
49+
# Python 3.9+.
50+
relative_item_location = item_location
51+
package_files.append(str(relative_item_location))
52+
53+
log.debug('installed package: %s (at %s)', package_name,
54+
package_location)
55+
for package_file in package_files:
3456
path = os.path.realpath(
35-
os.path.join(package['location'], package_file),
57+
os.path.join(package_location, package_file),
3658
)
37-
installed_files[path] = package['name']
59+
installed_files[path] = package_name
3860
package_path = common.is_package_file(path)
3961
if package_path:
4062
# we've seen a package file so add the bare package directory
4163
# to the installed list as well as we might want to look up
4264
# a package by its directory path later
43-
installed_files[package_path] = package['name']
65+
installed_files[package_path] = package_name
4466

4567
# 3. match imported modules against those packages
4668
used = collections.defaultdict(list)

0 commit comments

Comments
 (0)