From 4d42022764386eaf96639bd08030483604de5696 Mon Sep 17 00:00:00 2001 From: Tolker-KU <55140581+Tolker-KU@users.noreply.github.com> Date: Thu, 31 Dec 2020 09:40:08 +0100 Subject: [PATCH] Use conda remove to clean up existing conda environment. Closes https://github.com/theacodes/nox/issues/372. (#373) Co-authored-by: Santos Gallegos Co-authored-by: Christopher Wilcox --- nox/virtualenv.py | 37 ++++++++++++++++++++++++------------- 1 file changed, 24 insertions(+), 13 deletions(-) diff --git a/nox/virtualenv.py b/nox/virtualenv.py index 4a086e0f..c32efaf2 100644 --- a/nox/virtualenv.py +++ b/nox/virtualenv.py @@ -139,17 +139,6 @@ def locate_using_path_and_version(version: str) -> Optional[str]: return None -def _clean_location(self: "Union[CondaEnv, VirtualEnv]") -> bool: - """Deletes any existing path-based environment""" - if os.path.exists(self.location): - if self.reuse_existing: - return False - else: - shutil.rmtree(self.location) - - return True - - class PassthroughEnv(ProcessEnv): """Represents the environment used to run nox itself @@ -200,7 +189,21 @@ def __init__( self.venv_params = venv_params if venv_params else [] super(CondaEnv, self).__init__() - _clean_location = _clean_location + def _clean_location(self) -> bool: + """Deletes existing conda environment""" + if os.path.exists(self.location): + if self.reuse_existing: + return False + else: + cmd = ["conda", "remove", "--yes", "--prefix", self.location, "--all"] + nox.command.run(cmd, silent=True, log=False) + # Make sure that location is clean + try: + shutil.rmtree(self.location) + except FileNotFoundError: + pass + + return True @property def bin_paths(self) -> List[str]: @@ -302,7 +305,15 @@ def __init__( self.venv_params = venv_params if venv_params else [] super(VirtualEnv, self).__init__(env={"VIRTUAL_ENV": self.location}) - _clean_location = _clean_location + def _clean_location(self) -> bool: + """Deletes any existing virtual environment""" + if os.path.exists(self.location): + if self.reuse_existing: + return False + else: + shutil.rmtree(self.location) + + return True @property def _resolved_interpreter(self) -> str: