25
25
_WHEEL_PKG_DIR = sysconfig .get_config_var ('WHEEL_PKG_DIR' )
26
26
27
27
28
- def _get_replacement_pip_package (path : str | None ) -> _Package :
29
- if path is None :
30
- raise LookupError (
31
- 'The compile-time `WHEEL_PKG_DIR` is unset so there is '
32
-
8000
'no place for looking up the wheels.' ,
33
- )
34
-
28
+ def _find_wheel_pkg_dir_pip ():
29
+ if _WHEEL_PKG_DIR is None :
30
+ return None
35
31
try :
36
- filenames = os .listdir (path )
32
+ filenames = os .listdir (_WHEEL_PKG_DIR )
37
33
except OSError :
38
34
# Ignore: path doesn't exist or permission error
39
- filenames = ()
35
+ return None
40
36
# Make the code deterministic if a directory contains multiple wheel files
41
37
# of the same package, but don't attempt to implement correct version
42
38
# comparison since this case should not happen.
43
- filenames = sorted (filenames )
44
- pip_pkg = None
39
+ filenames = sorted (filenames , reverse = True )
45
40
for filename in filenames :
46
41
# filename is like 'pip-21.2.4-py3-none-any.whl'
47
- if not filename .endswith (".whl" ):
48
- continue
49
- if not filename .startswith ('pip-' ):
42
+ if not filename .startswith ("pip-" ) or not filename .endswith (".whl" ):
50
43
continue
51
44
52
45
# Extract '21.2.4' from 'pip-21.2.4-py3-none-any.whl'
53
- discovered_pip_pkg_version = filename .removeprefix (
54
- 'pip-' ,
55
- ).partition ('-' )[0 ]
56
- wheel_path = os .path .join (path , filename )
57
- pip_pkg = _Package (discovered_pip_pkg_version , None , wheel_path )
46
+ version = filename .removeprefix ("pip-" ).partition ("-" )[0 ]
47
+ wheel_path = os .path .join (_WHEEL_PKG_DIR , filename )
48
+ return _Package (version , None , wheel_path )
58
49
59
- if pip_pkg is None :
60
- raise LookupError (
61
- '`WHEEL_PKG_DIR` does not contain any wheel files for `pip`.' ,
62
- )
63
-
64
- return pip_pkg
50
+ return None
65
51
66
52
67
53
@cache
@@ -71,7 +57,7 @@ def _get_usable_pip_package() -> _Package:
71
57
72
58
with suppress (LookupError ):
73
59
# only use the wheel package directory if pip wheel is found there
74
- pip_pkg = _get_replacement_pip_package (_WHEEL_PKG_DIR )
60
+ pip_pkg = _find_wheel_pkg_dir_pip (_WHEEL_PKG_DIR )
75
61
76
62
return pip_pkg
77
63
0 commit comments