From 2b7f1d8bf3f0556e3602abf6307eb4510880c5fd Mon Sep 17 00:00:00 2001 From: adator <85586985+adator85@users.noreply.github.com> Date: Thu, 3 Oct 2024 23:56:01 +0200 Subject: [PATCH 1/6] V5.3.7 Defender can update packages --- core/base.py | 9 ++++- core/installation.py | 78 ++++++++++++++++++++++++++++++++++++++++++-- version.json | 8 ++++- 3 files changed, 91 insertions(+), 4 deletions(-) diff --git a/core/base.py b/core/base.py index 776a253..e0e4674 100644 --- a/core/base.py +++ b/core/base.py @@ -48,7 +48,6 @@ def __set_current_defender_version(self) -> None: with open(version_filename, 'r') as version_data: current_version:dict[str, str] = json.load(version_data) - # self.DEFENDER_VERSION = current_version["version"] self.Config.current_version = current_version['version'] return None @@ -81,6 +80,14 @@ def __get_latest_defender_version(self) -> None: self.logs.warning(f'Github not available to fetch latest version') def check_for_new_version(self, online:bool) -> bool: + """Check if there is a new version available + + Args: + online (bool): True if you want to get the version from github (main branch) + + Returns: + bool: True if there is a new version available + """ try: self.logs.debug(f'Checking for a new service version') diff --git a/core/installation.py b/core/installation.py index ec7400b..21ad421 100644 --- a/core/installation.py +++ b/core/installation.py @@ -1,7 +1,8 @@ import os +import json from sys import exit from dataclasses import dataclass -from subprocess import check_call, run, CalledProcessError, PIPE +from subprocess import check_call, run, CalledProcessError, PIPE, check_output from platform import python_version, python_version_tuple class Install: @@ -24,6 +25,13 @@ class CoreConfig: venv_pip_executable: str venv_python_executable: str + @dataclass + class Package: + name: str = None + version: str = None + + DB_PACKAGES: list[Package] = [] + def __init__(self) -> None: self.set_configuration() @@ -73,9 +81,11 @@ def set_configuration(self): # If the Python version is not good then Exit exit("/!\\ Python version error /!\\") + self.check_packages_version() + if not os.path.exists(os.path.join(self.config.defender_install_folder, 'core', 'configuration.json')): # If configuration file do not exist - exit("/!\\ Configuration file (configuration.json) doesn't exist /!\\") + exit("/!\\ Configuration file (core/configuration.json) doesn't exist /!\\") # Exclude Windows OS from the installation if os.name == 'nt': @@ -123,6 +133,70 @@ def run_subprocess(self, command:list) -> None: print(f"Try to install dependencies ...") exit(5) + def get_packages_version_from_json(self) -> None: + """This will create Package model with package names and required version + """ + try: + + version_filename = f'.{os.sep}version.json' + with open(version_filename, 'r') as version_data: + package_info:dict[str, str] = json.load(version_data) + + for name, version in package_info.items(): + if name == 'version': + continue + + self.DB_PACKAGES.append( + self.Package(name=name, version=version) + ) + + return None + except FileNotFoundError as fe: + print(f"File not found: {fe}") + except Exception as err: + print(f"General Error: {err}") + + def check_packages_version(self) -> bool: + + try: + newVersion = False + self.get_packages_version_from_json() + print(f"> Checking for dependencies versions ==> WAIT") + for package in self.DB_PACKAGES: + newVersion = False + required_version = package.version + installed_version = None + + output = check_output(['pip', 'show', package.name]) + for line in output.decode().splitlines(): + if line.startswith('Version:'): + installed_version = line.split(':')[1].strip() + break + + required_major, required_minor, required_patch = required_version.split('.') + installed_major, installed_minor, installed_patch = installed_version.split('.') + + if required_major > installed_major: + print(f'> New version of {package.name} is available {installed_version} ==> {required_version}') + newVersion = True + elif required_major == installed_major and required_minor > installed_minor: + print(f'> New version of {package.name} is available {installed_version} ==> {required_version}') + newVersion = True + elif required_major == installed_major and required_minor == installed_minor and required_patch > installed_patch: + print(f'> New version of {package.name} is available {installed_version} ==> {required_version}') + newVersion = True + + if newVersion: + self.run_subprocess(['pip', 'install', '--upgrade', package.name]) + + print(f"> Dependencies versions ==> OK") + return newVersion + + except CalledProcessError: + print(f"/!\\ Package {package.name} not installed /!\\") + except Exception as err: + print(f"General Error: {err}") + def check_python_version(self) -> bool: """Test si la version de python est autorisée ou non diff --git a/version.json b/version.json index 0e29c03..a71d6c7 100644 --- a/version.json +++ b/version.json @@ -1,3 +1,9 @@ { - "version": "5.3.6" + "version": "5.3.7", + + "requests": "2.32.3", + "psutil": "6.0.0", + "unrealircd_rpc_py": "1.0.4", + "sqlalchemy": "2.0.35", + "faker": "30.1.0" } \ No newline at end of file From ad5b7ffbf23b87b09d16d0d3548594ff9070c004 Mon Sep 17 00:00:00 2001 From: adator <85586985+adator85@users.noreply.github.com> Date: Fri, 4 Oct 2024 00:08:14 +0200 Subject: [PATCH 2/6] Check if virtual env is available --- core/installation.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/core/installation.py b/core/installation.py index 21ad421..e972253 100644 --- a/core/installation.py +++ b/core/installation.py @@ -1,6 +1,6 @@ import os import json -from sys import exit +from sys import exit, executable, prefix, exec_prefix from dataclasses import dataclass from subprocess import check_call, run, CalledProcessError, PIPE, check_output from platform import python_version, python_version_tuple @@ -161,6 +161,11 @@ def check_packages_version(self) -> bool: try: newVersion = False self.get_packages_version_from_json() + + if not self.config.venv_folder in prefix: + print(f"You are probably running a new installation or you are not using your virtual env {self.config.venv_folder}") + return newVersion + print(f"> Checking for dependencies versions ==> WAIT") for package in self.DB_PACKAGES: newVersion = False From fd88df1017f6fd703db1d31d10f3b6d4dea02b38 Mon Sep 17 00:00:00 2001 From: adator <85586985+adator85@users.noreply.github.com> Date: Fri, 4 Oct 2024 00:22:50 +0200 Subject: [PATCH 3/6] Exec pip from virtual env --- core/installation.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/installation.py b/core/installation.py index e972253..1dd9047 100644 --- a/core/installation.py +++ b/core/installation.py @@ -192,7 +192,7 @@ def check_packages_version(self) -> bool: newVersion = True if newVersion: - self.run_subprocess(['pip', 'install', '--upgrade', package.name]) + self.run_subprocess([self.config.venv_pip_executable, 'install', '--upgrade', package.name]) print(f"> Dependencies versions ==> OK") return newVersion From 6801c981ab7a255d8777b0fefcdb867e1e7f0955 Mon Sep 17 00:00:00 2001 From: adator <85586985+adator85@users.noreply.github.com> Date: Fri, 4 Oct 2024 00:54:31 +0200 Subject: [PATCH 4/6] V5.3.7 --- core/installation.py | 2 +- core/irc.py | 10 ++++++---- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/core/installation.py b/core/installation.py index 1dd9047..564b963 100644 --- a/core/installation.py +++ b/core/installation.py @@ -172,7 +172,7 @@ def check_packages_version(self) -> bool: required_version = package.version installed_version = None - output = check_output(['pip', 'show', package.name]) + output = check_output([self.config.venv_pip_executable, 'show', package.name]) for line in output.decode().splitlines(): if line.startswith('Version:'): installed_version = line.split(':')[1].strip() diff --git a/core/irc.py b/core/irc.py index f671530..91a0ec6 100644 --- a/core/irc.py +++ b/core/irc.py @@ -95,13 +95,15 @@ def __create_socket(self) -> None: return None except ssl.SSLEOFError as soe: - self.Base.logs.critical(f"SSLEOFError __create_socket: {soe} - {soc.fileno()}") + self.Base.logs.critical(f"SSLEOFError: {soe} - {soc.fileno()}") except ssl.SSLError as se: - self.Base.logs.critical(f"SSLError __create_socket: {se} - {soc.fileno()}") + self.Base.logs.critical(f"SSLError: {se} - {soc.fileno()}") except OSError as oe: - self.Base.logs.critical(f"OSError __create_socket: {oe} - {soc.fileno()}") + self.Base.logs.critical(f"OSError: {oe} - {soc.fileno()}") + if 'connection refused' in str(oe).lower(): + sys.exit(oe) except AttributeError as ae: - self.Base.logs.critical(f"AttributeError __create_socket: {ae} - {soc.fileno()}") + self.Base.logs.critical(f"AttributeError: {ae} - {soc.fileno()}") def __ssl_context(self) -> ssl.SSLContext: ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT) From 2be6ece27fd44dc23fe678f582b3cb66261d02a2 Mon Sep 17 00:00:00 2001 From: adator <85586985+adator85@users.noreply.github.com> Date: Fri, 4 Oct 2024 01:03:36 +0200 Subject: [PATCH 5/6] Exclude windows from update packages --- core/installation.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/core/installation.py b/core/installation.py index 564b963..ac27161 100644 --- a/core/installation.py +++ b/core/installation.py @@ -1,6 +1,6 @@ import os import json -from sys import exit, executable, prefix, exec_prefix +from sys import exit, prefix from dataclasses import dataclass from subprocess import check_call, run, CalledProcessError, PIPE, check_output from platform import python_version, python_version_tuple @@ -42,6 +42,8 @@ def __init__(self) -> None: # Sinon tester les dependances python et les installer avec pip if self.do_install(): + self.check_packages_version() + self.install_dependencies() self.create_service_file() @@ -81,8 +83,6 @@ def set_configuration(self): # If the Python version is not good then Exit exit("/!\\ Python version error /!\\") - self.check_packages_version() - if not os.path.exists(os.path.join(self.config.defender_install_folder, 'core', 'configuration.json')): # If configuration file do not exist exit("/!\\ Configuration file (core/configuration.json) doesn't exist /!\\") From f0c0a2d06a7ebe3eea7181456f735c3cadf1645a Mon Sep 17 00:00:00 2001 From: adator <85586985+adator85@users.noreply.github.com> Date: Fri, 4 Oct 2024 01:07:37 +0200 Subject: [PATCH 6/6] check packages versions just after windows check --- core/installation.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/core/installation.py b/core/installation.py index ac27161..6a0bcc3 100644 --- a/core/installation.py +++ b/core/installation.py @@ -39,11 +39,11 @@ def __init__(self) -> None: if self.skip_install: return None + self.check_packages_version() + # Sinon tester les dependances python et les installer avec pip if self.do_install(): - self.check_packages_version() - self.install_dependencies() self.create_service_file()