From b9060da7508e1e3ec6d5624aca331a878a95f2c4 Mon Sep 17 00:00:00 2001 From: yunohost-bot Date: Thu, 20 Jan 2022 18:43:33 +0000 Subject: [PATCH] [CI] Format code with Black --- maintenance/autofix_locale_format.py | 15 ++++++++------- src/__init__.py | 2 +- src/app.py | 15 ++++++++++----- src/app_catalog.py | 17 +++++++++++++---- src/backup.py | 7 +++---- src/certificate.py | 9 +++++---- src/diagnosers/10-ip.py | 4 +--- src/diagnosis.py | 18 +++++++++++------- src/domain.py | 4 +--- src/dyndns.py | 17 +++++++++-------- src/migrations/0021_migrate_to_bullseye.py | 4 ++-- src/permission.py | 7 ++----- src/regenconf.py | 4 +--- src/service.py | 19 ++++++++++++------- src/settings.py | 16 +++++++--------- src/tools.py | 10 ++++------ src/user.py | 8 ++------ src/utils/error.py | 1 - src/utils/legacy.py | 21 ++++++++++++++------- 19 files changed, 106 insertions(+), 92 deletions(-) diff --git a/maintenance/autofix_locale_format.py b/maintenance/autofix_locale_format.py index 6eb78b497..1c56ea386 100644 --- a/maintenance/autofix_locale_format.py +++ b/maintenance/autofix_locale_format.py @@ -60,18 +60,20 @@ def autofix_i18n_placeholders(): k[0] for k in re.findall(r"{(\w+)(:\w)?}", this_locale[key]) ] if any(k not in subkeys_in_ref for k in subkeys_in_this_locale): - raise Exception("""\n + raise Exception( + """\n ========================== Format inconsistency for string {key} in {locale_file}:" en.json -> {string} {locale_file} -> {translated_string} Please fix it manually ! """.format( - key=key, - string=string.encode("utf-8"), - locale_file=locale_file, - translated_string=this_locale[key].encode("utf-8"), - )) + key=key, + string=string.encode("utf-8"), + locale_file=locale_file, + translated_string=this_locale[key].encode("utf-8"), + ) + ) if fixed_stuff: json.dump( @@ -86,7 +88,6 @@ Please fix it manually ! def autofix_orthotypography_and_standardized_words(): - def reformat(lang, transformations): locale = open(f"{LOCALE_FOLDER}{lang}.json").read() diff --git a/src/__init__.py b/src/__init__.py index b9dcd93d9..608917185 100644 --- a/src/__init__.py +++ b/src/__init__.py @@ -28,7 +28,7 @@ def cli(debug, quiet, output_as, timeout, args, parser): locales_dir="/usr/share/yunohost/locales/", output_as=output_as, timeout=timeout, - top_parser=parser + top_parser=parser, ) sys.exit(ret) diff --git a/src/app.py b/src/app.py index af13765e3..e03d94317 100644 --- a/src/app.py +++ b/src/app.py @@ -1425,7 +1425,7 @@ def app_action_run(operation_logger, app, action, args=None): actions = {x["id"]: x for x in actions} if action not in actions: - available_actions = ", ".join(actions.keys()), + available_actions = (", ".join(actions.keys()),) raise YunohostValidationError( f"action '{action}' not available for app '{app}', available actions are: {available_actions}", raw_msg=True, @@ -2416,10 +2416,15 @@ def unstable_apps(): for infos in app_list(full=True)["apps"]: - if not infos.get("from_catalog") or infos.get("from_catalog").get("state") in [ - "inprogress", - "notworking", - ] or infos["id"] in deprecated_apps: + if ( + not infos.get("from_catalog") + or infos.get("from_catalog").get("state") + in [ + "inprogress", + "notworking", + ] + or infos["id"] in deprecated_apps + ): output.append(infos["id"]) return output diff --git a/src/app_catalog.py b/src/app_catalog.py index b2b35b8c3..5ae8ef30b 100644 --- a/src/app_catalog.py +++ b/src/app_catalog.py @@ -103,7 +103,9 @@ def _initialize_apps_catalog_system(): ) 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) + raise YunohostError( + f"Could not initialize the apps catalog system... : {e}", raw_msg=True + ) logger.success(m18n.n("apps_catalog_init_success")) @@ -119,7 +121,9 @@ def _read_apps_catalog_list(): # by returning [] if list_ is None return list_ if list_ else [] except Exception as e: - raise YunohostError(f"Could not read the apps_catalog list ... : {e}", raw_msg=True) + raise YunohostError( + f"Could not read the apps_catalog list ... : {e}", raw_msg=True + ) def _actual_apps_catalog_api_url(base_url): @@ -172,7 +176,10 @@ def _update_apps_catalog(): try: write_to_json(cache_file, apps_catalog_content) except Exception as e: - raise YunohostError(f"Unable to write cache data for {apps_catalog_id} apps_catalog : {e}", raw_msg=True) + raise YunohostError( + f"Unable to write cache data for {apps_catalog_id} apps_catalog : {e}", + raw_msg=True, + ) logger.success(m18n.n("apps_catalog_update_success")) @@ -220,7 +227,9 @@ def _load_apps_catalog(): # in which case we keep only the first one found) if app in merged_catalog["apps"]: other_catalog = merged_catalog["apps"][app]["repository"] - logger.warning(f"Duplicate app {app} found between apps catalog {apps_catalog_id} and {other_catalog}") + logger.warning( + f"Duplicate app {app} found between apps catalog {apps_catalog_id} and {other_catalog}" + ) continue info["repository"] = apps_catalog_id diff --git a/src/backup.py b/src/backup.py index 57e667d8d..bba60b895 100644 --- a/src/backup.py +++ b/src/backup.py @@ -866,7 +866,7 @@ class RestoreManager: from_version = self.info.get("from_yunohost_version", "") # Remove any '~foobar' in the version ... c.f ~alpha, ~beta version during # early dev for next debian version - from_version = re.sub(r'~\w+', '', from_version) + from_version = re.sub(r"~\w+", "", from_version) if not from_version or version.parse(from_version) < version.parse("4.2.0"): raise YunohostValidationError("restore_backup_too_old") @@ -2403,10 +2403,9 @@ def backup_list(with_info=False, human_readable=False): logger.warning(str(e)) except Exception: import traceback + trace_ = "\n" + traceback.format_exc() - logger.warning( - f"Could not check infos for archive {archive}: {trace_}" - ) + logger.warning(f"Could not check infos for archive {archive}: {trace_}") archives = d diff --git a/src/certificate.py b/src/certificate.py index a681d106b..2ad294605 100644 --- a/src/certificate.py +++ b/src/certificate.py @@ -415,9 +415,7 @@ def certificate_renew( traceback.print_exc(file=stack) msg = f"Certificate renewing for {domain} failed!" if no_checks: - msg += ( - f"\nPlease consider checking the 'DNS records' (basic) and 'Web' categories of the diagnosis to check for possible issues that may prevent installing a Let's Encrypt certificate on domain {domain}." - ) + msg += f"\nPlease consider checking the 'DNS records' (basic) and 'Web' categories of the diagnosis to check for possible issues that may prevent installing a Let's Encrypt certificate on domain {domain}." logger.error(msg) operation_logger.error(msg) logger.error(stack.getvalue()) @@ -788,7 +786,10 @@ def _enable_certificate(domain, new_cert_folder): for service in ("postfix", "dovecot", "metronome"): # Ugly trick to not restart metronome if it's not installed - if service == "metronome" and os.system("dpkg --list | grep -q 'ii *metronome'") != 0: + if ( + service == "metronome" + and os.system("dpkg --list | grep -q 'ii *metronome'") != 0 + ): continue _run_service_command("restart", service) diff --git a/src/diagnosers/10-ip.py b/src/diagnosers/10-ip.py index 5e49ae927..247c486fc 100644 --- a/src/diagnosers/10-ip.py +++ b/src/diagnosers/10-ip.py @@ -155,9 +155,7 @@ class MyDiagnoser(Diagnoser): return None # We use the resolver file as a list of well-known, trustable (ie not google ;)) IPs that we can ping - resolver_file = ( - "/usr/share/yunohost/conf/dnsmasq/plain/resolv.dnsmasq.conf" - ) + resolver_file = "/usr/share/yunohost/conf/dnsmasq/plain/resolv.dnsmasq.conf" resolvers = [ r.split(" ")[1] for r in read_file(resolver_file).split("\n") diff --git a/src/diagnosis.py b/src/diagnosis.py index 2486887b9..b44028d29 100644 --- a/src/diagnosis.py +++ b/src/diagnosis.py @@ -419,10 +419,7 @@ class Diagnoser: def diagnose(self, force=False): - if ( - not force - and self.cached_time_ago() < self.cache_duration - ): + if not force and self.cached_time_ago() < self.cache_duration: logger.debug(f"Cache still valid : {self.cache_file}") logger.info( m18n.n("diagnosis_cache_still_valid", category=self.description) @@ -659,7 +656,9 @@ class Diagnoser: def _list_diagnosis_categories(): paths = glob.glob(os.path.dirname(__file__) + "/diagnosers/??-*.py") - names = sorted([os.path.basename(path)[: -len(".py")].split("-")[-1] for path in paths]) + names = sorted( + [os.path.basename(path)[: -len(".py")].split("-")[-1] for path in paths] + ) return names @@ -671,7 +670,10 @@ def _load_diagnoser(diagnoser_name): paths = glob.glob(os.path.dirname(__file__) + f"/diagnosers/??-{diagnoser_name}.py") if len(paths) != 1: - raise YunohostError(f"Uhoh, found several matches (or none?) for diagnoser {diagnoser_name} : {paths}", raw_msg=True) + raise YunohostError( + f"Uhoh, found several matches (or none?) for diagnoser {diagnoser_name} : {paths}", + raw_msg=True, + ) module_id = os.path.basename(paths[0][: -len(".py")]) @@ -686,7 +688,9 @@ def _load_diagnoser(diagnoser_name): traceback.print_exc() - raise YunohostError(f"Failed to load diagnoser {diagnoser_name} : {e}", raw_msg=True) + raise YunohostError( + f"Failed to load diagnoser {diagnoser_name} : {e}", raw_msg=True + ) def _email_diagnosis_issues(): diff --git a/src/domain.py b/src/domain.py index 6fd1724b4..c94dc694d 100644 --- a/src/domain.py +++ b/src/domain.py @@ -68,9 +68,7 @@ def domain_list(exclude_subdomains=False): ldap = _get_ldap_interface() result = [ entry["virtualdomain"][0] - for entry in ldap.search( - "ou=domains", "virtualdomain=*", ["virtualdomain"] - ) + for entry in ldap.search("ou=domains", "virtualdomain=*", ["virtualdomain"]) ] result_list = [] diff --git a/src/dyndns.py b/src/dyndns.py index c9da4f1be..34f3dd5dc 100644 --- a/src/dyndns.py +++ b/src/dyndns.py @@ -190,7 +190,6 @@ def dyndns_update( import dns.tsigkeyring import dns.update - # If domain is not given, try to guess it from keys available... key = None if domain is None: @@ -227,7 +226,7 @@ def dyndns_update( with open(key) as f: key = f.readline().strip().split(" ", 6)[-1] - keyring = dns.tsigkeyring.from_text({f'{domain}.': key}) + keyring = dns.tsigkeyring.from_text({f"{domain}.": key}) # Python's dns.update is similar to the old nsupdate cli tool update = dns.update.Update(zone, keyring=keyring, keyalgorithm=dns.tsig.HMAC_SHA512) @@ -300,7 +299,9 @@ def dyndns_update( # [{"name": "...", "ttl": "...", "type": "...", "value": "..."}] for records in dns_conf.values(): for record in records: - name = f"{record['name']}.{domain}." if record['name'] != "@" else f"{domain}." + name = ( + f"{record['name']}.{domain}." if record["name"] != "@" else f"{domain}." + ) update.delete(name) # Add the new records for all domain/subdomains @@ -313,9 +314,11 @@ def dyndns_update( if record["value"] == "@": record["value"] = domain record["value"] = record["value"].replace(";", r"\;") - name = f"{record['name']}.{domain}." if record['name'] != "@" else f"{domain}." + name = ( + f"{record['name']}.{domain}." if record["name"] != "@" else f"{domain}." + ) - update.add(name, record['ttl'], record['type'], record['value']) + update.add(name, record["ttl"], record["type"], record["value"]) logger.debug("Now pushing new conf to DynDNS host...") logger.debug(update) @@ -347,9 +350,7 @@ def _guess_current_dyndns_domain(): dynette...) """ - DYNDNS_KEY_REGEX = re.compile( - r".*/K(?P[^\s\+]+)\.\+165.+\.key$" - ) + DYNDNS_KEY_REGEX = re.compile(r".*/K(?P[^\s\+]+)\.\+165.+\.key$") # Retrieve the first registered domain paths = list(glob.iglob("/etc/yunohost/dyndns/K*.key")) diff --git a/src/migrations/0021_migrate_to_bullseye.py b/src/migrations/0021_migrate_to_bullseye.py index 77797c63f..f4361cb19 100644 --- a/src/migrations/0021_migrate_to_bullseye.py +++ b/src/migrations/0021_migrate_to_bullseye.py @@ -331,8 +331,8 @@ class MyMigration(Migration): # FIXME: update this message with updated topic link once we release the migration as stable message = ( - "N.B.: **THIS MIGRATION IS STILL IN BETA-STAGE** ! If your server hosts critical services and if you are not too confident with debugging possible issues, we recommend you to wait a little bit more while we gather more feedback and polish things up. If on the other hand you are relatively confident with debugging small issues that may arise, you are encouraged to run this migration ;)! You can read and share feedbacks on this forum thread: https://forum.yunohost.org/t/18531\n\n" - + message + "N.B.: **THIS MIGRATION IS STILL IN BETA-STAGE** ! If your server hosts critical services and if you are not too confident with debugging possible issues, we recommend you to wait a little bit more while we gather more feedback and polish things up. If on the other hand you are relatively confident with debugging small issues that may arise, you are encouraged to run this migration ;)! You can read and share feedbacks on this forum thread: https://forum.yunohost.org/t/18531\n\n" + + message ) # message = ( # "N.B.: This migration has been tested by the community over the last few months but has only been declared stable recently. If your server hosts critical services and if you are not too confident with debugging possible issues, we recommend you to wait a little bit more while we gather more feedback and polish things up. If on the other hand you are relatively confident with debugging small issues that may arise, you are encouraged to run this migration ;)! You can read about remaining known issues and feedback from the community here: https://forum.yunohost.org/t/12195\n\n" diff --git a/src/permission.py b/src/permission.py index 995cd34bb..2a6f6d954 100644 --- a/src/permission.py +++ b/src/permission.py @@ -406,9 +406,7 @@ def permission_create( permission = permission + ".main" # Validate uniqueness of permission in LDAP - if ldap.get_conflict( - {"cn": permission}, base_dn="ou=permission" - ): + if ldap.get_conflict({"cn": permission}, base_dn="ou=permission"): raise YunohostValidationError("permission_already_exist", permission=permission) # Get random GID @@ -678,8 +676,7 @@ def permission_sync_to_user(): new_inherited_perms = { "inheritPermission": [ - f"uid={u},ou=users,dc=yunohost,dc=org" - for u in should_be_allowed_users + f"uid={u},ou=users,dc=yunohost,dc=org" for u in should_be_allowed_users ], "memberUid": should_be_allowed_users, } diff --git a/src/regenconf.py b/src/regenconf.py index 0f855878d..5922e6832 100644 --- a/src/regenconf.py +++ b/src/regenconf.py @@ -505,9 +505,7 @@ def _calculate_hash(path): return hasher.hexdigest() except IOError as e: - logger.warning( - f"Error while calculating file '{path}' hash: {e}", exc_info=1 - ) + logger.warning(f"Error while calculating file '{path}' hash: {e}", exc_info=1) return None diff --git a/src/service.py b/src/service.py index 4da5d5546..988c7eb13 100644 --- a/src/service.py +++ b/src/service.py @@ -695,19 +695,25 @@ def _get_services(): if "log" not in services["ynh-vpnclient"]: services["ynh-vpnclient"]["log"] = ["/var/log/ynh-vpnclient.log"] - services_with_package_condition = [name for name, infos in services.items() if infos.get("ignore_if_package_is_not_installed")] + services_with_package_condition = [ + name + for name, infos in services.items() + if infos.get("ignore_if_package_is_not_installed") + ] for name in services_with_package_condition: package = services[name]["ignore_if_package_is_not_installed"] if os.system(f"dpkg --list | grep -q 'ii *{package}'") != 0: del services[name] - php_fpm_versions = check_output(r"dpkg --list | grep -P 'ii php\d.\d-fpm' | awk '{print $2}' | grep -o -P '\d.\d' || true") - php_fpm_versions = [v for v in php_fpm_versions.split('\n') if v.strip()] + php_fpm_versions = check_output( + r"dpkg --list | grep -P 'ii php\d.\d-fpm' | awk '{print $2}' | grep -o -P '\d.\d' || true" + ) + php_fpm_versions = [v for v in php_fpm_versions.split("\n") if v.strip()] for version in php_fpm_versions: services[f"php{version}-fpm"] = { "log": f"/var/log/php{version}-fpm.log", "test_conf": f"php-fpm{version} --test", # ofc the service is phpx.y-fpm but the program is php-fpmx.y because why not ... - "category": "web" + "category": "web", } # Remove legacy /var/log/daemon.log and /var/log/syslog from log entries @@ -833,7 +839,6 @@ def _get_journalctl_logs(service, number="all"): ) except Exception: import traceback + trace_ = traceback.format_exc() - return ( - f"error while get services logs from journalctl:\n{trace_}" - ) + return f"error while get services logs from journalctl:\n{trace_}" diff --git a/src/settings.py b/src/settings.py index 498e6d5cc..cec416550 100644 --- a/src/settings.py +++ b/src/settings.py @@ -341,9 +341,7 @@ def _get_settings(): _save_settings(unknown_settings, location=unknown_settings_path) _save_settings(settings) except Exception as e: - logger.warning( - f"Failed to save unknown settings (because {e}), aborting." - ) + logger.warning(f"Failed to save unknown settings (because {e}), aborting.") return settings @@ -373,12 +371,12 @@ post_change_hooks = {} def post_change_hook(setting_name): def decorator(func): - assert setting_name in DEFAULTS.keys(), ( - f"The setting {setting_name} does not exists" - ) - assert setting_name not in post_change_hooks, ( - f"You can only register one post change hook per setting (in particular for {setting_name})" - ) + assert ( + setting_name in DEFAULTS.keys() + ), f"The setting {setting_name} does not exists" + assert ( + setting_name not in post_change_hooks + ), f"You can only register one post change hook per setting (in particular for {setting_name})" post_change_hooks[setting_name] = func return func diff --git a/src/tools.py b/src/tools.py index 9ede24551..28b4457b4 100644 --- a/src/tools.py +++ b/src/tools.py @@ -536,9 +536,7 @@ def tools_upgrade(operation_logger, target=None): # Restart the API after 10 sec (at now doesn't support sub-minute times...) # We do this so that the API / webadmin still gets the proper HTTP response # It's then up to the webadmin to implement a proper UX process to wait 10 sec and then auto-fresh the webadmin - cmd = ( - "at -M now >/dev/null 2>&1 <<< \"sleep 10; systemctl restart yunohost-api\"" - ) + cmd = 'at -M now >/dev/null 2>&1 <<< "sleep 10; systemctl restart yunohost-api"' # For some reason subprocess doesn't like the redirections so we have to use bash -c explicity... subprocess.check_call(["bash", "-c", cmd]) @@ -894,9 +892,9 @@ def _get_migration_by_name(migration_name): if re.match(r"^\d+_%s\.py$" % migration_name, x) ] - assert len(migrations_found) == 1, ( - f"Unable to find migration with name {migration_name}" - ) + assert ( + len(migrations_found) == 1 + ), f"Unable to find migration with name {migration_name}" return _load_migration(migrations_found[0]) diff --git a/src/user.py b/src/user.py index c03023387..7d023fd83 100644 --- a/src/user.py +++ b/src/user.py @@ -254,9 +254,7 @@ def user_create( logger.warning(m18n.n("user_home_creation_failed", home=home), exc_info=1) try: - subprocess.check_call( - ["setfacl", "-m", "g:all_users:---", f"/home/{username}"] - ) + subprocess.check_call(["setfacl", "-m", "g:all_users:---", f"/home/{username}"]) except subprocess.CalledProcessError: logger.warning(f"Failed to protect /home/{username}", exc_info=1) @@ -986,9 +984,7 @@ def user_group_create( ldap = _get_ldap_interface() # Validate uniqueness of groupname in LDAP - conflict = ldap.get_conflict( - {"cn": groupname}, base_dn="ou=groups" - ) + conflict = ldap.get_conflict({"cn": groupname}, base_dn="ou=groups") if conflict: raise YunohostValidationError("group_already_exist", group=groupname) diff --git a/src/utils/error.py b/src/utils/error.py index aa76ba67e..a92f3bd5a 100644 --- a/src/utils/error.py +++ b/src/utils/error.py @@ -65,4 +65,3 @@ class YunohostValidationError(YunohostError): class YunohostAuthenticationError(MoulinetteAuthenticationError): pass - diff --git a/src/utils/legacy.py b/src/utils/legacy.py index 306fcc87f..910dfd5a3 100644 --- a/src/utils/legacy.py +++ b/src/utils/legacy.py @@ -116,10 +116,7 @@ def _patch_legacy_php_versions(app_folder): c = ( "sed -i " - + "".join( - f"-e 's@{p}@{r}@g' " - for p, r in LEGACY_PHP_VERSION_REPLACEMENTS - ) + + "".join(f"-e 's@{p}@{r}@g' " for p, r in LEGACY_PHP_VERSION_REPLACEMENTS) + "%s" % filename ) os.system(c) @@ -137,7 +134,11 @@ def _patch_legacy_php_versions_in_settings(app_folder): settings["phpversion"] = "7.4" # We delete these checksums otherwise the file will appear as manually modified - list_to_remove = ["checksum__etc_php_7.3_fpm_pool", "checksum__etc_php_7.0_fpm_pool", "checksum__etc_nginx_conf.d"] + list_to_remove = [ + "checksum__etc_php_7.3_fpm_pool", + "checksum__etc_php_7.0_fpm_pool", + "checksum__etc_nginx_conf.d", + ] settings = { k: v for k, v in settings.items() @@ -168,9 +169,15 @@ def _patch_legacy_helpers(app_folder): "important": False, }, # Old $1, $2 in backup/restore scripts... - "app=$2": {"only_for": ["scripts/backup", "scripts/restore"], "important": True}, + "app=$2": { + "only_for": ["scripts/backup", "scripts/restore"], + "important": True, + }, # Old $1, $2 in backup/restore scripts... - "backup_dir=$1": {"only_for": ["scripts/backup", "scripts/restore"], "important": True}, + "backup_dir=$1": { + "only_for": ["scripts/backup", "scripts/restore"], + "important": True, + }, # Old $1, $2 in backup/restore scripts... "restore_dir=$1": {"only_for": ["scripts/restore"], "important": True}, # Old $1, $2 in install scripts...