|
| 1 | +import os |
| 2 | +import os.path |
| 3 | +import shlex |
| 4 | +import subprocess |
| 5 | +import sys |
| 6 | + |
| 7 | +from . import _utils, _pythoninfo |
| 8 | + |
| 9 | + |
| 10 | +GET_PIP_URL = 'https://bootstrap.pypa.io/get-pip.py' |
| 11 | +# pip 6 is the first version supporting environment markers |
| 12 | +MIN_PIP = '6.0' |
| 13 | +OLD_PIP = '7.1.2' |
| 14 | +OLD_SETUPTOOLS = '18.5' |
| 15 | + |
| 16 | + |
| 17 | +def get_best_pip_version(python): |
| 18 | + """Return the pip to install for the given Python executable.""" |
| 19 | + if not python or isinstance(python, str): |
| 20 | + info = _pythoninfo.get_info(python) |
| 21 | + else: |
| 22 | + info = python |
| 23 | + # On Python: 3.5a0 <= version < 3.5.0 (final), install pip 7.1.2, |
| 24 | + # the last version working on Python 3.5a0: |
| 25 | + # https://sourceforge.net/p/pyparsing/bugs/100/ |
| 26 | + if 0x30500a0 <= info.sys.hexversion < 0x30500f0: |
| 27 | + return OLD_PIP |
| 28 | + else: |
| 29 | + return None |
| 30 | + |
| 31 | + |
| 32 | +def run_pip(cmd, *args, **kwargs): |
| 33 | + """Return the result of running pip with the given args.""" |
| 34 | + return _utils.run_python('-m', 'pip', cmd, *args, **kwargs) |
| 35 | + |
| 36 | + |
| 37 | +def is_pip_installed(python, *, env=None): |
| 38 | + """Return True if pip is installed on the given Python executable.""" |
| 39 | + ec, _, _ = run_pip('--version', env=env, capture=True, verbose=False) |
| 40 | + return ec == 0 |
| 41 | + |
| 42 | + |
| 43 | +def install_pip(python=sys.executable, *, |
| 44 | + info=None, |
| 45 | + downloaddir=None, |
| 46 | + env=None, |
| 47 | + **kwargs |
| 48 | + ): |
| 49 | + """Install pip on the given Python executable.""" |
| 50 | + if not python: |
| 51 | + python = getattr(info, 'executable', None) or sys.executable |
| 52 | + |
| 53 | + # python -m ensurepip |
| 54 | + res = _utils.run_python( |
| 55 | + '-m', 'ensurepip', '--verbose', |
| 56 | + python=python, |
| 57 | + **kwargs |
| 58 | + ) |
| 59 | + ec, _, _ = res |
| 60 | + if ec == 0 and is_pip_installed(python, env=env): |
| 61 | + return res |
| 62 | + |
| 63 | + ############################## |
| 64 | + # Fall back to get-pip.py. |
| 65 | + |
| 66 | + if not downloaddir: |
| 67 | + downloaddir = '.' |
| 68 | + os.makedirs(downloaddir, exist_ok=True) |
| 69 | + |
| 70 | + # download get-pip.py |
| 71 | + filename = os.path.join(downloaddir, 'get-pip.py') |
| 72 | + if not os.path.exists(filename): |
| 73 | + print("Download %s into %s" % (GET_PIP_URL, filename)) |
| 74 | + _utils.download(GET_PIP_URL, filename) |
| 75 | + |
| 76 | + # python get-pip.py |
| 77 | + argv = [python, '-u', filename] |
| 78 | + version = get_best_pip_version(info or python) |
| 79 | + if version: |
| 80 | + argv.append(version) |
| 81 | + res = _utils.run_cmd(argv, env=env) |
| 82 | + ec, _, _ = res |
| 83 | + if ec != 0: |
| 84 | + # get-pip.py was maybe not properly downloaded: remove it to |
| 85 | + # download it again next time |
| 86 | + os.unlink(filename) |
| 87 | + return res |
| 88 | + |
| 89 | + |
| 90 | +def upgrade_pip(python=sys.executable, *, |
| 91 | + info=None, |
| 92 | + installer=False, |
| 93 | + **kwargs, |
| 94 | + ): |
| 95 | + """Upgrade pip on the given Python to the latest version.""" |
| 96 | + if not python: |
| 97 | + python = getattr(info, 'executable', None) or sys.executable |
| 98 | + |
| 99 | + version = get_best_pip_version(info or python) |
| 100 | + if version: |
| 101 | + reqs = [f'pip=={version}'] |
| 102 | + if installer: |
| 103 | + reqs.append(f'setuptools=={OLD_SETUPTOOLS}') |
| 104 | + else: |
| 105 | + # pip 6 is the first version supporting environment markers |
| 106 | + reqs = [f'pip>={MIN_PIP}'] |
| 107 | + res = install_requirements(*reqs, python=python, upgrade=True, **kwargs) |
| 108 | + ec, _, _ = res |
| 109 | + if ec != 0: |
| 110 | + return res |
| 111 | + |
| 112 | + if installer: |
| 113 | + # Upgrade installer dependencies (setuptools, ...) |
| 114 | + reqs = [ |
| 115 | + f'setuptools>={OLD_SETUPTOOLS}', |
| 116 | + # install wheel so pip can cache binary wheel packages locally, |
| 117 | + # and install prebuilt wheel packages from PyPI. |
| 118 | + 'wheel', |
| 119 | + ] |
| 120 | + res = install_requirements(*reqs, python=python, upgrade=True, **kwargs) |
| 121 | + return res |
| 122 | + |
| 123 | + |
| 124 | +def install_requirements(reqs, *extra, |
| 125 | + upgrade=True, |
| 126 | + **kwargs |
| 127 | + ): |
| 128 | + """Install the given packages from PyPI.""" |
| 129 | + args = [] |
| 130 | + if upgrade: |
| 131 | + args.append('-U') # --upgrade |
| 132 | + for reqs in [reqs, *extra]: |
| 133 | + if os.path.exists(reqs): |
| 134 | + args.append('-r') # --requirement |
| 135 | + args.append(reqs) |
| 136 | + return run_pip('install', *args, **kwargs) |
| 137 | + |
| 138 | + |
| 139 | +def install_editable(projectroot, **kwargs): |
| 140 | + """Install the given project as an "editable" install.""" |
| 141 | + return run_pip('install', '-e', projectroot, **kwargs) |
0 commit comments