From 85f83af86279d2a8819509140f224ef497aab156 Mon Sep 17 00:00:00 2001 From: Alexandre Aubin Date: Wed, 10 Apr 2024 23:08:31 +0200 Subject: [PATCH] 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):