Skip to content

Commit

Permalink
feat: add prune option to delete all project folder
Browse files Browse the repository at this point in the history
  • Loading branch information
Henrrypg committed Dec 15, 2022
1 parent 3990790 commit 3006c06
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 16 deletions.
3 changes: 3 additions & 0 deletions docs/source/tvm_topic_guides/environment_manager.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
------------------------------

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def test_should_remove_tutor_project():

# When
remove = TutorProjectRemover(repository=repository)
remove()
remove(prune=False)

# Then
assert repository.PROJECT_NAME == []
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
15 changes: 11 additions & 4 deletions tvm/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}"

Expand All @@ -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})
Expand Down
4 changes: 2 additions & 2 deletions tvm/environment_manager/application/tutor_project_remover.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand All @@ -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(
[
Expand All @@ -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."""
Expand Down

0 comments on commit 3006c06

Please sign in to comment.