Skip to content

Commit

Permalink
feat: migrate uninstall command
Browse files Browse the repository at this point in the history
  • Loading branch information
Alec4r committed Aug 10, 2022
1 parent 0b1e088 commit bc11b68
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 13 deletions.
29 changes: 16 additions & 13 deletions tvm/cli.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
"""Entry point for all the `tvm *` commands."""
import datetime
import json
import os
import pathlib
Expand All @@ -10,7 +9,6 @@
import string
import subprocess
import sys
import zipfile
from distutils.dir_util import copy_tree
from distutils.version import LooseVersion
from typing import Optional
Expand All @@ -24,8 +22,10 @@
from tvm.templates.tvm_activate import TVM_ACTIVATE_SCRIPT
from tvm.version_manager.application.tutor_version_finder import TutorVersionFinder
from tvm.version_manager.application.tutor_version_installer import TutorVersionInstaller
from tvm.version_manager.application.tutor_version_uninstaller import TutorVersionUninstaller
from tvm.version_manager.application.tutor_vesion_lister import TutorVersionLister
from tvm.version_manager.domain.tutor_version_format_error import TutorVersionFormatError
from tvm.version_manager.domain.tutor_version_is_not_installed import TutorVersionIsNotInstalled
from tvm.version_manager.infrastructure.version_manager_git_repository import VersionManagerGitRepository

VERSIONS_URL = "https://api.github.com/repos/overhangio/tutor/tags"
Expand Down Expand Up @@ -216,7 +216,7 @@ def list_versions_backup(limit: int):


@click.command(name="install")
@click.argument('version', callback=validate_version)
@click.argument('version', required=True)
def install(version: str):
"""Install the given VERSION of tutor in the .tvm directory."""
repository = VersionManagerGitRepository()
Expand All @@ -234,19 +234,22 @@ def install(version: str):
installer(version=tutor_version)


def do_uninstall(version: str):
"""Uninstall the version by deleting the dir."""
try:
shutil.rmtree(f'{TVM_PATH}/{version}')
except FileNotFoundError:
click.echo('Nothing to uninstall for this version')


@click.command(name="uninstall")
@click.argument('version', type=TutorVersionType())
@click.argument('version', required=True)
def uninstall(version: str):
"""Install the given VERSION of tutor in the .tvm directory."""
do_uninstall(version=version)
repository = VersionManagerGitRepository()
uninstaller = TutorVersionUninstaller(repository=repository)
try:
uninstaller(version=version)
click.echo(click.style(
f"The {version} has been uninstalled.",
fg='green',
))
except TutorVersionFormatError as format_err:
raise click.UsageError(f'{format_err}')
except TutorVersionIsNotInstalled as not_installed_err:
raise click.exceptions.ClickException(f"{not_installed_err}")


def get_active_version() -> str:
Expand Down
11 changes: 11 additions & 0 deletions tvm/version_manager/application/tutor_version_uninstaller.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from tvm.version_manager.domain.tutor_version import TutorVersion
from tvm.version_manager.domain.version_manager_repository import VersionManagerRepository


class TutorVersionUninstaller:
def __init__(self, repository: VersionManagerRepository) -> None:
self.repository = repository

def __call__(self, version: str) -> None:
version = TutorVersion(version)
self.repository.uninstall_version(version)
2 changes: 2 additions & 0 deletions tvm/version_manager/domain/tutor_version_is_not_installed.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class TutorVersionIsNotInstalled(Exception):
pass
4 changes: 4 additions & 0 deletions tvm/version_manager/domain/version_manager_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,7 @@ def install_version(self, version: TutorVersion) -> None:
@abstractmethod
def find_version(self, version: TutorVersion) -> Optional[TutorVersion]:
pass

@abstractmethod
def uninstall_version(self, version: TutorVersion) -> None:
pass
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Actions to get tutor versions"""
import json
import os
import shutil
import stat
import pathlib
import subprocess
Expand All @@ -9,6 +10,7 @@

import requests
from tvm.version_manager.domain.tutor_version import TutorVersion
from tvm.version_manager.domain.tutor_version_is_not_installed import TutorVersionIsNotInstalled
from tvm.version_manager.domain.version_manager_repository import (
VersionManagerRepository,
)
Expand Down Expand Up @@ -151,3 +153,9 @@ def install_tutor(self, version: TutorVersion) -> None:
f'pip install -e {self.TVM_PATH}/{version}/overhangio-tutor-*/',
shell=True, check=True,
executable='/bin/bash')

def uninstall_version(self, version: TutorVersion) -> None:
try:
shutil.rmtree(f'{self.TVM_PATH}/{version}')
except FileNotFoundError:
raise TutorVersionIsNotInstalled(f"The version {version} is not installed")

0 comments on commit bc11b68

Please sign in to comment.