Improve ftp error logging. Update ftp tests.

This commit is contained in:
Anton Skrypnyk
2024-07-26 16:53:20 +03:00
parent 0d16732561
commit 4ea98036e5
3 changed files with 87 additions and 40 deletions

View File

@ -26,6 +26,7 @@ wrapper for FTPS server interactions
from __future__ import annotations
from dataclasses import dataclass
from datetime import datetime, timezone
import ftplib
import os
from pathlib import Path
@ -194,6 +195,28 @@ class IoTFTPSConnection:
pass
return
def get_file_size(self, file_path: str):
try:
return self.ftps_session.size(file_path)
except Exception as e:
raise RuntimeError(
f'Cannot get file size for "{file_path}" due to error: {str(e)}'
)
def get_file_date(self, file_path: str) -> datetime:
try:
date_response = self.ftps_session.sendcmd(f"MDTM {file_path}").replace(
"213 ", ""
)
date = datetime.strptime(date_response, "%Y%m%d%H%M%S").replace(
tzinfo=timezone.utc
)
return date
except Exception as e:
raise RuntimeError(
f'Cannot get file date for "{file_path}" due to error: {str(e)}'
)
@dataclass
class IoTFTPSClient:

View File

@ -102,13 +102,8 @@ class RemoteSDCardFileList:
file_path: Path,
existing_files: list[str] | None = None,
):
file_size = ftp.ftps_session.size(file_path.as_posix())
date_str = ftp.ftps_session.sendcmd(f"MDTM {file_path.as_posix()}").replace(
"213 ", ""
)
date = datetime.datetime.strptime(date_str, "%Y%m%d%H%M%S").replace(
tzinfo=datetime.timezone.utc
)
file_size = ftp.get_file_size(file_path.as_posix())
date = ftp.get_file_date(file_path.as_posix())
file_name = file_path.name.lower()
dosname = get_dos_filename(file_name, existing_filenames=existing_files).lower()
return FileInfo(
@ -128,10 +123,13 @@ class RemoteSDCardFileList:
existing_files = []
for entry in files:
file_info = self._get_ftp_file_info(ftp, entry, existing_files)
yield file_info
existing_files.append(file_info.file_name)
existing_files.append(file_info.dosname)
try:
file_info = self._get_ftp_file_info(ftp, entry, existing_files)
yield file_info
existing_files.append(file_info.file_name)
existing_files.append(file_info.dosname)
except Exception as e:
self._logger.exception(e, exc_info=False)
def get_ftps_client(self):
host = self._settings.get(["host"])