Skip to content

Commit

Permalink
Fix poetry env commands not using package name (python-poetry#1417)
Browse files Browse the repository at this point in the history
  • Loading branch information
pspeter committed Oct 15, 2019
1 parent 1814327 commit ff4e9e5
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 29 deletions.
4 changes: 3 additions & 1 deletion poetry/console/commands/env/info.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ def handle(self):
from poetry.utils.env import EnvManager

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

if self.option("path"):
if not env.is_venv():
Expand Down
6 changes: 4 additions & 2 deletions poetry/console/commands/env/list.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ def handle(self):

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

for venv in manager.list(self.poetry.file.parent):
for venv in manager.list(self.poetry.file.parent, self.poetry.package.name):
name = venv.path.name
if self.option("full-path"):
name = str(venv.path)
Expand Down
4 changes: 3 additions & 1 deletion poetry/console/commands/env/remove.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ def handle(self):

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

self.line("Deleted virtualenv: <comment>{}</comment>".format(venv.path))
6 changes: 4 additions & 2 deletions poetry/console/commands/env/use.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@ def handle(self):
manager = EnvManager(poetry.config)

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

return

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

self.line("Using virtualenv: <comment>{}</>".format(env.path))
46 changes: 31 additions & 15 deletions poetry/utils/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,9 @@ def __init__(self, config=None): # type: (Config) -> None

self._config = config

def activate(self, python, cwd, io): # type: (str, Optional[Path], IO) -> Env
def activate(
self, python, cwd, io, name=None
): # type: (str, Optional[Path], IO, Optional[str]) -> Env
venv_path = self._config.get("virtualenvs.path")
if venv_path is None:
venv_path = Path(CACHE_DIR) / "virtualenvs"
Expand Down Expand Up @@ -179,10 +181,12 @@ def activate(self, python, cwd, io): # type: (str, Optional[Path], IO) -> Env
python_version = Version.parse(python_version.strip())
minor = "{}.{}".format(python_version.major, python_version.minor)
patch = python_version.text
if not name:
name = cwd.name

create = False
envs = tomlkit.document()
base_env_name = self.generate_env_name(cwd.name, str(cwd))
base_env_name = self.generate_env_name(name, str(cwd))
if envs_file.exists():
envs = envs_file.read()
current_env = envs.get(base_env_name)
Expand All @@ -194,8 +198,8 @@ def activate(self, python, cwd, io): # type: (str, Optional[Path], IO) -> Env
# We need to recreate
create = True

name = "{}-py{}".format(base_env_name, minor)
venv = venv_path / name
full_name = "{}-py{}".format(base_env_name, minor)
venv = venv_path / full_name

# Create if needed
if not venv.exists() or venv.exists() and create:
Expand All @@ -211,22 +215,25 @@ def activate(self, python, cwd, io): # type: (str, Optional[Path], IO) -> Env
if patch != current_patch:
create = True

self.create_venv(cwd, io, executable=python, force=create)
self.create_venv(cwd, io, name=name, executable=python, force=create)

# Activate
envs[base_env_name] = {"minor": minor, "patch": patch}
envs_file.write(envs)

return self.get(cwd, reload=True)
return self.get(cwd, reload=True, name=name)

def deactivate(self, cwd, io): # type: (Optional[Path], IO) -> None
def deactivate(
self, cwd, io, name=None
): # type: (Optional[Path], IO, Optional[str]) -> None
venv_path = self._config.get("virtualenvs.path")
if venv_path is None:
venv_path = Path(CACHE_DIR) / "virtualenvs"
else:
venv_path = Path(venv_path)

name = cwd.name
if not name:
name = cwd.name
name = self.generate_env_name(name, str(cwd))

envs_file = TomlFile(venv_path / self.ENVS_FILE)
Expand All @@ -243,7 +250,9 @@ def deactivate(self, cwd, io): # type: (Optional[Path], IO) -> None

envs_file.write(envs)

def get(self, cwd, reload=False): # type: (Path, bool) -> Env
def get(
self, cwd, reload=False, name=None
): # type: (Path, bool, Optional[str]) -> Env
if self._env is not None and not reload:
return self._env

Expand All @@ -257,7 +266,10 @@ def get(self, cwd, reload=False): # type: (Path, bool) -> Env

envs_file = TomlFile(venv_path / self.ENVS_FILE)
env = None
base_env_name = self.generate_env_name(cwd.name, str(cwd))
if not name:
name = cwd.name

base_env_name = self.generate_env_name(name, str(cwd))
if envs_file.exists():
envs = envs_file.read()
env = envs.get(base_env_name)
Expand Down Expand Up @@ -320,18 +332,22 @@ def list(self, cwd, name=None): # type: (Path, Optional[str]) -> List[VirtualEn
for p in sorted(venv_path.glob("{}-py*".format(venv_name)))
]

