feat(RepoManager): implement RepoManager
Signed-off-by: Dominik Willner <th33xitus@gmail.com>
This commit is contained in:
@@ -23,6 +23,7 @@ from kiauh.modules.klipper.klipper_utils import (
|
||||
print_instance_overview,
|
||||
print_missing_usergroup_dialog,
|
||||
)
|
||||
from kiauh.repo_manager.repo_manager import RepoManager
|
||||
from kiauh.utils.constants import CURRENT_USER, KLIPPER_DIR, KLIPPER_ENV_DIR
|
||||
from kiauh.utils.input_utils import (
|
||||
get_confirm,
|
||||
@@ -33,7 +34,6 @@ from kiauh.utils.input_utils import (
|
||||
from kiauh.utils.logger import Logger
|
||||
from kiauh.utils.system_utils import (
|
||||
parse_packages_from_file,
|
||||
clone_repo,
|
||||
create_python_venv,
|
||||
install_python_requirements,
|
||||
update_system_package_lists,
|
||||
@@ -119,9 +119,12 @@ def install_klipper(instance_manager: InstanceManager) -> None:
|
||||
|
||||
def setup_klipper_prerequesites() -> None:
|
||||
# clone klipper TODO: read branch and url from json to allow forks
|
||||
url = "https://github.com/Klipper3D/klipper"
|
||||
branch = "master"
|
||||
clone_repo(Path(KLIPPER_DIR), url, branch)
|
||||
repo_manager = RepoManager(
|
||||
repo="https://github.com/Klipper3D/klipper",
|
||||
branch="master",
|
||||
target_dir=KLIPPER_DIR,
|
||||
)
|
||||
repo_manager.clone_repo()
|
||||
|
||||
# install klipper dependencies and create python virtualenv
|
||||
install_klipper_packages(Path(KLIPPER_DIR))
|
||||
|
||||
0
kiauh/repo_manager/__init__.py
Normal file
0
kiauh/repo_manager/__init__.py
Normal file
105
kiauh/repo_manager/repo_manager.py
Normal file
105
kiauh/repo_manager/repo_manager.py
Normal file
@@ -0,0 +1,105 @@
|
||||
#!/usr/bin/env python
|
||||
|
||||
# ======================================================================= #
|
||||
# Copyright (C) 2020 - 2023 Dominik Willner <th33xitus@gmail.com> #
|
||||
# #
|
||||
# This file is part of KIAUH - Klipper Installation And Update Helper #
|
||||
# https://github.com/dw-0/kiauh #
|
||||
# #
|
||||
# This file may be distributed under the terms of the GNU GPLv3 license #
|
||||
# ======================================================================= #
|
||||
|
||||
import shutil
|
||||
import subprocess
|
||||
from pathlib import Path
|
||||
from typing import Union
|
||||
|
||||
from kiauh.utils.input_utils import get_confirm
|
||||
from kiauh.utils.logger import Logger
|
||||
|
||||
|
||||
# noinspection PyMethodMayBeStatic
|
||||
class RepoManager:
|
||||
def __init__(self, repo: str, branch: str, target_dir: str):
|
||||
self._repo = repo
|
||||
self._branch = branch
|
||||
self._method = self._get_method()
|
||||
self._target_dir = target_dir
|
||||
|
||||
@property
|
||||
def repo(self) -> str:
|
||||
return self._repo
|
||||
|
||||
@repo.setter
|
||||
def repo(self, value) -> None:
|
||||
self._repo = value
|
||||
|
||||
@property
|
||||
def branch(self) -> str:
|
||||
return self._branch
|
||||
|
||||
@branch.setter
|
||||
def branch(self, value) -> None:
|
||||
self._branch = value
|
||||
|
||||
@property
|
||||
def method(self) -> str:
|
||||
return self._method
|
||||
|
||||
@method.setter
|
||||
def method(self, value) -> None:
|
||||
self._method = value
|
||||
|
||||
@property
|
||||
def target_dir(self) -> str:
|
||||
return self._target_dir
|
||||
|
||||
@target_dir.setter
|
||||
def target_dir(self, value) -> None:
|
||||
self._target_dir = value
|
||||
|
||||
def clone_repo(self):
|
||||
log = f"Cloning repository from '{self.repo}' with method '{self.method}'"
|
||||
Logger.print_info(log)
|
||||
try:
|
||||
question = "Target directory already exists. Overwrite?"
|
||||
if Path(self.target_dir).exists() and get_confirm(question):
|
||||
shutil.rmtree(self.target_dir)
|
||||
else:
|
||||
Logger.print_info("Skipping re-clone of repository ...")
|
||||
return
|
||||
|
||||
self._clone()
|
||||
self._checkout()
|
||||
except subprocess.CalledProcessError:
|
||||
log = "An unexpected error occured during cloning of the repository."
|
||||
Logger.print_error(log)
|
||||
return
|
||||
except OSError as e:
|
||||
Logger.print_error(f"Error removing existing repository: {e.strerror}")
|
||||
return
|
||||
|
||||
def _clone(self):
|
||||
try:
|
||||
command = ["git", "clone", f"{self.repo}"]
|
||||
subprocess.run(command, check=True)
|
||||
|
||||
Logger.print_ok("Clone successfull!")
|
||||
except subprocess.CalledProcessError as e:
|
||||
log = f"Error cloning repository {self.repo}: {e.stderr.decode()}"
|
||||
Logger.print_error(log)
|
||||
raise
|
||||
|
||||
def _checkout(self):
|
||||
try:
|
||||
command = ["git", "checkout", f"{self.branch}"]
|
||||
subprocess.run(command, cwd=self.target_dir, check=True)
|
||||
|
||||
Logger.print_ok("Checkout successfull!")
|
||||
except subprocess.CalledProcessError as e:
|
||||
log = f"Error checking out branch {self.branch}: {e.stderr.decode()}"
|
||||
Logger.print_error(log)
|
||||
raise
|
||||
|
||||
def _get_method(self) -> str:
|
||||
return "ssh" if self.repo.startswith("git") else "https"
|
||||
@@ -42,31 +42,6 @@ def kill(opt_err_msg=None) -> None:
|
||||
sys.exit(1)
|
||||
|
||||
|
||||
def clone_repo(target_dir: Path, url: str, branch: str) -> None:
|
||||
Logger.print_info(f"Cloning repository from {url}")
|
||||
if not target_dir.exists():
|
||||
try:
|
||||
command = ["git", "clone", f"{url}"]
|
||||
subprocess.run(command, check=True)
|
||||
|
||||
command = ["git", "checkout", f"{branch}"]
|
||||
subprocess.run(command, cwd=target_dir, check=True)
|
||||
|
||||
Logger.print_ok("Clone successfull!")
|
||||
except subprocess.CalledProcessError as e:
|
||||
print("Error cloning repository:", e.output.decode())
|
||||
else:
|
||||
overwrite_target = get_confirm("Target directory already exists. Overwrite?")
|
||||
if overwrite_target:
|
||||
try:
|
||||
shutil.rmtree(target_dir)
|
||||
clone_repo(target_dir, url, branch)
|
||||
except OSError as e:
|
||||
print("Error removing existing repository:", e.strerror)
|
||||
else:
|
||||
print("Skipping re-clone of repository ...")
|
||||
|
||||
|
||||
def parse_packages_from_file(source_file) -> List[str]:
|
||||
packages = []
|
||||
print("Reading dependencies...")
|
||||
|
||||
Reference in New Issue
Block a user