From f0377dd59ee10891edac84443e4a4018708d016b Mon Sep 17 00:00:00 2001 From: Teal Dulcet Date: Sat, 23 Dec 2023 05:26:32 -0800 Subject: [PATCH] Fixed SIM105 (suppressible-exception) --- management/cli.py | 5 ++--- management/daemon.py | 5 ++--- management/dns_update.py | 5 ++--- setup/migrate.py | 5 ++--- 4 files changed, 8 insertions(+), 12 deletions(-) diff --git a/management/cli.py b/management/cli.py index ea060d3f..70dbe894 100755 --- a/management/cli.py +++ b/management/cli.py @@ -7,6 +7,7 @@ # tool can only be used as root. import sys, getpass, urllib.request, urllib.error, json, csv +import contextlib def mgmt(cmd, data=None, is_json=False): # The base URL for the management daemon. (Listens on IPv4 only.) @@ -19,10 +20,8 @@ def mgmt(cmd, data=None, is_json=False): response = urllib.request.urlopen(req) except urllib.error.HTTPError as e: if e.code == 401: - try: + with contextlib.suppress(Exception): print(e.read().decode("utf8")) - except: - pass print("The management daemon refused access. The API key file may be out of sync. Try 'service mailinabox restart'.", file=sys.stderr) elif hasattr(e, 'read'): print(e.read().decode('utf8'), file=sys.stderr) diff --git a/management/daemon.py b/management/daemon.py index 074e9fa7..bb4cd15d 100755 --- a/management/daemon.py +++ b/management/daemon.py @@ -22,6 +22,7 @@ from mailconfig import get_mail_users, get_mail_users_ex, get_admins, add_mail_u from mailconfig import get_mail_user_privileges, add_remove_mail_user_privilege from mailconfig import get_mail_aliases, get_mail_aliases_ex, get_mail_domains, add_mail_alias, remove_mail_alias from mfa import get_public_mfa_state, provision_totp, validate_totp_secret, enable_mfa, disable_mfa +import contextlib env = utils.load_environment() @@ -29,10 +30,8 @@ auth_service = auth.AuthService() # We may deploy via a symbolic link, which confuses flask's template finding. me = __file__ -try: +with contextlib.suppress(OSError): me = os.readlink(__file__) -except OSError: - pass # for generating CSRs we need a list of country codes csr_country_codes = [] diff --git a/management/dns_update.py b/management/dns_update.py index c06ef352..5e24f118 100755 --- a/management/dns_update.py +++ b/management/dns_update.py @@ -11,6 +11,7 @@ import dns.resolver from utils import shell, load_env_vars_from_file, safe_domain_name, sort_domains from ssl_certificates import get_ssl_certificates, check_certificate +import contextlib # From https://stackoverflow.com/questions/3026957/how-to-validate-a-domain-name-using-regex-php/16491074#16491074 # This regular expression matches domain names according to RFCs, it also accepts fqdn with an leading dot, @@ -456,10 +457,8 @@ def build_sshfp_records(): for line in f: s = line.rstrip().split() if len(s) == 2 and s[0] == 'Port': - try: + with contextlib.suppress(ValueError): port = int(s[1]) - except ValueError: - pass break keys = shell("check_output", ["ssh-keyscan", "-4", "-t", "rsa,dsa,ecdsa,ed25519", "-p", str(port), "localhost"]) diff --git a/setup/migrate.py b/setup/migrate.py index 3364b55d..17fbd5cf 100755 --- a/setup/migrate.py +++ b/setup/migrate.py @@ -9,6 +9,7 @@ import sys, os, os.path, glob, re, shutil sys.path.insert(0, 'management') from utils import load_environment, save_environment, shell +import contextlib def migration_1(env): # Re-arrange where we store SSL certificates. There was a typo also. @@ -31,10 +32,8 @@ def migration_1(env): move_file(sslfn, domain_name, file_type) # Move the old domains directory if it is now empty. - try: + with contextlib.suppress(Exception): os.rmdir(os.path.join( env["STORAGE_ROOT"], 'ssl/domains')) - except: - pass def migration_2(env): # Delete the .dovecot_sieve script everywhere. This was formerly a copy of our spam -> Spam