def remove(self, python, cwd): # type: (str, Optional[Path]) -> Env
def remove(
self, python, cwd, name=None
): # type: (str, Optional[Path], Optional[str]) -> Env
venv_path = self._config.get("virtualenvs.path")
if venv_path is None:
venv_path = Path(CACHE_DIR) / "virtualenvs"
else:
venv_path = Path(venv_path)

envs_file = TomlFile(venv_path / self.ENVS_FILE)
base_env_name = self.generate_env_name(cwd.name, str(cwd))
if not name:
name = cwd.name

base_env_name = self.generate_env_name(name, str(cwd))
if python.startswith(base_env_name):
venvs = self.list(cwd)
venvs = self.list(cwd, name)
for venv in venvs:
if venv.path.name == python:
# Exact virtualenv name
Expand All @@ -341,7 +357,7 @@ def remove(self, python, cwd): # type: (str, Optional[Path]) -> Env
return venv

venv_minor = ".".join(str(v) for v in venv.version_info[:2])
base_env_name = self.generate_env_name(cwd.name, str(cwd))
base_env_name = self.generate_env_name(name, str(cwd))
envs = envs_file.read()

current_env = envs.get(base_env_name)
Expand Down Expand Up @@ -418,7 +434,7 @@ def create_venv(
if self._env is not None and not force:
return self._env

env = self.get(cwd, reload=True)
env = self.get(cwd, reload=True, name=name)
if env.is_venv() and not force:
# Already inside a virtualenv.
return env
Expand Down
4 changes: 2 additions & 2 deletions tests/console/commands/env/test_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def test_none_activated(app, tmp_dir):
app.poetry.config.merge({"virtualenvs": {"path": str(tmp_dir)}})

venv_name = EnvManager.generate_env_name(
"simple_project", str(app.poetry.file.parent)
"simple-project", str(app.poetry.file.parent)
)
(Path(tmp_dir) / "{}-py3.7".format(venv_name)).mkdir()
(Path(tmp_dir) / "{}-py3.6".format(venv_name)).mkdir()
Expand All @@ -34,7 +34,7 @@ def test_activated(app, tmp_dir):
app.poetry.config.merge({"virtualenvs": {"path": str(tmp_dir)}})

venv_name = EnvManager.generate_env_name(
"simple_project", str(app.poetry.file.parent)
"simple-project", str(app.poetry.file.parent)
)
(Path(tmp_dir) / "{}-py3.7".format(venv_name)).mkdir()
(Path(tmp_dir) / "{}-py3.6".format(venv_name)).mkdir()
Expand Down
4 changes: 2 additions & 2 deletions tests/console/commands/env/test_remove.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def test_remove_by_python_version(app, tmp_dir, mocker):
app.poetry.config.merge({"virtualenvs": {"path": str(tmp_dir)}})

venv_name = EnvManager.generate_env_name(
"simple_project", str(app.poetry.file.parent)
"simple-project", str(app.poetry.file.parent)
)
(Path(tmp_dir) / "{}-py3.7".format(venv_name)).mkdir()
(Path(tmp_dir) / "{}-py3.6".format(venv_name)).mkdir()
Expand Down Expand Up @@ -39,7 +39,7 @@ def test_remove_by_name(app, tmp_dir):
app.poetry.config.merge({"virtualenvs": {"path": str(tmp_dir)}})

venv_name = EnvManager.generate_env_name(
"simple_project", str(app.poetry.file.parent)
"simple-project", str(app.poetry.file.parent)
)
(Path(tmp_dir) / "{}-py3.7".format(venv_name)).mkdir()
(Path(tmp_dir) / "{}-py3.6".format(venv_name)).mkdir()
Expand Down
7 changes: 3 additions & 4 deletions tests/console/commands/env/test_use.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def test_activate_activates_non_existing_virtualenv_no_envs_file(app, tmp_dir, m
tester.execute("3.7")

venv_name = EnvManager.generate_env_name(
"simple_project", str(app.poetry.file.parent)
"simple-project", str(app.poetry.file.parent)
)

m.assert_called_with(
Expand Down Expand Up @@ -88,7 +88,7 @@ def test_get_prefers_explicitly_activated_virtualenvs_over_env_var(
os.environ["VIRTUAL_ENV"] = "/environment/prefix"

venv_name = EnvManager.generate_env_name(
"simple_project", str(app.poetry.file.parent)
"simple-project", str(app.poetry.file.parent)
)
current_python = sys.version_info[:3]
python_minor = ".".join(str(v) for v in current_python[:2])
Expand Down Expand Up @@ -128,9 +128,8 @@ def test_get_prefers_explicitly_activated_non_existing_virtualenvs_over_env_var(
app, tmp_dir, mocker
):
os.environ["VIRTUAL_ENV"] = "/environment/prefix"

venv_name = EnvManager.generate_env_name(
"simple_project", str(app.poetry.file.parent)
"simple-project", str(app.poetry.file.parent)
)
current_python = sys.version_info[:3]
python_minor = ".".join(str(v) for v in current_python[:2])
Expand Down

0 comments on commit ff4e9e5

Please sign in to comment.