diff --git a/mobros/constants.py b/mobros/constants.py index 4d16851..4cdd1ad 100644 --- a/mobros/constants.py +++ b/mobros/constants.py @@ -59,4 +59,4 @@ class Commands(Enum): MOBROS_CONFIG_PATH = "/etc/mobros/config" MOBROS_CONFIG_SECTION = "conflict-solving" -MOBROS_CONFIG_BLACKLIST_KEY = "blacklistSource" \ No newline at end of file +MOBROS_CONFIG_BLACKLIST_KEY = "blacklistSource" diff --git a/mobros/types/apt_cache_singleton.py b/mobros/types/apt_cache_singleton.py index 2ac7051..2bd9839 100644 --- a/mobros/types/apt_cache_singleton.py +++ b/mobros/types/apt_cache_singleton.py @@ -1,10 +1,12 @@ """Module defining the apt cache singleton not to be constantly requesting apt for his cache""" +import os import apt import mobros.utils.logger as logging from mobros.exceptions import AptCacheInitializationException from mobros.utils.utilitary import execute_shell_command + # pylint: disable=R0903,W0107 class AptCache: """Apt cache singleton""" @@ -31,7 +33,10 @@ def __new__(cls): message = "Unable to fetch apt cache. Please check your internet connection! Try running 'sudo apt update' for more info." logging.error(message) - execute_shell_command("sudo apt update", log_output=True) + apt_cmd = ["apt", "update"] + if os.geteuid() != 0: + apt_cmd = ["sudo"] + apt_cmd + execute_shell_command(apt_cmd, log_output=True) raise AptCacheInitializationException(message) from fetched_failed_exception cls._installed_cache = [] diff --git a/tests/test_utils/test_utilitary.py b/tests/test_utils/test_utilitary.py index c1f7eb7..257c31c 100644 --- a/tests/test_utils/test_utilitary.py +++ b/tests/test_utils/test_utilitary.py @@ -1,5 +1,5 @@ import unittest -from os import remove +from os import remove, geteuid from os.path import dirname, exists, realpath import mock @@ -47,6 +47,15 @@ def test_execute_bash_fail_file_not_found(self): except Exception: pass + def test_execute_apt_update(self): + + + apt_cmd = ["apt", "update"] + if geteuid() != 0: + apt_cmd = ["sudo"] + apt_cmd + execute_shell_command(apt_cmd, log_output=True) + + def test_is_blacklisted_origin(self): GlobalData().set_conflict_solving_blacklist(["*.mov.ai/repository/ppa-testing","*.mov.ai/repository/ppa-main"])