diff --git a/.gitignore b/.gitignore index 83a9457..8105baa 100644 --- a/.gitignore +++ b/.gitignore @@ -3,6 +3,4 @@ .pytest_cache .kiauh-env *.code-workspace -klipper_repos.txt -klipper_repos.json - +kiauh.cfg diff --git a/kiauh.cfg.example b/kiauh.cfg.example new file mode 100644 index 0000000..0ba34db --- /dev/null +++ b/kiauh.cfg.example @@ -0,0 +1,12 @@ +[kiauh] +backup_before_update: False + +[klipper] +repository_url: https://github.com/Klipper3d/klipper +branch: master +method: https + +[moonraker] +repository_url: https://github.com/Klipper3d/klipper +branch: master +method: https diff --git a/kiauh/config_manager/__init__.py b/kiauh/config_manager/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/kiauh/config_manager/config_manager.py b/kiauh/config_manager/config_manager.py new file mode 100644 index 0000000..0f80647 --- /dev/null +++ b/kiauh/config_manager/config_manager.py @@ -0,0 +1,61 @@ +#!/usr/bin/env python + +# ======================================================================= # +# Copyright (C) 2020 - 2023 Dominik Willner # +# # +# 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 os +import configparser +from pathlib import Path +from typing import Union + +from kiauh.utils.logger import Logger + + +# noinspection PyMethodMayBeStatic +class ConfigManager: + def __init__(self): + self.config_file = self._get_cfg_location() + self.config = configparser.ConfigParser() + + def read_config(self) -> None: + if not self.config_file: + Logger.print_error("Unable to read config file. File not found.") + return + + self.config.read_file(open(self.config_file, "r")) + + def write_config(self) -> None: + with open(self.config_file, "w") as cfg: + self.config.write(cfg) + + def get_value(self, section: str, key: str) -> Union[str, None]: + if not self.config.has_section(section): + log = f"Section not defined. Unable to read section: [{section}]." + Logger.print_error(log) + return None + + if not self.config.has_option(section, key): + log = f"Option not defined in section [{section}]. Unable to read option: '{key}'." + Logger.print_error(log) + return None + + return self.config.get(section, key) + + def set_value(self, section: str, key: str, value: str): + self.config.set(section, key, value) + + def check_config_exists(self) -> bool: + return True if self._get_cfg_location() else False + + def _get_cfg_location(self) -> str: + current_dir = os.path.dirname(os.path.abspath(__file__)) + project_dir = os.path.dirname(os.path.dirname(current_dir)) + cfg_path = os.path.join(project_dir, "kiauh.cfg") + + return cfg_path if Path(cfg_path).exists() else None diff --git a/kiauh/modules/klipper/klipper_setup.py b/kiauh/modules/klipper/klipper_setup.py index ec9eb81..865d2bf 100644 --- a/kiauh/modules/klipper/klipper_setup.py +++ b/kiauh/modules/klipper/klipper_setup.py @@ -17,6 +17,7 @@ import textwrap from pathlib import Path from typing import Optional, List, Union +from kiauh.config_manager.config_manager import ConfigManager from kiauh.instance_manager.instance_manager import InstanceManager from kiauh.modules.klipper.klipper import Klipper from kiauh.modules.klipper.klipper_utils import ( @@ -118,10 +119,18 @@ def install_klipper(instance_manager: InstanceManager) -> None: def setup_klipper_prerequesites() -> None: - # clone klipper TODO: read branch and url from json to allow forks + cm = ConfigManager() + cm.read_config() + + repo = ( + cm.get_value("klipper", "repository_url") + or "https://github.com/Klipper3D/klipper" + ) + branch = cm.get_value("klipper", "branch") or "master" + repo_manager = RepoManager( - repo="https://github.com/Klipper3D/klipper", - branch="master", + repo=repo, + branch=branch, target_dir=KLIPPER_DIR, ) repo_manager.clone_repo() diff --git a/kiauh/repo_manager/repo_manager.py b/kiauh/repo_manager/repo_manager.py index dfffe31..b1501d2 100644 --- a/kiauh/repo_manager/repo_manager.py +++ b/kiauh/repo_manager/repo_manager.py @@ -9,10 +9,9 @@ # This file may be distributed under the terms of the GNU GPLv3 license # # ======================================================================= # +import os 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 @@ -62,12 +61,11 @@ class RepoManager: 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): + if os.path.exists(self.target_dir): + if not get_confirm("Target directory already exists. Overwrite?"): + Logger.print_info("Skipping re-clone of repository ...") + return shutil.rmtree(self.target_dir) - else: - Logger.print_info("Skipping re-clone of repository ...") - return self._clone() self._checkout() @@ -81,7 +79,7 @@ class RepoManager: def _clone(self): try: - command = ["git", "clone", f"{self.repo}"] + command = ["git", "clone", self.repo, self.target_dir] subprocess.run(command, check=True) Logger.print_ok("Clone successfull!") diff --git a/klipper_repos.txt.example b/klipper_repos.txt.example deleted file mode 100644 index 6cc3393..0000000 --- a/klipper_repos.txt.example +++ /dev/null @@ -1,18 +0,0 @@ -# This file acts as an example file. -# -# 1) Make a copy of this file and rename it to 'klipper_repos.txt' -# 2) Add your custom Klipper repository to the bottom of that copy -# 3) Save the file -# -# Back in KIAUH you can now go into -> [Settings] and use action '2' to set a different Klipper repository -# -# Make sure to always separate the repository and the branch with a ','. -# , -> https://github.com/Klipper3d/klipper,master -# If you omit a branch, it will always default to 'master' -# -# You are allowed to omit the 'https://github.com/' part of the repository URL -# Down below are now a few examples of what is considered as valid: -https://github.com/Klipper3d/klipper,master -https://github.com/Klipper3d/klipper -Klipper3d/klipper,master -Klipper3d/klipper