From a733ea45c3e9d9c34a7e8b13bd17af6a6b3b36b6 Mon Sep 17 00:00:00 2001 From: alexAubin <4533074+alexAubin@users.noreply.github.com> Date: Wed, 10 Apr 2024 19:03:03 +0000 Subject: [PATCH 1/5] :art: Format Python code with Black --- src/utils/resources.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/utils/resources.py b/src/utils/resources.py index e5552a106..f6a58c964 100644 --- a/src/utils/resources.py +++ b/src/utils/resources.py @@ -156,7 +156,9 @@ class AppResource: def recursive_apply(function: Callable, data: Any) -> Any: if isinstance(data, dict): # FIXME: hashable? - return {key: recursive_apply(value, function) for key, value in data.items()} + return { + key: recursive_apply(value, function) for key, value in data.items() + } if isinstance(data, list): # FIXME: iterable? return [recursive_apply(value, function) for value in data] From 835303200d0ddb9c66b267704905a37753403903 Mon Sep 17 00:00:00 2001 From: tituspijean Date: Wed, 10 Apr 2024 22:26:58 +0200 Subject: [PATCH 2/5] Fix and enh variables for app resources Co-authored-by: Alexandre Aubin --- src/utils/resources.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/utils/resources.py b/src/utils/resources.py index f6a58c964..7acc84016 100644 --- a/src/utils/resources.py +++ b/src/utils/resources.py @@ -152,26 +152,29 @@ class AppResource: "__APP__": self.app, "__YNH_ARCH__": system_arch(), "__YNH_DEBIAN_VERSION__": debian_version(), + "__YNH_APP_UPSTREAM_VERSION__": manager.wanted["version"].split("~")[0] if manager.wanted else manager.current["version"].split("~")[0], } def recursive_apply(function: Callable, data: Any) -> Any: if isinstance(data, dict): # FIXME: hashable? return { - key: recursive_apply(value, function) for key, value in data.items() + key: recursive_apply(function, value) for key, value in data.items() } if isinstance(data, list): # FIXME: iterable? - return [recursive_apply(value, function) for value in data] + return [recursive_apply(function, value) for value in data] return function(data) def replace_tokens_in_strings(data: Any): if not isinstance(data, str): - return + return data for token, replacement in replacements.items(): data = data.replace(token, replacement) - recursive_apply(replace_tokens_in_strings, properties) + return data + + properties = recursive_apply(replace_tokens_in_strings, properties) for key, value in properties.items(): setattr(self, key, value) From ff6b6954aafbbb91d83110ef408f4164b60ed54f Mon Sep 17 00:00:00 2001 From: tituspijean <8769166+tituspijean@users.noreply.github.com> Date: Wed, 10 Apr 2024 20:32:30 +0000 Subject: [PATCH 3/5] :art: Format Python code with Black --- src/utils/resources.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/utils/resources.py b/src/utils/resources.py index 7acc84016..1e12d67e2 100644 --- a/src/utils/resources.py +++ b/src/utils/resources.py @@ -152,7 +152,11 @@ class AppResource: "__APP__": self.app, "__YNH_ARCH__": system_arch(), "__YNH_DEBIAN_VERSION__": debian_version(), - "__YNH_APP_UPSTREAM_VERSION__": manager.wanted["version"].split("~")[0] if manager.wanted else manager.current["version"].split("~")[0], + "__YNH_APP_UPSTREAM_VERSION__": ( + manager.wanted["version"].split("~")[0] + if manager.wanted + else manager.current["version"].split("~")[0] + ), } def recursive_apply(function: Callable, data: Any) -> Any: From 85f83af86279d2a8819509140f224ef497aab156 Mon Sep 17 00:00:00 2001 From: Alexandre Aubin Date: Wed, 10 Apr 2024 23:08:31 +0200 Subject: [PATCH 4/5] perf: add cache for system utils that fetch debian_version, debian_version_id, system_arch, system_virt --- src/utils/system.py | 30 +++++++++++++++++++++++++----- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/src/utils/system.py b/src/utils/system.py index 6a77e293b..57e7eb8f4 100644 --- a/src/utils/system.py +++ b/src/utils/system.py @@ -29,13 +29,25 @@ YUNOHOST_PACKAGES = ["yunohost", "yunohost-admin", "moulinette", "ssowat"] def debian_version(): - return check_output( - 'grep "^VERSION_CODENAME=" /etc/os-release 2>/dev/null | cut -d= -f2' - ) + if debian_version.cache is None: + debian_version.cache = check_output( + 'grep "^VERSION_CODENAME=" /etc/os-release 2>/dev/null | cut -d= -f2' + ) + return debian_version.cache + + +def debian_version_id(): + if debian_version_id.cache is None: + debian_version_id.cache = check_output( + 'grep "^VERSION_ID=" /etc/os-release 2>/dev/null | cut -d= -f2' + ).strip('"') + return debian_version_id.cache def system_arch(): - return check_output("dpkg --print-architecture 2>/dev/null") + if system_arch.cache is None: + system_arch.cache = check_output("dpkg --print-architecture 2>/dev/null") + return system_arch.cache def system_virt(): @@ -46,7 +58,15 @@ def system_virt(): # Detect virt technology (if not bare metal) and arch # Gotta have this "|| true" because it systemd-detect-virt return 'none' # with an error code on bare metal ~.~ - return check_output("systemd-detect-virt 2>/dev/null || true") + if system_virt.cache is None: + system_virt.cache = check_output("systemd-detect-virt 2>/dev/null || true") + return system_virt.cache + + +debian_version.cache = None +debian_version_id.cache = None +system_arch.cache = None +system_virt.cache = None def free_space_in_directory(dirpath): From a3ab7c9199f8f450034fbf78f752b1e01bf5a7fd Mon Sep 17 00:00:00 2001 From: Alexandre Aubin Date: Thu, 11 Apr 2024 00:38:23 +0200 Subject: [PATCH 5/5] app resources: add __YNH_DEBIAN_VERSION_ID__ in available variable, corresponing to debian number (11, 12, ...) --- src/utils/resources.py | 1 + 1 file changed, 1 insertion(+) diff --git a/src/utils/resources.py b/src/utils/resources.py index 1e12d67e2..7b2bbee96 100644 --- a/src/utils/resources.py +++ b/src/utils/resources.py @@ -152,6 +152,7 @@ class AppResource: "__APP__": self.app, "__YNH_ARCH__": system_arch(), "__YNH_DEBIAN_VERSION__": debian_version(), + "__YNH_DEBIAN_VERSION_ID__": debian_version_id(), "__YNH_APP_UPSTREAM_VERSION__": ( manager.wanted["version"].split("~")[0] if manager.wanted