From 25aaaf2055ae7b04c7e4a0b353b787008238ad62 Mon Sep 17 00:00:00 2001 From: Dimitris Zlatanidis Date: Mon, 1 Apr 2024 16:04:52 +0300 Subject: [PATCH] Added log file for upgrade --- slpkg/configs.py | 1 + slpkg/upgrade.py | 16 +++++++++++++++- tests/test_configs.py | 1 + 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/slpkg/configs.py b/slpkg/configs.py index a121b069..8155f981 100644 --- a/slpkg/configs.py +++ b/slpkg/configs.py @@ -32,6 +32,7 @@ class Configs: deps_log_file: Path = Path(log_path, 'deps.log') slpkg_log_file: Path = Path(log_path, 'slpkg.log') + upgrade_log_file: Path = Path(log_path, 'upgrade.log') file_list_suffix: str = '.pkgs' installpkg: str = 'upgradepkg --install-new' diff --git a/slpkg/upgrade.py b/slpkg/upgrade.py index 1cda4f08..87fede45 100644 --- a/slpkg/upgrade.py +++ b/slpkg/upgrade.py @@ -42,6 +42,10 @@ class Upgrade(Configs): def packages(self) -> Generator: """ Returns the upgradable packages. """ + # Delete log file before starts. + if self.upgrade_log_file.is_file(): + self.upgrade_log_file.unlink() + self.load_installed_packages(self.repository) for inst in self.installed_packages: @@ -60,6 +64,7 @@ class Upgrade(Configs): yield name def is_package_upgradeable(self, installed: str) -> bool: + """ Returns True for upgradeable packages. """ inst_name: str = self.utils.split_package(installed)['name'] if self.data.get(inst_name): repo_version: str = self.data[inst_name]['version'] @@ -72,10 +77,19 @@ class Upgrade(Configs): if parse(repo_version) == parse(inst_version) and int(repo_build) > int(inst_build): return True - except InvalidVersion: + except InvalidVersion as err: + self._write_log_file(installed, inst_name, err) return False return False + def _write_log_file(self, installed: str, name: str, err: InvalidVersion) -> None: + """ Writes a log file for invalid versions. """ + if self.log_path.is_dir(): + with self.upgrade_log_file.open('a') as log: + log.write(f"Installed: {installed}, " + f"Repository: {self.data[name]['package']}, " + f"Error: {err}\n") + def check_packages(self) -> None: repo_data: dict = {} found_packages: dict = {} diff --git a/tests/test_configs.py b/tests/test_configs.py index 23050bd6..2f30feac 100644 --- a/tests/test_configs.py +++ b/tests/test_configs.py @@ -22,6 +22,7 @@ class TestConfigs(unittest.TestCase): self.assertEqual(Path('/var/log/slpkg/deps.log'), self.configs.deps_log_file) self.assertEqual(Path('/var/log/slpkg/slpkg.log'), self.configs.slpkg_log_file) + self.assertEqual(Path('/var/log/slpkg/upgrade.log'), self.configs.upgrade_log_file) self.assertEqual('.pkgs', self.configs.file_list_suffix) self.assertEqual('upgradepkg --install-new', self.configs.installpkg)