Skip to content

Commit

Permalink
remove code duplication to get python version from executable
Browse files Browse the repository at this point in the history
  • Loading branch information
finswimmer committed Jun 14, 2021
1 parent efeb695 commit 7d90e7f
Showing 1 changed file with 11 additions and 56 deletions.
67 changes: 11 additions & 56 deletions poetry/utils/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@ def __init__(self, poetry: Poetry) -> None:
self._poetry = poetry

@lru_cache()
def _python_version(self, executable: str) -> str:
def _python_version(self, executable: str) -> Version:
try:
python_version = decode(
subprocess.check_output(
Expand All @@ -446,7 +446,7 @@ def _python_version(self, executable: str) -> str:
except CalledProcessError as e:
raise EnvCommandError(e)

return python_version
return Version.parse(python_version.strip())

def activate(self, python: str, io: IO) -> "Env":
venv_path = self._poetry.config.get("virtualenvs.path")
Expand All @@ -468,23 +468,7 @@ def activate(self, python: str, io: IO) -> "Env":
# Executable in PATH or full executable path
pass

try:
python_version = decode(
subprocess.check_output(
list_to_shell_command(
[
python,
"-c",
"\"import sys; print('.'.join([str(s) for s in sys.version_info[:3]]))\"",
]
),
shell=True,
)
)
except CalledProcessError as e:
raise EnvCommandError(e)

python_version = Version.parse(python_version.strip())
python_version = self._python_version(python)
minor = f"{python_version.major}.{python_version.minor}"
patch = python_version.text

Expand Down Expand Up @@ -577,7 +561,6 @@ def get(

if executable:
python_version = self._python_version(executable)
python_version = Version.parse(python_version.strip())
python_minor = f"{python_version.major}.{python_version.minor}"
else:
python_minor = ".".join([str(v) for v in sys.version_info[:2]])
Expand Down Expand Up @@ -721,23 +704,7 @@ def remove(self, python: str) -> "Env":
# Executable in PATH or full executable path
pass

try:
python_version = decode(
subprocess.check_output(
list_to_shell_command(
[
python,
"-c",
"\"import sys; print('.'.join([str(s) for s in sys.version_info[:3]]))\"",
]
),
shell=True,
)
)
except CalledProcessError as e:
raise EnvCommandError(e)

python_version = Version.parse(python_version.strip())
python_version = self._python_version(python)
minor = f"{python_version.major}.{python_version.minor}"

name = f"{base_env_name}-py{minor}"
Expand Down Expand Up @@ -794,14 +761,14 @@ def create_venv(
if not name:
name = self._poetry.package.name

python_patch = ".".join([str(v) for v in sys.version_info[:3]])
python_patch = Version.parse(".".join([str(v) for v in sys.version_info[:3]]))
python_minor = ".".join([str(v) for v in sys.version_info[:2]])
if executable:
python_patch = self._python_version(executable)
python_minor = ".".join(python_patch.split(".")[:2])
python_minor = f"{python_patch.major}.{python_patch.minor}"

supported_python = self._poetry.package.python_constraint
if not supported_python.allows(Version.parse(python_patch)):
if not supported_python.allows(python_patch):
# The currently activated or chosen Python version
# is not compatible with the Python constraint specified
# for the project.
Expand All @@ -810,7 +777,7 @@ def create_venv(
# Otherwise, we try to find a compatible Python version.
if executable:
raise NoCompatiblePythonVersionFound(
self._poetry.package.python_versions, python_patch
self._poetry.package.python_versions, str(python_patch)
)

io.write_line(
Expand Down Expand Up @@ -843,29 +810,17 @@ def create_venv(
io.write_line(f"<debug>Trying {python}</debug>")

try:
python_patch = decode(
subprocess.check_output(
list_to_shell_command(
[
python,
"-c",
"\"import sys; print('.'.join([str(s) for s in sys.version_info[:3]]))\"",
]
),
stderr=subprocess.STDOUT,
shell=True,
).strip()
)
python_patch = self._python_version(python)
except CalledProcessError:
continue

if not python_patch:
continue

if supported_python.allows(Version.parse(python_patch)):
if supported_python.allows(python_patch):
io.write_line(f"Using <c1>{python}</c1> ({python_patch})")
executable = python
python_minor = ".".join(python_patch.split(".")[:2])
python_minor = f"{python_patch.major}.{python_patch.minor}"
break

if not executable:
Expand Down

0 comments on commit 7d90e7f

Please sign in to comment.