fix: fix switching of repositories (#519)

* fix: fix repo switching

Extend the functionality of repo switching by creating a backup before the switch. Also implement a rollback mechanic in case of an error.

Signed-off-by: Dominik Willner <th33xitus@gmail.com>

* refactor: fail when installing requirements fails

Signed-off-by: Dominik Willner <th33xitus@gmail.com>

* refactor: display owner and repo in main menu on separate lines

long owner and repo names would case the menu to be too wide

Signed-off-by: Dominik Willner <th33xitus@gmail.com>

---------

Signed-off-by: Dominik Willner <th33xitus@gmail.com>
This commit is contained in:
dw-0
2024-09-05 20:31:38 +02:00
committed by GitHub
parent e438081c35
commit a54514c400
9 changed files with 264 additions and 134 deletions

View File

@@ -124,10 +124,12 @@ def get_install_status(
else:
status = 1 # incomplete
org, repo = get_repo_name(repo_dir)
return ComponentStatus(
status=status,
instances=instances,
repo=get_repo_name(repo_dir),
owner=org,
repo=repo,
local=get_local_commit(repo_dir),
remote=get_remote_commit(repo_dir),
)

View File

@@ -16,6 +16,10 @@ from utils.input_utils import get_confirm, get_number_input
from utils.instance_utils import get_instances
class GitException(Exception):
pass
def git_clone_wrapper(
repo: str, target_dir: Path, branch: str | None = None, force: bool = False
) -> None:
@@ -43,10 +47,10 @@ def git_clone_wrapper(
except CalledProcessError:
log = "An unexpected error occured during cloning of the repository."
Logger.print_error(log)
return
raise GitException(log)
except OSError as e:
Logger.print_error(f"Error removing existing repository: {e.strerror}")
return
raise GitException(f"Error removing existing repository: {e.strerror}")
def git_pull_wrapper(repo: str, target_dir: Path) -> None:
@@ -66,20 +70,22 @@ def git_pull_wrapper(repo: str, target_dir: Path) -> None:
return
def get_repo_name(repo: Path) -> str | None:
def get_repo_name(repo: Path) -> tuple[str, str] | None:
"""
Helper method to extract the organisation and name of a repository |
:param repo: repository to extract the values from
:return: String in form of "<orga>/<name>" or None
"""
if not repo.exists() or not repo.joinpath(".git").exists():
return "-"
return "-", "-"
try:
cmd = ["git", "-C", repo.as_posix(), "config", "--get", "remote.origin.url"]
result: str = check_output(cmd, stderr=DEVNULL).decode(encoding="utf-8")
substrings: List[str] = result.strip().split("/")[-2:]
return "/".join(substrings).replace(".git", "")
return substrings[0], substrings[1]
# return "/".join(substrings).replace(".git", "")
except CalledProcessError:
return None

View File

@@ -39,6 +39,10 @@ SysCtlServiceAction = Literal[
SysCtlManageAction = Literal["daemon-reload", "reset-failed"]
class VenvCreationFailedException(Exception):
pass
def kill(opt_err_msg: str = "") -> None:
"""
Kills the application |
@@ -87,11 +91,12 @@ def parse_packages_from_file(source_file: Path) -> List[str]:
return packages
def create_python_venv(target: Path) -> bool:
def create_python_venv(target: Path, force: bool = False) -> bool:
"""
Create a python 3 virtualenv at the provided target destination.
Returns True if the virtualenv was created successfully.
Returns False if the virtualenv already exists, recreation was declined or creation failed.
:param force: Force recreation of the virtualenv
:param target: Path where to create the virtualenv at
:return: bool
"""
@@ -106,7 +111,7 @@ def create_python_venv(target: Path) -> bool:
Logger.print_error(f"Error setting up virtualenv:\n{e}")
return False
else:
if not get_confirm(
if not force and not get_confirm(
"Virtualenv already exists. Re-create?", default_choice=False
):
Logger.print_info("Skipping re-creation of virtualenv ...")
@@ -174,14 +179,14 @@ def install_python_requirements(target: Path, requirements: Path) -> None:
if result.returncode != 0 or result.stderr:
Logger.print_error(f"{result.stderr}", False)
Logger.print_error("Installing Python requirements failed!")
return
raise VenvCreationFailedException("Installing Python requirements failed!")
Logger.print_ok("Installing Python requirements successful!")
except CalledProcessError as e:
log = f"Error installing Python requirements:\n{e.output.decode()}"
except Exception as e:
log = f"Error installing Python requirements: {e}"
Logger.print_error(log)
raise
raise VenvCreationFailedException(log)
def update_system_package_lists(silent: bool, rls_info_change=False) -> None: