|
1 | 1 | from __future__ import unicode_literals |
2 | 2 |
|
| 3 | +import os.path |
| 4 | + |
3 | 5 | from pre_commit.languages import python |
| 6 | +from pre_commit.util import CalledProcessError |
4 | 7 | from pre_commit.util import cmd_output |
5 | 8 |
|
6 | 9 |
|
7 | 10 | ENVIRONMENT_DIR = 'py_venv' |
8 | 11 |
|
9 | 12 |
|
| 13 | +def orig_py_exe(exe): # pragma: no cover (platform specific) |
| 14 | + """A -mvenv virtualenv made from a -mvirtualenv virtualenv installs |
| 15 | + packages to the incorrect location. Attempt to find the _original_ exe |
| 16 | + and invoke `-mvenv` from there. |
| 17 | +
|
| 18 | + See: |
| 19 | + - https://github.com/pre-commit/pre-commit/issues/755 |
| 20 | + - https://github.com/pypa/virtualenv/issues/1095 |
| 21 | + - https://bugs.python.org/issue30811 |
| 22 | + """ |
| 23 | + try: |
| 24 | + prefix_script = 'import sys; print(sys.real_prefix)' |
| 25 | + _, prefix, _ = cmd_output(exe, '-c', prefix_script) |
| 26 | + prefix = prefix.strip() |
| 27 | + except CalledProcessError: |
| 28 | + # not created from -mvirtualenv |
| 29 | + return exe |
| 30 | + |
| 31 | + if os.name == 'nt': |
| 32 | + expected = os.path.join(prefix, 'python.exe') |
| 33 | + else: |
| 34 | + expected = os.path.join(prefix, 'bin', os.path.basename(exe)) |
| 35 | + |
| 36 | + if os.path.exists(expected): |
| 37 | + return expected |
| 38 | + else: |
| 39 | + return exe |
| 40 | + |
| 41 | + |
10 | 42 | def make_venv(envdir, python): |
11 | | - cmd_output(python, '-mvenv', envdir, cwd='/') |
| 43 | + cmd_output(orig_py_exe(python), '-mvenv', envdir, cwd='/') |
12 | 44 |
|
13 | 45 |
|
14 | 46 | get_default_version = python.get_default_version |
|
0 commit comments