Try to remove as many app_list() call as possible, replace them with a simple _installed_apps()

This commit is contained in:
Alexandre Aubin 2019-11-25 19:07:52 +01:00
parent 67bb386c39
commit 97138e9209
3 changed files with 24 additions and 33 deletions

View file

@ -90,7 +90,7 @@ def app_list(filter=None, raw=False, installed=False, with_backup=False):
app_dict = _load_apps_catalog() app_dict = _load_apps_catalog()
# Get app list from the app settings directory # Get app list from the app settings directory
for app in os.listdir(APPS_SETTING_PATH): for app in _installed_apps():
if app not in app_dict: if app not in app_dict:
# Handle multi-instance case like wordpress__2 # Handle multi-instance case like wordpress__2
if '__' in app: if '__' in app:
@ -452,19 +452,12 @@ def app_upgrade(app=[], url=None, file=None):
from yunohost.hook import hook_add, hook_remove, hook_exec, hook_callback from yunohost.hook import hook_add, hook_remove, hook_exec, hook_callback
from yunohost.permission import permission_sync_to_user from yunohost.permission import permission_sync_to_user
try:
app_list()
except YunohostError:
raise YunohostError('apps_already_up_to_date')
not_upgraded_apps = []
apps = app apps = app
# If no app is specified, upgrade all apps # If no app is specified, upgrade all apps
if not apps: if not apps:
# FIXME : not sure what's supposed to happen if there is a url and a file but no apps... # FIXME : not sure what's supposed to happen if there is a url and a file but no apps...
if not url and not file: if not url and not file:
apps = [app_["id"] for app_ in app_list(installed=True)["apps"]] apps = _installed_apps()
elif not isinstance(app, list): elif not isinstance(app, list):
apps = [app] apps = [app]
@ -668,7 +661,10 @@ def app_install(operation_logger, app, label=None, args=None, no_remove_on_failu
# If we got an url like "https://github.com/foo/bar_ynh, we want to # If we got an url like "https://github.com/foo/bar_ynh, we want to
# extract "bar" and test if we know this app # extract "bar" and test if we know this app
elif ('http://' in app) or ('https://' in app): elif ('http://' in app) or ('https://' in app):
app_name_to_test = app.strip("/").split("/")[-1].replace("_ynh","") app_name_to_test = app.strip("/").split("/")[-1].replace("_ynh", "")
else:
# FIXME : watdo if '@' in app ?
app_name_to_test = None
if app_name_to_test in raw_app_list: if app_name_to_test in raw_app_list:
@ -1216,8 +1212,7 @@ def app_register_url(app, domain, path):
# We cannot change the url of an app already installed simply by changing # We cannot change the url of an app already installed simply by changing
# the settings... # the settings...
installed = app in app_list(installed=True, raw=True).keys() if _is_installed(app):
if installed:
settings = _get_app_settings(app) settings = _get_app_settings(app)
if "path" in settings.keys() and "domain" in settings.keys(): if "path" in settings.keys() and "domain" in settings.keys():
raise YunohostError('app_already_installed_cant_change_url') raise YunohostError('app_already_installed_cant_change_url')
@ -1263,19 +1258,13 @@ def app_ssowatconf():
redirected_regex = {main_domain + '/yunohost[\/]?$': 'https://' + main_domain + '/yunohost/sso/'} redirected_regex = {main_domain + '/yunohost[\/]?$': 'https://' + main_domain + '/yunohost/sso/'}
redirected_urls = {} redirected_urls = {}
try:
apps_list = app_list(installed=True)['apps']
except Exception as e:
logger.debug("cannot get installed app list because %s", e)
apps_list = []
def _get_setting(settings, name): def _get_setting(settings, name):
s = settings.get(name, None) s = settings.get(name, None)
return s.split(',') if s else [] return s.split(',') if s else []
for app in apps_list: for app in _installed_apps():
app_settings = read_yaml(APPS_SETTING_PATH + app['id'] + '/settings.yml') app_settings = read_yaml(APPS_SETTING_PATH + app + '/settings.yml')
if 'domain' not in app_settings: if 'domain' not in app_settings:
continue continue
@ -1622,8 +1611,7 @@ def _get_all_installed_apps_id():
* ...' * ...'
""" """
all_apps_ids = [x["id"] for x in app_list(installed=True)["apps"]] all_apps_ids = sorted(_installed_apps())
all_apps_ids = sorted(all_apps_ids)
all_apps_ids_formatted = "\n * ".join(all_apps_ids) all_apps_ids_formatted = "\n * ".join(all_apps_ids)
all_apps_ids_formatted = "\n * " + all_apps_ids_formatted all_apps_ids_formatted = "\n * " + all_apps_ids_formatted
@ -2269,6 +2257,8 @@ def _is_installed(app):
""" """
return os.path.isdir(APPS_SETTING_PATH + app) return os.path.isdir(APPS_SETTING_PATH + app)
def _installed_apps():
return os.listdir(APPS_SETTING_PATH)
def _value_for_locale(values): def _value_for_locale(values):
""" """

View file

@ -8,7 +8,7 @@ from moulinette.utils.filesystem import read_yaml
from yunohost.tools import Migration from yunohost.tools import Migration
from yunohost.user import user_list, user_group_create, user_group_update from yunohost.user import user_list, user_group_create, user_group_update
from yunohost.app import app_setting, app_list from yunohost.app import app_setting, _installed_apps
from yunohost.regenconf import regen_conf, BACKUP_CONF_DIR from yunohost.regenconf import regen_conf, BACKUP_CONF_DIR
from yunohost.permission import permission_create, user_permission_update, permission_sync_to_user from yunohost.permission import permission_create, user_permission_update, permission_sync_to_user
@ -96,13 +96,16 @@ class MyMigration(Migration):
def migrate_app_permission(self, app=None): def migrate_app_permission(self, app=None):
logger.info(m18n.n("migration_0011_migrate_permission")) logger.info(m18n.n("migration_0011_migrate_permission"))
if app: apps = _installed_apps()
apps = app_list(installed=True, filter=app)['apps']
else:
apps = app_list(installed=True)['apps']
for app_info in apps: if app:
app = app_info['id'] if app not in apps:
logger.error("Can't migrate permission for app %s because it ain't installed..." % app)
apps = []
else:
apps = [app]
for app in apps:
permission = app_setting(app, 'allowed_users') permission = app_setting(app, 'allowed_users')
path = app_setting(app, 'path') path = app_setting(app, 'path')
domain = app_setting(app, 'domain') domain = app_setting(app, 'domain')

View file

@ -3,7 +3,7 @@ import pytest
from conftest import message, raiseYunohostError from conftest import message, raiseYunohostError
from yunohost.app import app_install, app_remove, app_change_url, app_list, app_map from yunohost.app import app_install, app_remove, app_change_url, app_list, app_map, _installed_apps
from yunohost.user import user_list, user_create, user_delete, \ from yunohost.user import user_list, user_create, user_delete, \
user_group_list, user_group_delete user_group_list, user_group_delete
from yunohost.permission import user_permission_update, user_permission_list, user_permission_reset, \ from yunohost.permission import user_permission_update, user_permission_list, user_permission_reset, \
@ -163,9 +163,7 @@ def check_permission_for_apps():
app_perms_prefix = set(p.split(".")[0] for p in app_perms) app_perms_prefix = set(p.split(".")[0] for p in app_perms)
installed_apps = {app['id'] for app in app_list(installed=True)['apps']} assert set(_installed_apps()) == app_perms_prefix
assert installed_apps == app_perms_prefix
def can_access_webpage(webpath, logged_as=None): def can_access_webpage(webpath, logged_as=None):