Only turn warnings into errors where needed

warnings.filterwarnings('error') applied to the whole program, so
any warning raised by a library (e.g. a DeprecationWarning) would
crash it. Limit it to the ssh connection (unknown host key) and
the password prompt (no terminal available).
This commit is contained in:
Fuxino
2026-10-08 17:50:47 +02:00
parent e2d8897a45
commit 578d992a19
+10 -5
View File
@@ -37,8 +37,6 @@ from glob import glob
from dotenv import load_dotenv from dotenv import load_dotenv
warnings.filterwarnings('error')
try: try:
import paramiko import paramiko
from paramiko import RSAKey, Ed25519Key, ECDSAKey, DSSKey from paramiko import RSAKey, Ed25519Key, ECDSAKey, DSSKey
@@ -346,7 +344,10 @@ class Backup:
ssh.set_missing_host_key_policy(paramiko.WarningPolicy()) ssh.set_missing_host_key_policy(paramiko.WarningPolicy())
try: try:
ssh.connect(self.ssh_host, username=self.ssh_user) # WarningPolicy emits a UserWarning for unknown host keys
with warnings.catch_warnings():
warnings.simplefilter('error', UserWarning)
ssh.connect(self.ssh_host, username=self.ssh_user)
return ssh return ssh
except UserWarning: except UserWarning:
@@ -374,8 +375,12 @@ class Backup:
if self.ssh_keyfile is None: if self.ssh_keyfile is None:
try: try:
password = getpass( # Fail instead of reading the password with echo if no terminal is available
f'{self.ssh_user}@{self.ssh_host}\'s password: ') with warnings.catch_warnings():
warnings.simplefilter('error', GetPassWarning)
password = getpass(
f'{self.ssh_user}@{self.ssh_host}\'s password: ')
ssh.connect(self.ssh_host, username=self.ssh_user, ssh.connect(self.ssh_host, username=self.ssh_user,
password=password) password=password)