|
| 1 | +from glob import glob |
| 2 | +import distutils.sysconfig |
| 3 | +import os |
| 4 | +import shutil |
| 5 | +import subprocess |
| 6 | +import sys |
| 7 | +import textwrap |
| 8 | + |
| 9 | + |
| 10 | +VIRTUAL_ENV = os.environ['VIRTUAL_ENV'] |
| 11 | +TOX_PIP_DIR = os.path.join(VIRTUAL_ENV, 'pip') |
| 12 | +SITE_PACKAGES = distutils.sysconfig.get_python_lib() |
| 13 | + |
| 14 | + |
| 15 | +def pip(args): |
| 16 | + # First things first, safeguard the environment |
| 17 | + # original pip so it can be used for all calls. |
| 18 | + if not os.path.exists(TOX_PIP_DIR): |
| 19 | + os.mkdir(TOX_PIP_DIR) |
| 20 | + # Remove executable/launchers. |
| 21 | + for entry in glob(os.path.join(VIRTUAL_ENV, 'bin', 'pip*')): |
| 22 | + os.unlink(entry) |
| 23 | + # Relocate package and distribution info. |
| 24 | + for src in ( |
| 25 | + os.path.join(SITE_PACKAGES, 'pip'), |
| 26 | + glob(os.path.join(SITE_PACKAGES, 'pip-*.dist-info'))[0], |
| 27 | + ): |
| 28 | + shutil.move(src, TOX_PIP_DIR) |
| 29 | + # Create a very simple launcher that |
| 30 | + # can be used for Linux and Windows. |
| 31 | + with open(os.path.join(TOX_PIP_DIR, 'pip.py'), 'w') as fp: |
| 32 | + fp.write(textwrap.dedent( |
| 33 | + ''' |
| 34 | + import sys |
| 35 | + from pip._vendor import pkg_resources |
| 36 | + sys.exit(pkg_resources.load_entry_point( |
| 37 | + 'pip', 'console_scripts', 'pip' |
| 38 | + )()) |
| 39 | + ''' |
| 40 | + ).lstrip()) |
| 41 | + # And use a temporary copy of that version |
| 42 | + # so it can uninstall itself if needed. |
| 43 | + temp_pip = TOX_PIP_DIR + '.tmp' |
| 44 | + try: |
| 45 | + shutil.copytree(TOX_PIP_DIR, temp_pip) |
| 46 | + cmd = [sys.executable, os.path.join(temp_pip, 'pip.py')] |
| 47 | + cmd.extend(args) |
| 48 | + subprocess.check_call(cmd) |
| 49 | + finally: |
| 50 | + shutil.rmtree(temp_pip) |
| 51 | + |
| 52 | + |
| 53 | +if __name__ == '__main__': |
| 54 | + pip(sys.argv[1:]) |
0 commit comments