|
38 | 38 | import pkgutil
|
39 | 39 | import shutil
|
40 | 40 | import tempfile
|
| 41 | +import argparse |
| 42 | +import importlib |
41 | 43 | from base64 import b85decode
|
42 | 44 |
|
43 | 45 |
|
| 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 |
| 54 | + |
| 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 | + |
| 65 | + |
44 | 66 | def determine_pip_install_arguments():
|
45 |
| - implicit_pip = True |
46 |
| - implicit_setuptools = True |
47 |
| - implicit_wheel = True |
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 |
| - |
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 |
60 |
| - |
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 |
75 |
| - |
76 |
| - # Add any implicit installations to the end of our args |
77 |
| - if implicit_pip: |
78 |
| - args += ["pip{pip_version}"] |
79 |
| - if implicit_setuptools: |
80 |
| - args += ["setuptools{setuptools_version}"] |
81 |
| - if implicit_wheel: |
82 |
| - args += ["wheel{wheel_version}"] |
| 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() |
| 71 | + |
| 72 | + args.append("pip{pip_version}") |
| 73 | + |
| 74 | + if include_setuptools(pre): |
| 75 | + args.append("setuptools{setuptools_version}") |
| 76 | + |
| 77 | + if include_wheel(pre): |
| 78 | + args.append("wheel{wheel_version}") |
83 | 79 |
|
84 | 80 | return ["install", "--upgrade", "--force-reinstall"] + args
|
85 | 81 |
|
|
0 commit comments