From d6a7e34ac4a191cc34d43da0b248f709d7f2ade7 Mon Sep 17 00:00:00 2001 From: ItsDrike Date: Thu, 22 Oct 2020 18:05:39 +0200 Subject: [PATCH] Add package base class --- src/util/package.py | 49 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 src/util/package.py diff --git a/src/util/package.py b/src/util/package.py new file mode 100644 index 0000000..2a1ec02 --- /dev/null +++ b/src/util/package.py @@ -0,0 +1,49 @@ +from src.util.install import Install + + +class InvalidPackage(Exception): + pass + + +class PackageAlreadyInstalled(Exception): + pass + + +class Package: + def __init__(self, name: str, aur: bool = False, git: bool = False): + self.name = name + self.aur = aur + self.git = git + + if self.git: + self._resolve_git_package() + + def _resolve_git_package(self) -> None: + """Figure out `git_url` variable from `name`.""" + if "/" not in self.name: + raise InvalidPackage("You need to specify both author and repository name for git packages (f.e. `ItsDrike/dotfiles`)") + + if "http://" in self.name or "https://" in self.name: + self.git_url = self.name + else: + self.git_url = f"https://github.com/{self.name}" + + def install(self) -> None: + if not self.git and Install.is_installed(self.name): + raise PackageAlreadyInstalled(f"Package {self} is already installed") + + if self.aur: + if not Install.is_installed("yay"): + raise InvalidPackage(f"Package {self} can't be installed (missing `yay` - AUR installation software)") + Install.yay_install(self.name) + elif self.git: + Install.git_install(self.git_url) + else: + Install.pacman_install(self.name) + + def __repr__(self) -> str: + if self.git: + return f"" + elif self.aur: + return f"" + return f""