Skip to content

Commit

Permalink
Improve environment management (#1477)
Browse files Browse the repository at this point in the history
* Refactor the environment management code

* Improve executable selection when current Python is incompatible
  • Loading branch information
sdispater authored Oct 18, 2019
1 parent 92c460e commit 7f5acc3
Show file tree
Hide file tree
Showing 20 changed files with 370 additions and 164 deletions.
2 changes: 1 addition & 1 deletion poetry/console/commands/debug/resolve.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def handle(self):

return 0

env = EnvManager(self.poetry.config).get(self.poetry.file.parent)
env = EnvManager(self.poetry).get()
current_python_version = parse_constraint(
".".join(str(v) for v in env.version_info)
)
Expand Down
3 changes: 1 addition & 2 deletions poetry/console/commands/env/info.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ class EnvInfoCommand(Command):
def handle(self):
from poetry.utils.env import EnvManager

poetry = self.poetry
env = EnvManager(poetry.config).get(cwd=poetry.file.parent)
env = EnvManager(self.poetry).get()

if self.option("path"):
if not env.is_venv():
Expand Down
7 changes: 3 additions & 4 deletions poetry/console/commands/env/list.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,10 @@ class EnvListCommand(Command):
def handle(self):
from poetry.utils.env import EnvManager

poetry = self.poetry
manager = EnvManager(poetry.config)
current_env = manager.get(self.poetry.file.parent)
manager = EnvManager(self.poetry)
current_env = manager.get()

for venv in manager.list(self.poetry.file.parent):
for venv in manager.list():
name = venv.path.name
if self.option("full-path"):
name = str(venv.path)
Expand Down
5 changes: 2 additions & 3 deletions poetry/console/commands/env/remove.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ class EnvRemoveCommand(Command):
def handle(self):
from poetry.utils.env import EnvManager

poetry = self.poetry
manager = EnvManager(poetry.config)
venv = manager.remove(self.argument("python"), poetry.file.parent)
manager = EnvManager(self.poetry)
venv = manager.remove(self.argument("python"))

self.line("Deleted virtualenv: <comment>{}</comment>".format(venv.path))
7 changes: 3 additions & 4 deletions poetry/console/commands/env/use.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,13 @@ class EnvUseCommand(Command):
def handle(self):
from poetry.utils.env import EnvManager

poetry = self.poetry
manager = EnvManager(poetry.config)
manager = EnvManager(self.poetry)

if self.argument("python") == "system":
manager.deactivate(poetry.file.parent, self._io)
manager.deactivate(self._io)

return

env = manager.activate(self.argument("python"), poetry.file.parent, self._io)
env = manager.activate(self.argument("python"), self._io)

self.line("Using virtualenv: <comment>{}</>".format(env.path))
6 changes: 3 additions & 3 deletions poetry/console/commands/init.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import os
import re
import sys

from typing import Dict
from typing import List
Expand All @@ -15,7 +16,6 @@
from poetry.utils._compat import Path
from poetry.utils._compat import OrderedDict
from poetry.utils._compat import urlparse
from poetry.utils.helpers import temporary_directory

from .command import Command
from .env_command import EnvCommand
Expand Down Expand Up @@ -63,7 +63,7 @@ def __init__(self):
def handle(self):
from poetry.layouts import layout
from poetry.utils._compat import Path
from poetry.utils.env import EnvManager
from poetry.utils.env import SystemEnv
from poetry.vcs.git import GitConfig

if (Path.cwd() / "pyproject.toml").exists():
Expand Down Expand Up @@ -126,7 +126,7 @@ def handle(self):
question.set_validator(self._validate_license)
license = self.ask(question)

current_env = EnvManager().get(Path.cwd())
current_env = SystemEnv(Path(sys.executable))
default_python = "^{}".format(
".".join(str(v) for v in current_env.version_info[:2])
)
Expand Down
26 changes: 3 additions & 23 deletions poetry/console/config/application_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,7 @@ def configure(self):
self.add_event_listener(ConsoleEvents.PRE_HANDLE.value, self.set_env)

def register_command_loggers(
self,
event, # type: PreHandleEvent
event_name, # type: str
_,
self, event, event_name, _ # type: PreHandleEvent # type: str
): # type: (...) -> None
command = event.command.config.handler
if not isinstance(command, Command):
Expand Down Expand Up @@ -70,25 +67,8 @@ def set_env(self, event, event_name, _): # type: (PreHandleEvent, str, _) -> No
io = event.io
poetry = command.poetry

env_manager = EnvManager(poetry.config)

# Checking compatibility of the current environment with
# the python dependency specified in pyproject.toml
current_env = env_manager.get(poetry.file.parent)
supported_python = poetry.package.python_constraint
current_python = parse_constraint(
".".join(str(v) for v in current_env.version_info[:3])
)

if not supported_python.allows(current_python):
raise RuntimeError(
"The current Python version ({}) is not supported by the project ({})\n"
"Please activate a compatible Python version.".format(
current_python, poetry.package.python_versions
)
)

env = env_manager.create_venv(poetry.file.parent, io, poetry.package.name)
env_manager = EnvManager(poetry)
env = env_manager.create_venv(io)

if env.is_venv() and io.is_verbose():
io.write_line("Using virtualenv: <comment>{}</>".format(env.path))
Expand Down
2 changes: 1 addition & 1 deletion poetry/packages/package.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

class Package(object):

AVAILABLE_PYTHONS = {"2", "2.7", "3", "3.4", "3.5", "3.6", "3.7"}
AVAILABLE_PYTHONS = {"2", "2.7", "3", "3.4", "3.5", "3.6", "3.7", "3.8"}

def __init__(self, name, version, pretty_version=None):
"""
Expand Down
8 changes: 5 additions & 3 deletions poetry/puzzle/provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
from poetry.utils.helpers import temporary_directory
from poetry.utils.env import EnvManager
from poetry.utils.env import EnvCommandError
from poetry.utils.env import VirtualEnv
from poetry.utils.inspector import Inspector
from poetry.utils.setup_reader import SetupReader
from poetry.utils.toml_file import TomlFile
Expand Down Expand Up @@ -326,9 +327,10 @@ def get_package_from_directory(
os.chdir(str(directory))

try:
cwd = directory
venv = EnvManager().get(cwd)
venv.run("python", "setup.py", "egg_info")
with temporary_directory() as tmp_dir:
EnvManager.build_venv(tmp_dir)
venv = VirtualEnv(Path(tmp_dir), Path(tmp_dir))
venv.run("python", "setup.py", "egg_info")
except EnvCommandError:
result = SetupReader.read_from_directory(directory)
if not result["name"]:
Expand Down
Loading

0 comments on commit 7f5acc3

Please sign in to comment.