Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
keewenaw
GitHub Repository: keewenaw/ethereum-wallet-cracker
Path: blob/main/test/lib/python3.9/site-packages/pip/_internal/utils/entrypoints.py
4804 views
1
import itertools
2
import os
3
import shutil
4
import sys
5
from typing import List, Optional
6
7
from pip._internal.cli.main import main
8
from pip._internal.utils.compat import WINDOWS
9
10
_EXECUTABLE_NAMES = [
11
"pip",
12
f"pip{sys.version_info.major}",
13
f"pip{sys.version_info.major}.{sys.version_info.minor}",
14
]
15
if WINDOWS:
16
_allowed_extensions = {"", ".exe"}
17
_EXECUTABLE_NAMES = [
18
"".join(parts)
19
for parts in itertools.product(_EXECUTABLE_NAMES, _allowed_extensions)
20
]
21
22
23
def _wrapper(args: Optional[List[str]] = None) -> int:
24
"""Central wrapper for all old entrypoints.
25
26
Historically pip has had several entrypoints defined. Because of issues
27
arising from PATH, sys.path, multiple Pythons, their interactions, and most
28
of them having a pip installed, users suffer every time an entrypoint gets
29
moved.
30
31
To alleviate this pain, and provide a mechanism for warning users and
32
directing them to an appropriate place for help, we now define all of
33
our old entrypoints as wrappers for the current one.
34
"""
35
sys.stderr.write(
36
"WARNING: pip is being invoked by an old script wrapper. This will "
37
"fail in a future version of pip.\n"
38
"Please see https://github.com/pypa/pip/issues/5599 for advice on "
39
"fixing the underlying issue.\n"
40
"To avoid this problem you can invoke Python with '-m pip' instead of "
41
"running pip directly.\n"
42
)
43
return main(args)
44
45
46
def get_best_invocation_for_this_pip() -> str:
47
"""Try to figure out the best way to invoke pip in the current environment."""
48
binary_directory = "Scripts" if WINDOWS else "bin"
49
binary_prefix = os.path.join(sys.prefix, binary_directory)
50
51
# Try to use pip[X[.Y]] names, if those executables for this environment are
52
# the first on PATH with that name.
53
path_parts = os.path.normcase(os.environ.get("PATH", "")).split(os.pathsep)
54
exe_are_in_PATH = os.path.normcase(binary_prefix) in path_parts
55
if exe_are_in_PATH:
56
for exe_name in _EXECUTABLE_NAMES:
57
found_executable = shutil.which(exe_name)
58
if found_executable and os.path.samefile(
59
found_executable,
60
os.path.join(binary_prefix, exe_name),
61
):
62
return exe_name
63
64
# Use the `-m` invocation, if there's no "nice" invocation.
65
return f"{get_best_invocation_for_this_python()} -m pip"
66
67
68
def get_best_invocation_for_this_python() -> str:
69
"""Try to figure out the best way to invoke the current Python."""
70
exe = sys.executable
71
exe_name = os.path.basename(exe)
72
73
# Try to use the basename, if it's the first executable.
74
found_executable = shutil.which(exe_name)
75
if found_executable and os.path.samefile(found_executable, exe):
76
return exe_name
77
78
# Use the full executable name, because we couldn't find something simpler.
79
return exe
80
81