From de149975d2c235fe29beba1eee4108d5c9f5532d Mon Sep 17 00:00:00 2001 From: Kay0u Date: Wed, 29 Dec 2021 22:40:54 +0100 Subject: [PATCH 1/5] Remove unused variable --- data/hooks/diagnosis/12-dnsrecords.py | 1 - 1 file changed, 1 deletion(-) diff --git a/data/hooks/diagnosis/12-dnsrecords.py b/data/hooks/diagnosis/12-dnsrecords.py index cedd3891e..554576f79 100644 --- a/data/hooks/diagnosis/12-dnsrecords.py +++ b/data/hooks/diagnosis/12-dnsrecords.py @@ -56,7 +56,6 @@ class DNSRecordsDiagnoser(Diagnoser): def check_domain(self, domain, is_main_domain): if is_special_use_tld(domain): - categories = [] yield dict( meta={"domain": domain}, data={}, From f92c88aff7abdd9812a32592e0dd7fa052937b58 Mon Sep 17 00:00:00 2001 From: Kay0u Date: Wed, 29 Dec 2021 22:41:57 +0100 Subject: [PATCH 2/5] remove unused lines --- src/yunohost/app.py | 1 - src/yunohost/dns.py | 3 +-- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/src/yunohost/app.py b/src/yunohost/app.py index d5e57d6cf..bf41f163d 100644 --- a/src/yunohost/app.py +++ b/src/yunohost/app.py @@ -1033,7 +1033,6 @@ def app_remove(operation_logger, app, purge=False): remove_script = f"{tmp_workdir_for_app}/scripts/remove" env_dict = {} - app_id, app_instance_nb = _parse_app_instance_name(app) env_dict = _make_environment_for_app_script(app, workdir=tmp_workdir_for_app) env_dict["YNH_APP_PURGE"] = str(1 if purge else 0) diff --git a/src/yunohost/dns.py b/src/yunohost/dns.py index 23c641a35..492b285c0 100644 --- a/src/yunohost/dns.py +++ b/src/yunohost/dns.py @@ -857,7 +857,6 @@ def domain_dns_push(operation_logger, domain, dry_run=False, force=False, purge= ignored = "" if action == "create": - old_content = record.get("old_content", "(None)")[:30] new_content = record.get("content", "(None)")[:30] return f"{name:>20} [{t:^5}] {new_content:^30} {ignored}" elif action == "update": @@ -867,7 +866,7 @@ def domain_dns_push(operation_logger, domain, dry_run=False, force=False, purge= f"{name:>20} [{t:^5}] {old_content:^30} -> {new_content:^30} {ignored}" ) elif action == "unchanged": - old_content = new_content = record.get("content", "(None)")[:30] + old_content = record.get("content", "(None)")[:30] return f"{name:>20} [{t:^5}] {old_content:^30}" else: old_content = record.get("content", "(None)")[:30] From 2cc09aca91c97ae2bfbda9d224a93cd422b206ae Mon Sep 17 00:00:00 2001 From: Kay0u Date: Wed, 29 Dec 2021 22:45:25 +0100 Subject: [PATCH 3/5] some improvements --- .../0016_php70_to_php73_pools.py | 12 +++---- src/yunohost/service.py | 2 +- src/yunohost/tools.py | 34 ++++++++----------- 3 files changed, 22 insertions(+), 26 deletions(-) diff --git a/src/yunohost/data_migrations/0016_php70_to_php73_pools.py b/src/yunohost/data_migrations/0016_php70_to_php73_pools.py index fed96c9c8..570e63ea4 100644 --- a/src/yunohost/data_migrations/0016_php70_to_php73_pools.py +++ b/src/yunohost/data_migrations/0016_php70_to_php73_pools.py @@ -38,11 +38,11 @@ class MyMigration(Migration): # Ignore the "www.conf" (default stuff, probably don't want to touch it ?) php70_pool_files = [f for f in php70_pool_files if f != "www.conf"] - for f in php70_pool_files: + for pf in php70_pool_files: # Copy the files to the php7.3 pool - src = "{}/{}".format(PHP70_POOLS, f) - dest = "{}/{}".format(PHP73_POOLS, f) + src = "{}/{}".format(PHP70_POOLS, pf) + dest = "{}/{}".format(PHP73_POOLS, pf) copy2(src, dest) # Replace the socket prefix if it's found @@ -56,17 +56,17 @@ class MyMigration(Migration): c = "sed -i '1i {}' {}".format(MIGRATION_COMMENT, dest) os.system(c) - app_id = os.path.basename(f)[: -len(".conf")] + app_id = os.path.basename(pf)[: -len(".conf")] if _is_installed(app_id): _patch_legacy_php_versions_in_settings( "/etc/yunohost/apps/%s/" % app_id ) nginx_conf_files = glob.glob("/etc/nginx/conf.d/*.d/%s.conf" % app_id) - for f in nginx_conf_files: + for nf in nginx_conf_files: # Replace the socket prefix if it's found c = "sed -i -e 's@{}@{}@g' {}".format( - PHP70_SOCKETS_PREFIX, PHP73_SOCKETS_PREFIX, f + PHP70_SOCKETS_PREFIX, PHP73_SOCKETS_PREFIX, nf ) os.system(c) diff --git a/src/yunohost/service.py b/src/yunohost/service.py index 4f453dfe9..8a7b146b1 100644 --- a/src/yunohost/service.py +++ b/src/yunohost/service.py @@ -587,7 +587,7 @@ def service_regen_conf( if name not in services.keys(): raise YunohostValidationError("service_unknown", service=name) - if names is []: + if names == []: names = list(services.keys()) logger.warning(m18n.n("service_regen_conf_is_deprecated")) diff --git a/src/yunohost/tools.py b/src/yunohost/tools.py index 679b4d16e..a0411c69e 100644 --- a/src/yunohost/tools.py +++ b/src/yunohost/tools.py @@ -236,28 +236,24 @@ def tools_postinstall( # If this is a nohost.me/noho.st, actually check for availability if not ignore_dyndns and is_yunohost_dyndns_domain(domain): - # (Except if the user explicitly said he/she doesn't care about dyndns) - if ignore_dyndns: - dyndns = False # Check if the domain is available... - else: - try: - available = _dyndns_available(domain) - # If an exception is thrown, most likely we don't have internet - # connectivity or something. Assume that this domain isn't manageable - # and inform the user that we could not contact the dyndns host server. - except Exception: - logger.warning( - m18n.n( - "dyndns_provider_unreachable", provider="dyndns.yunohost.org" - ) + try: + available = _dyndns_available(domain) + # If an exception is thrown, most likely we don't have internet + # connectivity or something. Assume that this domain isn't manageable + # and inform the user that we could not contact the dyndns host server. + except Exception: + logger.warning( + m18n.n( + "dyndns_provider_unreachable", provider="dyndns.yunohost.org" ) + ) - if available: - dyndns = True - # If not, abort the postinstall - else: - raise YunohostValidationError("dyndns_unavailable", domain=domain) + if available: + dyndns = True + # If not, abort the postinstall + else: + raise YunohostValidationError("dyndns_unavailable", domain=domain) else: dyndns = False From bbe0486935615779925b4a11ba239395a095f33d Mon Sep 17 00:00:00 2001 From: Kay0u Date: Thu, 30 Dec 2021 00:08:49 +0100 Subject: [PATCH 4/5] Trying to fix some lgtm warnings --- src/yunohost/app.py | 7 ++- src/yunohost/certificate.py | 52 +++++++++---------- .../data_migrations/0018_xtable_to_nftable.py | 6 ++- src/yunohost/domain.py | 20 +++---- src/yunohost/regenconf.py | 5 +- src/yunohost/tools.py | 2 +- src/yunohost/user.py | 24 ++++----- src/yunohost/utils/config.py | 5 +- 8 files changed, 66 insertions(+), 55 deletions(-) diff --git a/src/yunohost/app.py b/src/yunohost/app.py index bf41f163d..ca56be232 100644 --- a/src/yunohost/app.py +++ b/src/yunohost/app.py @@ -627,7 +627,7 @@ def app_upgrade(app=[], url=None, file=None, force=False, no_safety_backup=False if upgrade_failed or broke_the_system: # display this if there are remaining apps - if apps[number + 1 :]: + if apps[number + 1:]: not_upgraded_apps = apps[number:] logger.error( m18n.n( @@ -1626,9 +1626,12 @@ class AppConfigPanel(ConfigPanel): error=message, ) - def _call_config_script(self, action, env={}): + def _call_config_script(self, action, env=None): from yunohost.hook import hook_exec + if env is None: + env = {} + # Add default config script if needed config_script = os.path.join( APPS_SETTING_PATH, self.entity, "scripts", "config" diff --git a/src/yunohost/certificate.py b/src/yunohost/certificate.py index 79e3ae092..2f3676202 100644 --- a/src/yunohost/certificate.py +++ b/src/yunohost/certificate.py @@ -70,28 +70,28 @@ PRODUCTION_CERTIFICATION_AUTHORITY = "https://acme-v02.api.letsencrypt.org" # -def certificate_status(domain_list, full=False): +def certificate_status(domains, full=False): """ Print the status of certificate for given domains (all by default) Keyword argument: - domain_list -- Domains to be checked + domains -- Domains to be checked full -- Display more info about the certificates """ - import yunohost.domain + from yunohost.domain import domain_list, _assert_domain_exists # If no domains given, consider all yunohost domains - if domain_list == []: - domain_list = yunohost.domain.domain_list()["domains"] + if domains == []: + domains = domain_list()["domains"] # Else, validate that yunohost knows the domains given else: - for domain in domain_list: - yunohost.domain._assert_domain_exists(domain) + for domain in domains: + _assert_domain_exists(domain) certificates = {} - for domain in domain_list: + for domain in domains: status = _get_status(domain) if not full: @@ -239,28 +239,28 @@ def _certificate_install_selfsigned(domain_list, force=False): def _certificate_install_letsencrypt( - domain_list, force=False, no_checks=False, staging=False + domains, force=False, no_checks=False, staging=False ): - import yunohost.domain + from yunohost.domain import domain_list, _assert_domain_exists if not os.path.exists(ACCOUNT_KEY_FILE): _generate_account_key() # If no domains given, consider all yunohost domains with self-signed # certificates - if domain_list == []: - for domain in yunohost.domain.domain_list()["domains"]: + if domains == []: + for domain in domain_list()["domains"]: status = _get_status(domain) if status["CA_type"]["code"] != "self-signed": continue - domain_list.append(domain) + domains.append(domain) # Else, validate that yunohost knows the domains given else: - for domain in domain_list: - yunohost.domain._assert_domain_exists(domain) + for domain in domains: + _assert_domain_exists(domain) # Is it self-signed? status = _get_status(domain) @@ -275,7 +275,7 @@ def _certificate_install_letsencrypt( ) # Actual install steps - for domain in domain_list: + for domain in domains: if not no_checks: try: @@ -311,25 +311,25 @@ def _certificate_install_letsencrypt( def certificate_renew( - domain_list, force=False, no_checks=False, email=False, staging=False + domains, force=False, no_checks=False, email=False, staging=False ): """ Renew Let's Encrypt certificate for given domains (all by default) Keyword argument: - domain_list -- Domains for which to renew the certificates + domains -- Domains for which to renew the certificates force -- Ignore the validity threshold (15 days) no-check -- Disable some checks about the reachability of web server before attempting the renewing email -- Emails root if some renewing failed """ - import yunohost.domain + from yunohost.domain import domain_list, _assert_domain_exists # If no domains given, consider all yunohost domains with Let's Encrypt # certificates - if domain_list == []: - for domain in yunohost.domain.domain_list()["domains"]: + if domains == []: + for domain in domain_list()["domains"]: # Does it have a Let's Encrypt cert? status = _get_status(domain) @@ -347,17 +347,17 @@ def certificate_renew( ) continue - domain_list.append(domain) + domains.append(domain) - if len(domain_list) == 0 and not email: + if len(domains) == 0 and not email: logger.info("No certificate needs to be renewed.") # Else, validate the domain list given else: - for domain in domain_list: + for domain in domains: # Is it in Yunohost domain list? - yunohost.domain._assert_domain_exists(domain) + _assert_domain_exists(domain) status = _get_status(domain) @@ -385,7 +385,7 @@ def certificate_renew( ) # Actual renew steps - for domain in domain_list: + for domain in domains: if not no_checks: try: diff --git a/src/yunohost/data_migrations/0018_xtable_to_nftable.py b/src/yunohost/data_migrations/0018_xtable_to_nftable.py index 94b47d944..374620f2f 100644 --- a/src/yunohost/data_migrations/0018_xtable_to_nftable.py +++ b/src/yunohost/data_migrations/0018_xtable_to_nftable.py @@ -41,7 +41,8 @@ class MyMigration(Migration): ) # For some reason if we don't do this, iptables-legacy-save is empty ? self.runcmd("iptables-legacy-save > %s" % self.backup_rules_ipv4) assert ( - open(self.backup_rules_ipv4).read().strip() + os.path.exists(self.backup_rules_ipv4) and + os.stat(self.backup_rules_ipv4).st_size > 0 ), "Uhoh backup of legacy ipv4 rules is empty !?" if self.do_ipv6 and not os.path.exists(self.backup_rules_ipv6): self.runcmd( @@ -49,7 +50,8 @@ class MyMigration(Migration): ) # For some reason if we don't do this, iptables-legacy-save is empty ? self.runcmd("ip6tables-legacy-save > %s" % self.backup_rules_ipv6) assert ( - open(self.backup_rules_ipv6).read().strip() + os.path.exists(self.backup_rules_ipv6) and + os.stat(self.backup_rules_ipv6).st_size > 0 ), "Uhoh backup of legacy ipv6 rules is empty !?" # We inject the legacy rules (iptables-legacy) into the new iptable (just "iptables") diff --git a/src/yunohost/domain.py b/src/yunohost/domain.py index 0bd84ea82..d1ea45a08 100644 --- a/src/yunohost/domain.py +++ b/src/yunohost/domain.py @@ -507,17 +507,17 @@ def _set_domain_settings(domain: str, settings: dict) -> None: def domain_cert_status(domain_list, full=False): - import yunohost.certificate + from yunohost.certificate import certificate_status - return yunohost.certificate.certificate_status(domain_list, full) + return certificate_status(domain_list, full) def domain_cert_install( domain_list, force=False, no_checks=False, self_signed=False, staging=False ): - import yunohost.certificate + from yunohost.certificate import certificate_install - return yunohost.certificate.certificate_install( + return certificate_install( domain_list, force, no_checks, self_signed, staging ) @@ -525,9 +525,9 @@ def domain_cert_install( def domain_cert_renew( domain_list, force=False, no_checks=False, email=False, staging=False ): - import yunohost.certificate + from yunohost.certificate import certificate_renew - return yunohost.certificate.certificate_renew( + return certificate_renew( domain_list, force, no_checks, email, staging ) @@ -537,12 +537,12 @@ def domain_dns_conf(domain): def domain_dns_suggest(domain): - import yunohost.dns + from yunohost.dns import domain_dns_suggest - return yunohost.dns.domain_dns_suggest(domain) + return domain_dns_suggest(domain) def domain_dns_push(domain, dry_run, force, purge): - import yunohost.dns + from yunohost.dns import domain_dns_push - return yunohost.dns.domain_dns_push(domain, dry_run, force, purge) + return domain_dns_push(domain, dry_run, force, purge) diff --git a/src/yunohost/regenconf.py b/src/yunohost/regenconf.py index adec5508d..4b697cd84 100644 --- a/src/yunohost/regenconf.py +++ b/src/yunohost/regenconf.py @@ -48,7 +48,7 @@ logger = log.getActionLogger("yunohost.regenconf") @is_unit_operation([("names", "configuration")]) def regen_conf( operation_logger, - names=[], + names=None, with_diff=False, force=False, dry_run=False, @@ -66,6 +66,9 @@ def regen_conf( """ + if names is None: + names = [] + result = {} # Return the list of pending conf diff --git a/src/yunohost/tools.py b/src/yunohost/tools.py index a0411c69e..6dd4032e9 100644 --- a/src/yunohost/tools.py +++ b/src/yunohost/tools.py @@ -1130,7 +1130,7 @@ class Migration: def description(self): return m18n.n("migration_description_%s" % self.id) - def ldap_migration(run): + def ldap_migration(self, run): def func(self): # Backup LDAP before the migration diff --git a/src/yunohost/user.py b/src/yunohost/user.py index 78fda8d09..a27fffbee 100644 --- a/src/yunohost/user.py +++ b/src/yunohost/user.py @@ -1263,25 +1263,25 @@ def user_group_remove(groupname, usernames, force=False, sync_perm=True): def user_permission_list(short=False, full=False, apps=[]): - import yunohost.permission + from yunohost.permission import user_permission_list - return yunohost.permission.user_permission_list( + return user_permission_list( short, full, absolute_urls=True, apps=apps ) def user_permission_update(permission, label=None, show_tile=None, sync_perm=True): - import yunohost.permission + from yunohost.permission import user_permission_update - return yunohost.permission.user_permission_update( + return user_permission_update( permission, label=label, show_tile=show_tile, sync_perm=sync_perm ) def user_permission_add(permission, names, protected=None, force=False, sync_perm=True): - import yunohost.permission + from yunohost.permission import user_permission_update - return yunohost.permission.user_permission_update( + return user_permission_update( permission, add=names, protected=protected, force=force, sync_perm=sync_perm ) @@ -1289,23 +1289,23 @@ def user_permission_add(permission, names, protected=None, force=False, sync_per def user_permission_remove( permission, names, protected=None, force=False, sync_perm=True ): - import yunohost.permission + from yunohost.permission import user_permission_update - return yunohost.permission.user_permission_update( + return user_permission_update( permission, remove=names, protected=protected, force=force, sync_perm=sync_perm ) def user_permission_reset(permission, sync_perm=True): - import yunohost.permission + from yunohost.permission import user_permission_reset - return yunohost.permission.user_permission_reset(permission, sync_perm=sync_perm) + return user_permission_reset(permission, sync_perm=sync_perm) def user_permission_info(permission): - import yunohost.permission + from yunohost.permission import user_permission_info - return yunohost.permission.user_permission_info(permission) + return user_permission_info(permission) # diff --git a/src/yunohost/utils/config.py b/src/yunohost/utils/config.py index e3b09f870..5b286a92b 100644 --- a/src/yunohost/utils/config.py +++ b/src/yunohost/utils/config.py @@ -52,7 +52,10 @@ CONFIG_PANEL_VERSION_SUPPORTED = 1.0 # Those js-like evaluate functions are used to eval safely visible attributes # The goal is to evaluate in the same way than js simple-evaluate # https://github.com/shepherdwind/simple-evaluate -def evaluate_simple_ast(node, context={}): +def evaluate_simple_ast(node, context=None): + if context is None: + context = {} + operators = { ast.Not: op.not_, ast.Mult: op.mul, From 7cb96c6652eeb6eb1e6a2e37fda26e9d396abfd9 Mon Sep 17 00:00:00 2001 From: Kay0u Date: Thu, 30 Dec 2021 00:25:54 +0100 Subject: [PATCH 5/5] fix ldap_migration decorator --- src/yunohost/tools.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/yunohost/tools.py b/src/yunohost/tools.py index 6dd4032e9..79c558b2d 100644 --- a/src/yunohost/tools.py +++ b/src/yunohost/tools.py @@ -1130,7 +1130,8 @@ class Migration: def description(self): return m18n.n("migration_description_%s" % self.id) - def ldap_migration(self, run): + @staticmethod + def ldap_migration(run): def func(self): # Backup LDAP before the migration