diff --git a/nxdrive/client/local/base.py b/nxdrive/client/local/base.py index 77e01bb1a3..46b2e72426 100644 --- a/nxdrive/client/local/base.py +++ b/nxdrive/client/local/base.py @@ -6,7 +6,7 @@ import unicodedata import uuid from contextlib import suppress -from datetime import datetime +from datetime import datetime, timezone from logging import getLogger from pathlib import Path from tempfile import mkdtemp @@ -263,12 +263,12 @@ def get_info(self, ref: Path, /, *, check: bool = True) -> FileInfo: stat_info = os_path.stat() size = 0 if folderish else stat_info.st_size try: - mtime = datetime.utcfromtimestamp(stat_info.st_mtime) + mtime = datetime.fromtimestamp(stat_info.st_mtime, tz=timezone.utc) except (ValueError, OverflowError, OSError) as e: log.warning( f"{e} file path: {os_path}. st_mtime value: {stat_info.st_mtime}" ) - mtime = datetime.utcfromtimestamp(0) + mtime = datetime.fromtimestamp(0, tz=timezone.utc) # TODO Do we need to load it every time ? remote_ref = self.get_remote_id(ref) diff --git a/nxdrive/dao/base.py b/nxdrive/dao/base.py index ab3462ddb4..bf5905f769 100644 --- a/nxdrive/dao/base.py +++ b/nxdrive/dao/base.py @@ -2,6 +2,7 @@ Query formatting in this file is based on http://www.sqlstyle.guide/ """ +from datetime import datetime, timezone import sys from contextlib import suppress from logging import getLogger @@ -19,14 +20,20 @@ log = getLogger(__name__) - class AutoRetryCursor(Cursor): + def adapt_datetime_iso(self, val): + return datetime.fromtimestamp(val.strftime('%s'), tz=timezone.utc) def execute(self, sql: str, parameters: Iterable[Any] = ()) -> Cursor: count = 1 while True: count += 1 try: - return super().execute(sql, parameters) + import sqlite3 + # return super().execute(sql, parameters) + # new_param = tuple( datetime.fromtimestamp(param, tz=timezone.utc) if isinstance(param, datetime) else param for param in parameters ) + new_param = tuple( sqlite3.register_adapter(param, self.adapt_datetime_iso) if isinstance(param, datetime) else param for param in parameters ) + + return super().execute(sql, new_param) except OperationalError as exc: log.info( f"Retry locked database #{count}, {sql=}, {parameters=}", diff --git a/nxdrive/dao/utils.py b/nxdrive/dao/utils.py index b1ba95d9fc..f88e5a9499 100644 --- a/nxdrive/dao/utils.py +++ b/nxdrive/dao/utils.py @@ -36,15 +36,15 @@ def dump(database: Path, dump_file: Path, /) -> None: """ log.info(f"Dumping the database {database!r} into {dump_file!r}...") - with sqlite3.connect(str(database)) as con, dump_file.open( - mode="w", encoding="utf-8" - ) as f: + con = sqlite3.connect(str(database)) + with dump_file.open(mode="w", encoding="utf-8") as f: for line in con.iterdump(): f.write(f"{line}\n") # Force write of file to disk f.flush() fsync(f.fileno()) + con.close() log.info("Dump finished with success.") diff --git a/nxdrive/direct_edit.py b/nxdrive/direct_edit.py index 4793c791c5..303ca660db 100644 --- a/nxdrive/direct_edit.py +++ b/nxdrive/direct_edit.py @@ -1111,4 +1111,5 @@ def _get_ref(self, src_path: Path) -> Path: ref = self.local.get_path(src_path) if ref not in self._file_metrics: self._file_metrics[ref] = defaultdict(int) + # testing return ref diff --git a/tests/conftest.py b/tests/conftest.py index c08b9b0496..8a2de4fb85 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -95,10 +95,6 @@ def no_warnings(recwarn): continue elif "Cryptography will be significantly faster" in message: continue - elif "The default datetime adapter is deprecated as of Python 3.12" in message: - continue - elif "datetime.datetime" in message: - continue warn = f"{warning.filename}:{warning.lineno} {message}" print(warn, file=sys.stderr) diff --git a/tests/unit/conftest.py b/tests/unit/conftest.py index 5ee49abf9f..7d1ddae65e 100644 --- a/tests/unit/conftest.py +++ b/tests/unit/conftest.py @@ -40,7 +40,8 @@ def __enter__(self): def __exit__(self, exc_type, exc_val, exc_tb): self.dispose() - self.db.unlink() + os.system(f'rmdir "{self.db}"') + # self.db.unlink() def _get_adjacent_sync_file( self, ref: str, comp: str, order: str, sync_mode: str = None diff --git a/tests/unit/test_proxy.py b/tests/unit/test_proxy.py index f90787ebc3..2ec07007e9 100644 --- a/tests/unit/test_proxy.py +++ b/tests/unit/test_proxy.py @@ -1,5 +1,6 @@ from unittest.mock import patch +import os import pytest from nxdrive.client.proxy import ( @@ -49,7 +50,7 @@ def config_dao(tmp_path): yield dao finally: dao.dispose() - db.unlink() + os.system(f'rmdir "{db}"') @pytest.fixture diff --git a/tests/unit/test_utils.py b/tests/unit/test_utils.py index 5c0d96cc95..ef46fcb598 100644 --- a/tests/unit/test_utils.py +++ b/tests/unit/test_utils.py @@ -3,7 +3,7 @@ from collections import namedtuple from datetime import datetime from math import pow -from pathlib import Path, _posix_flavour, _windows_flavour +from pathlib import Path from time import sleep from unittest.mock import patch @@ -75,18 +75,6 @@ Stat = namedtuple("Stat", "st_size") -class MockedPath(Path): - """Simple way to test Path methods. - Using mock did not make it. - """ - - _flavour = _windows_flavour if WINDOWS else _posix_flavour - - def resolve(self, *_, **__): - """Raise a PermissionError.""" - raise PermissionError("Boom!") - - @pytest.mark.parametrize( "size, digest_func, result", [