From 3006c069bcff2441f6c3e5a327a10f9000c48157 Mon Sep 17 00:00:00 2001 From: henrrypg Date: Thu, 15 Dec 2022 10:41:10 -0500 Subject: [PATCH] feat: add prune option to delete all project folder --- .../tvm_topic_guides/environment_manager.rst | 3 +++ .../application/test_tutor_project_remover.py | 2 +- ...environment_manager_in_memory_repository.py | 2 +- tvm/cli.py | 15 +++++++++++---- .../application/tutor_project_remover.py | 4 ++-- .../domain/environment_manager_repository.py | 2 +- .../environment_manager_git_repository.py | 18 +++++++++++------- 7 files changed, 30 insertions(+), 16 deletions(-) diff --git a/docs/source/tvm_topic_guides/environment_manager.rst b/docs/source/tvm_topic_guides/environment_manager.rst index adfd067..b54e52d 100644 --- a/docs/source/tvm_topic_guides/environment_manager.rst +++ b/docs/source/tvm_topic_guides/environment_manager.rst @@ -38,6 +38,9 @@ Remove a Project # tvm project remove tvm-test v14.0.0 +.. note:: You can use the flag --prune to remove the project folder. Ex: `tvm project remove tvm-test v14.0.0 --prune` + + Activate a Project Environment ------------------------------ diff --git a/tests/environment_manager/application/test_tutor_project_remover.py b/tests/environment_manager/application/test_tutor_project_remover.py index 6a3bb90..a28957b 100644 --- a/tests/environment_manager/application/test_tutor_project_remover.py +++ b/tests/environment_manager/application/test_tutor_project_remover.py @@ -14,7 +14,7 @@ def test_should_remove_tutor_project(): # When remove = TutorProjectRemover(repository=repository) - remove() + remove(prune=False) # Then assert repository.PROJECT_NAME == [] diff --git a/tests/environment_manager/infrastructure/environment_manager_in_memory_repository.py b/tests/environment_manager/infrastructure/environment_manager_in_memory_repository.py index 10bbe03..6a8c01e 100644 --- a/tests/environment_manager/infrastructure/environment_manager_in_memory_repository.py +++ b/tests/environment_manager/infrastructure/environment_manager_in_memory_repository.py @@ -15,7 +15,7 @@ def project_creator(self, version: str) -> None: else: raise Exception('There is already a project initiated.') - def project_remover(self) -> None: + def project_remover(self, prune: bool) -> None: """Tutor Project Remove to environment manager.""" if self.PROJECT_NAME: self.PROJECT_NAME.clear() diff --git a/tvm/cli.py b/tvm/cli.py index 5712646..56dfdde 100644 --- a/tvm/cli.py +++ b/tvm/cli.py @@ -410,9 +410,10 @@ def init(name: str = None, version: str = None): @click.command(name="remove") -@click.argument('name', required=True) @click.argument('version', required=True) -def remove(name: str, version: str): +@click.argument('name', required=True) +@click.option('--prune', is_flag=True, help="Remove all files in project folder.") +def remove(name: str, version: str, prune: bool): """Remove TVM project.""" project_name = f"{version}@{name}" @@ -424,11 +425,17 @@ def remove(name: str, version: str): if not os.path.exists(tvm_project_folder / 'config.yml'): raise click.UsageError('This project was created in older version or have corrupted files.') from IndexError - click.confirm(text=f"Are you sure you want to remove the project {project_name}?", abort=True) + if prune: + click.echo(click.style(f"Removing project {project_name} and all its files in project folder.", fg='red')) + else: + click.echo(click.style(f"Removing TVM files for project {project_name}.", fg='yellow')) + click.echo(click.style("Use --prune to remove all files in project folder.", fg='yellow')) + + click.confirm(text=f"\nAre you sure you want to remove the project {project_name}?", abort=True) repository = environment_manager(project_path=f"{project_name}") remover = TutorProjectRemover(repository=repository) - remover() + remover(prune=prune) @click.command(name="install", context_settings={"ignore_unknown_options": True}) diff --git a/tvm/environment_manager/application/tutor_project_remover.py b/tvm/environment_manager/application/tutor_project_remover.py index 2ecd288..29f9c08 100644 --- a/tvm/environment_manager/application/tutor_project_remover.py +++ b/tvm/environment_manager/application/tutor_project_remover.py @@ -9,6 +9,6 @@ def __init__(self, repository: EnvironmentManagerRepository) -> None: """init.""" self.repository = repository - def __call__(self) -> None: + def __call__(self, prune: bool) -> None: """call.""" - self.repository.project_remover() + self.repository.project_remover(prune=prune) diff --git a/tvm/environment_manager/domain/environment_manager_repository.py b/tvm/environment_manager/domain/environment_manager_repository.py index 4ccfeac..ba0f3fc 100644 --- a/tvm/environment_manager/domain/environment_manager_repository.py +++ b/tvm/environment_manager/domain/environment_manager_repository.py @@ -13,7 +13,7 @@ def project_creator(self, project_name: ProjectName) -> None: """Tutor Project Init to environment manager.""" @abstractmethod - def project_remover(self) -> None: + def project_remover(self, prune: bool) -> None: """Tutor Project Remove to environment manager.""" @abstractmethod diff --git a/tvm/environment_manager/infrastructure/environment_manager_git_repository.py b/tvm/environment_manager/infrastructure/environment_manager_git_repository.py index 338c1c8..ac0dfb9 100644 --- a/tvm/environment_manager/infrastructure/environment_manager_git_repository.py +++ b/tvm/environment_manager/infrastructure/environment_manager_git_repository.py @@ -38,16 +38,23 @@ def project_creator(self, project_name: ProjectName) -> None: self.create_active_script(data) self.create_project(project_name) - def project_remover(self) -> None: + def project_remover(self, prune: bool) -> None: """Remove tutor project.""" with open(f"{TVM_PATH}/{self.PROJECT_PATH}/config.yml", "r", encoding='utf-8') as f: data = yaml.load(f, Loader=yaml.FullLoader) + deleted_dirs = [] for project in data['project_directories']: - self.remove_project(project) - self.remove_project(f"{TVM_PATH}/{self.PROJECT_PATH}") + if os.path.exists(f"{project}"): + if not prune: + project = f"{project}/.tvm" + deleted_dirs.append(project) + self.remove_project(project) + else: + self.logger.echo(f"Project not found in {project}, if you moved it from the original path, you must remove it manually.\n") # pylint: disable=C0301 - self.logger.echo(f"Project {self.PROJECT_PATH} removed.") + self.remove_project(f"{TVM_PATH}/{self.PROJECT_PATH}") + self.logger.echo(f"Project {self.PROJECT_PATH} removed from the following paths: {', '.join(deleted_dirs)}") # pylint: disable=C0301 def remove_project(self, project_path: str): """Remove project.""" @@ -56,7 +63,6 @@ def remove_project(self, project_path: str): except PermissionError: self.logger.echo( "Don't Worry, TVM just needs sudo permissions to delete your project files." - "You can find more information about it in our docs." ) subprocess.call( [ @@ -66,8 +72,6 @@ def remove_project(self, project_path: str): f"{project_path}", ] ) - except FileExistsError: - pass def current_version(self) -> ProjectName: """Project name in current version."""