1
1
import collections
2
2
import logging
3
+ import pathlib
3
4
import optparse
4
5
import os
5
6
import sys
@@ -21,22 +22,44 @@ def find_extra_reqs(options, requirements_filename):
21
22
installed_files = {}
22
23
all_pkgs = (pkg .project_name for pkg in get_installed_distributions ())
23
24
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 :
27
49
path = os .path .realpath (
28
- os .path .join (package [ 'location' ] , package_file ),
50
+ os .path .join (package_location , package_file ),
29
51
)
30
- installed_files [path ] = package [ 'name' ]
52
+ installed_files [path ] = package_name
31
53
package_path = common .is_package_file (path )
32
54
if package_path :
33
55
# we've seen a package file so add the bare package directory
34
56
# to the installed list as well as we might want to look up
35
57
# a package by its directory path later
36
- installed_files [package_path ] = package [ 'name' ]
58
+ installed_files [package_path ] = package_name
37
59
38
60
# 3. match imported modules against those packages
39
61
used = collections .defaultdict (list )
62
+
40
63
for modname , info in used_modules .items ():
41
64
# probably standard library if it's not in the files list
42
65
if info .filename in installed_files :
0 commit comments