From 6a01b657da12bc609610140d077f08cf2cba2941 Mon Sep 17 00:00:00 2001 From: Alexandre Aubin Date: Sat, 27 Jan 2024 15:51:56 +0100 Subject: [PATCH] appscatalog: this 'init' step is overengineered ... let's instead say the nominal case is that there's no apps_catalog.yml defined, and in this case we use the default catalog --- locales/en.json | 1 - src/app_catalog.py | 29 +++++++---------------- src/tests/test_app_catalog.py | 44 +---------------------------------- src/tools.py | 5 +--- 4 files changed, 11 insertions(+), 68 deletions(-) diff --git a/locales/en.json b/locales/en.json index dcd5e4581..4628cb29f 100644 --- a/locales/en.json +++ b/locales/en.json @@ -76,7 +76,6 @@ "app_yunohost_version_not_supported": "This app requires YunoHost >= {required} but current installed version is {current}", "apps_already_up_to_date": "All apps are already up-to-date", "apps_catalog_failed_to_download": "Unable to download the {apps_catalog} app catalog: {error}", - "apps_catalog_init_success": "App catalog system initialized!", "apps_catalog_obsolete_cache": "The app catalog cache is empty or obsolete.", "apps_catalog_update_success": "The application catalog has been updated!", "apps_catalog_updating": "Updating application catalog...", diff --git a/src/app_catalog.py b/src/app_catalog.py index 5c15d696e..d6dacb879 100644 --- a/src/app_catalog.py +++ b/src/app_catalog.py @@ -41,6 +41,7 @@ APPS_CATALOG_LOGOS = "/usr/share/yunohost/applogos" APPS_CATALOG_CONF = "/etc/yunohost/apps_catalog.yml" APPS_CATALOG_API_VERSION = 3 APPS_CATALOG_DEFAULT_URL = "https://app.yunohost.org/default" +DEFAULT_APPS_CATALOG_LIST = [{"id": "default", "url": APPS_CATALOG_DEFAULT_URL}] def app_catalog(full=False, with_categories=False, with_antifeatures=False): @@ -120,33 +121,21 @@ def app_search(string): return matching_apps -def _initialize_apps_catalog_system(): - """ - This function is meant to intialize the apps_catalog system with YunoHost's default app catalog. - """ - - default_apps_catalog_list = [{"id": "default", "url": APPS_CATALOG_DEFAULT_URL}] - - try: - logger.debug( - "Initializing apps catalog system with YunoHost's default app list" - ) - write_to_yaml(APPS_CATALOG_CONF, default_apps_catalog_list) - except Exception as e: - raise YunohostError( - f"Could not initialize the apps catalog system... : {e}", raw_msg=True - ) - - logger.success(m18n.n("apps_catalog_init_success")) - - def _read_apps_catalog_list(): """ Read the json corresponding to the list of apps catalogs """ + if not os.path.exists(APPS_CATALOG_CONF): + return DEFAULT_APPS_CATALOG_LIST + try: list_ = read_yaml(APPS_CATALOG_CONF) + if list_ == DEFAULT_APPS_CATALOG_LIST: + try: + os.remove(APPS_CATALOG_CONF) + except Exception: + pass # Support the case where file exists but is empty # by returning [] if list_ is None return list_ if list_ else [] diff --git a/src/tests/test_app_catalog.py b/src/tests/test_app_catalog.py index 40daf5873..a2482d8b5 100644 --- a/src/tests/test_app_catalog.py +++ b/src/tests/test_app_catalog.py @@ -12,7 +12,6 @@ from moulinette.utils.filesystem import read_json, write_to_json, write_to_yaml from yunohost.utils.error import YunohostError from yunohost.app_catalog import ( - _initialize_apps_catalog_system, _read_apps_catalog_list, _update_apps_catalog, _actual_apps_catalog_api_url, @@ -66,44 +65,17 @@ def teardown_function(function): # -def test_apps_catalog_init(mocker): - # Cache is empty - assert not glob.glob(APPS_CATALOG_CACHE + "/*") - # Conf doesn't exist yet - assert not os.path.exists(APPS_CATALOG_CONF) - - # Initialize ... - mocker.spy(m18n, "n") - _initialize_apps_catalog_system() - m18n.n.assert_any_call("apps_catalog_init_success") - - # And a conf with at least one list - assert os.path.exists(APPS_CATALOG_CONF) - apps_catalog_list = _read_apps_catalog_list() - assert len(apps_catalog_list) - - # Cache is expected to still be empty though - # (if we did update the apps_catalog during init, - # we couldn't differentiate easily exceptions - # related to lack of network connectivity) - assert not glob.glob(APPS_CATALOG_CACHE + "/*") - - def test_apps_catalog_emptylist(): - # Initialize ... - _initialize_apps_catalog_system() # Let's imagine somebody removed the default apps catalog because uh idk they dont want to use our default apps catalog os.system("rm %s" % APPS_CATALOG_CONF) os.system("touch %s" % APPS_CATALOG_CONF) apps_catalog_list = _read_apps_catalog_list() - assert not len(apps_catalog_list) + assert len(apps_catalog_list) def test_apps_catalog_update_nominal(mocker): - # Initialize ... - _initialize_apps_catalog_system() # Cache is empty assert not glob.glob(APPS_CATALOG_CACHE + "/*") @@ -135,8 +107,6 @@ def test_apps_catalog_update_nominal(mocker): def test_apps_catalog_update_404(mocker): - # Initialize ... - _initialize_apps_catalog_system() with requests_mock.Mocker() as m: # 404 error @@ -149,8 +119,6 @@ def test_apps_catalog_update_404(mocker): def test_apps_catalog_update_timeout(mocker): - # Initialize ... - _initialize_apps_catalog_system() with requests_mock.Mocker() as m: # Timeout @@ -165,8 +133,6 @@ def test_apps_catalog_update_timeout(mocker): def test_apps_catalog_update_sslerror(mocker): - # Initialize ... - _initialize_apps_catalog_system() with requests_mock.Mocker() as m: # SSL error @@ -181,8 +147,6 @@ def test_apps_catalog_update_sslerror(mocker): def test_apps_catalog_update_corrupted(mocker): - # Initialize ... - _initialize_apps_catalog_system() with requests_mock.Mocker() as m: # Corrupted json @@ -197,8 +161,6 @@ def test_apps_catalog_update_corrupted(mocker): def test_apps_catalog_load_with_empty_cache(mocker): - # Initialize ... - _initialize_apps_catalog_system() # Cache is empty assert not glob.glob(APPS_CATALOG_CACHE + "/*") @@ -223,8 +185,6 @@ def test_apps_catalog_load_with_empty_cache(mocker): def test_apps_catalog_load_with_conflicts_between_lists(mocker): - # Initialize ... - _initialize_apps_catalog_system() conf = [ {"id": "default", "url": APPS_CATALOG_DEFAULT_URL}, @@ -261,8 +221,6 @@ def test_apps_catalog_load_with_conflicts_between_lists(mocker): def test_apps_catalog_load_with_outdated_api_version(): - # Initialize ... - _initialize_apps_catalog_system() # Update with requests_mock.Mocker() as m: diff --git a/src/tools.py b/src/tools.py index b7b849bc5..6d07f25fb 100644 --- a/src/tools.py +++ b/src/tools.py @@ -253,10 +253,7 @@ def tools_postinstall( # Enable UPnP silently and reload firewall firewall_upnp("enable", no_refresh=True) - # Initialize the apps catalog system - _initialize_apps_catalog_system() - - # Try to update the apps catalog ... + # Try to fetch the apps catalog ... # we don't fail miserably if this fails, # because that could be for example an offline installation... try: