|
38 | 38 | import pkgutil
|
39 | 39 | import shutil
|
40 | 40 | import tempfile
|
41 |
| -import argparse |
42 |
| -import importlib |
43 | 41 | from base64 import b85decode
|
44 | 42 |
|
45 | 43 |
|
46 |
| -def include_setuptools(args): |
47 |
| - """ |
48 |
| - Install setuptools only if absent and not excluded. |
49 |
| - """ |
50 |
| - cli = not args.no_setuptools |
51 |
| - env = not os.environ.get("PIP_NO_SETUPTOOLS") |
52 |
| - absent = not importlib.util.find_spec("setuptools") |
53 |
| - return cli and env and absent |
| 44 | +def determine_pip_install_arguments(): |
| 45 | + implicit_pip = True |
| 46 | + implicit_setuptools = True |
| 47 | + implicit_wheel = True |
54 | 48 |
|
| 49 | + # Check if the user has requested us not to install setuptools |
| 50 | + if "--no-setuptools" in sys.argv or os.environ.get("PIP_NO_SETUPTOOLS"): |
| 51 | + args = [x for x in sys.argv[1:] if x != "--no-setuptools"] |
| 52 | + implicit_setuptools = False |
| 53 | + else: |
| 54 | + args = sys.argv[1:] |
55 | 55 |
|
56 |
| -def include_wheel(args): |
57 |
| - """ |
58 |
| - Install wheel only if absent and not excluded. |
59 |
| - """ |
60 |
| - cli = not args.no_wheel |
61 |
| - env = not os.environ.get("PIP_NO_WHEEL") |
62 |
| - absent = not importlib.util.find_spec("wheel") |
63 |
| - return cli and env and absent |
64 |
| - |
| 56 | + # Check if the user has requested us not to install wheel |
| 57 | + if "--no-wheel" in args or os.environ.get("PIP_NO_WHEEL"): |
| 58 | + args = [x for x in args if x != "--no-wheel"] |
| 59 | + implicit_wheel = False |
65 | 60 |
|
66 |
| -def determine_pip_install_arguments(): |
67 |
| - pre_parser = argparse.ArgumentParser() |
68 |
| - pre_parser.add_argument("--no-setuptools", action="store_true") |
69 |
| - pre_parser.add_argument("--no-wheel", action="store_true") |
70 |
| - pre, args = pre_parser.parse_known_args() |
| 61 | + # We only want to implicitly install setuptools and wheel if they don't |
| 62 | + # already exist on the target platform. |
| 63 | + if implicit_setuptools: |
| 64 | + try: |
| 65 | + import setuptools # noqa |
| 66 | + implicit_setuptools = False |
| 67 | + except ImportError: |
| 68 | + pass |
| 69 | + if implicit_wheel: |
| 70 | + try: |
| 71 | + import wheel # noqa |
| 72 | + implicit_wheel = False |
| 73 | + except ImportError: |
| 74 | + pass |
71 | 75 |
|
72 |
| - args += ["pip"] |
73 |
| - args += ["setuptools"] * include_setuptools(pre) |
74 |
| - args += ["wheel"] * include_wheel(pre) |
| 76 | + # Add any implicit installations to the end of our args |
| 77 | + if implicit_pip: |
| 78 | + args += ["pip"] |
| 79 | + if implicit_setuptools: |
| 80 | + args += ["setuptools"] |
| 81 | + if implicit_wheel: |
| 82 | + args += ["wheel"] |
75 | 83 |
|
76 | 84 | return ["install", "--upgrade", "--force-reinstall"] + args
|
77 | 85 |
|
|
0 commit comments