Skip to content

Commit

Permalink
Minimal Pythonfinder intergration
Browse files Browse the repository at this point in the history
  • Loading branch information
uranusjr committed Jul 14, 2018
1 parent c99a4fb commit 4d54c61
Showing 1 changed file with 10 additions and 102 deletions.
112 changes: 10 additions & 102 deletions pipenv/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,76 +313,15 @@ def ensure_pipfile(validate=True, skip_requirements=False, system=False):
project.write_toml(p)


def find_python_from_py(python):
"""Find a Python executable from on Windows.
Ask py.exe for its opinion.
"""
py = system_which("py")
if not py:
return None

version_args = ["-{0}".format(python[0])]
if len(python) >= 2:
version_args.append("-{0}.{1}".format(python[0], python[2]))
import subprocess

for ver_arg in reversed(version_args):
try:
python_exe = subprocess.check_output(
[py, ver_arg, "-c", "import sys; print(sys.executable)"]
)
except subprocess.CalledProcessError:
continue

if not isinstance(python_exe, str):
python_exe = python_exe.decode(sys.getdefaultencoding())
python_exe = python_exe.strip()
version = python_version(python_exe)
if (version or "").startswith(python):
return python_exe


def find_python_in_path(python):
"""Find a Python executable from a version number.
This uses the PATH environment variable to locate an appropriate Python.
"""
possibilities = ["python", "python{0}".format(python[0])]
if len(python) >= 2:
possibilities.extend(
[
"python{0}{1}".format(python[0], python[2]),
"python{0}.{1}".format(python[0], python[2]),
"python{0}.{1}m".format(python[0], python[2]),
]
)
# Reverse the list, so we find specific ones first.
possibilities = reversed(possibilities)
for possibility in possibilities:
# Windows compatibility.
if os.name == "nt":
possibility = "{0}.exe".format(possibility)
pythons = system_which(possibility, mult=True)
for p in pythons:
version = python_version(p)
if (version or "").startswith(python):
return p


def find_a_system_python(python):
"""Finds a system python, given a version (e.g. 2 / 2.7 / 3.6.2), or a full path."""
if python.startswith("py"):
return system_which(python)

elif os.path.isabs(python):
return python

python_from_py = find_python_from_py(python)
if python_from_py:
return python_from_py

return find_python_in_path(python)
if not python:
return None
from .vendor import pythonfinder
finder = pythonfinder.Finder()
python_entry = finder.find_python_version(python)
if python_entry:
return str(python_entry.path)
return None


def ensure_python(three=None, python=None):
Expand All @@ -405,35 +344,7 @@ def abort():
)
sys.exit(1)

def activate_pyenv():
from notpip._vendor.packaging.version import parse as parse_version

"""Adds all pyenv installations to the PATH."""
if PYENV_INSTALLED:
if PYENV_ROOT:
pyenv_paths = {}
for found in glob("{0}{1}versions{1}*".format(PYENV_ROOT, os.sep)):
pyenv_paths[os.path.split(found)[1]] = "{0}{1}bin".format(
found, os.sep
)
for version_str, pyenv_path in pyenv_paths.items():
version = parse_version(version_str)
if version.is_prerelease and pyenv_paths.get(version.base_version):
continue

add_to_path(pyenv_path)
else:
click.echo(
"{0}: PYENV_ROOT is not set. New python paths will "
"probably not be exported properly after installation."
"".format(crayons.red("Warning", bold=True)),
err=True,
)

global USING_DEFAULT_PYTHON
# Add pyenv paths to PATH.
activate_pyenv()
path_to_python = None
USING_DEFAULT_PYTHON = three is None and not python
# Find out which python is desired.
if not python:
Expand All @@ -442,8 +353,7 @@ def activate_pyenv():
python = project.required_python_version
if not python:
python = PIPENV_DEFAULT_PYTHON_VERSION
if python:
path_to_python = find_a_system_python(python)
path_to_python = find_a_system_python(python)
if not path_to_python and python is not None:
# We need to install Python.
click.echo(
Expand Down Expand Up @@ -516,10 +426,8 @@ def activate_pyenv():
click.echo(crayons.blue(c.err), err=True)
# Print the results, in a beautiful blue…
click.echo(crayons.blue(c.out), err=True)
# Add new paths to PATH.
activate_pyenv()
# Find the newly installed Python, hopefully.
path_to_python = find_a_system_python(version)
path_to_python = find_a_system_python(python)
try:
assert python_version(path_to_python) == version
except AssertionError:
Expand Down

0 comments on commit 4d54c61

Please sign in to comment.