diff --git a/poetry/console/commands/env/info.py b/poetry/console/commands/env/info.py index d81343c7efc..0502b36d37a 100644 --- a/poetry/console/commands/env/info.py +++ b/poetry/console/commands/env/info.py @@ -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(): diff --git a/poetry/console/commands/env/list.py b/poetry/console/commands/env/list.py index edd82debb0d..39186a88b2d 100644 --- a/poetry/console/commands/env/list.py +++ b/poetry/console/commands/env/list.py @@ -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) diff --git a/poetry/console/commands/env/remove.py b/poetry/console/commands/env/remove.py index f03588998e0..858020f36b7 100644 --- a/poetry/console/commands/env/remove.py +++ b/poetry/console/commands/env/remove.py @@ -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: {}".format(venv.path)) diff --git a/poetry/console/commands/env/use.py b/poetry/console/commands/env/use.py index e7b84da17bc..48984bdd831 100644 --- a/poetry/console/commands/env/use.py +++ b/poetry/console/commands/env/use.py @@ -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: {}".format(env.path)) diff --git a/poetry/utils/env.py b/poetry/utils/env.py index 89d80b1661c..199b0f07b87 100644 --- a/poetry/utils/env.py +++ b/poetry/utils/env.py @@ -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" @@ -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) @@ -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: @@ -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) @@ -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 @@ -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) @@ -320,7 +332,9 @@ 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" @@ -328,10 +342,12 @@ def remove(self, python, cwd): # type: (str, Optional[Path]) -> Env 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 @@ -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) @@ -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 diff --git a/tests/console/commands/env/test_list.py b/tests/console/commands/env/test_list.py index 3e175cca01a..1d79b8e57a7 100644 --- a/tests/console/commands/env/test_list.py +++ b/tests/console/commands/env/test_list.py @@ -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() @@ -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() diff --git a/tests/console/commands/env/test_remove.py b/tests/console/commands/env/test_remove.py index 9b1e35c255c..bf3c3a6c4fb 100644 --- a/tests/console/commands/env/test_remove.py +++ b/tests/console/commands/env/test_remove.py @@ -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() @@ -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() diff --git a/tests/console/commands/env/test_use.py b/tests/console/commands/env/test_use.py index 7e1c3adb972..097ecf41f0d 100644 --- a/tests/console/commands/env/test_use.py +++ b/tests/console/commands/env/test_use.py @@ -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( @@ -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]) @@ -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])