Set mtime and atime to ILIAS dates where possible

This commit is contained in:
I-Al-Istannen 2020-07-13 13:12:01 +02:00
parent 4047fe78f3
commit 34152fbe54
2 changed files with 26 additions and 9 deletions

View File

@ -2,6 +2,8 @@
import datetime
import logging
import math
import os
from pathlib import Path, PurePath
from typing import Callable, List, Optional, Union
@ -119,7 +121,15 @@ class IliasDownloader:
LOGGER.info("Retrying download: %r", info)
self._authenticator.authenticate(self._session)
self._organizer.accept_file(tmp_file, info.path)
dst_path = self._organizer.accept_file(tmp_file, info.path)
if dst_path and info.modification_date:
os.utime(
dst_path,
times=(
math.ceil(info.modification_date.timestamp()),
math.ceil(info.modification_date.timestamp())
)
)
def _try_download(self, info: IliasDownloadInfo, target: Path) -> bool:
url = info.url()

View File

@ -7,7 +7,7 @@ import filecmp
import logging
import shutil
from pathlib import Path, PurePath
from typing import List, Set
from typing import List, Optional, Set
from .download_summary import DownloadSummary
from .location import Location
@ -35,8 +35,15 @@ class Organizer(Location):
self.download_summary = DownloadSummary()
def accept_file(self, src: Path, dst: PurePath) -> None:
"""Move a file to this organizer and mark it."""
def accept_file(self, src: Path, dst: PurePath) -> Optional[Path]:
"""
Move a file to this organizer and mark it.
Returns the path the file was moved to, to allow the caller to adjust the metadata.
As you might still need to adjust the metadata when the file was identical
(e.g. update the timestamp), the path is also returned in this case.
In all other cases (ignored, not overwritten, etc.) this method returns None.
"""
src_absolute = src.resolve()
dst_absolute = self.resolve(dst)
@ -52,7 +59,7 @@ class Organizer(Location):
PRETTY.warning(f"File {str(dst_absolute)!r} was already written!")
if not prompt_yes_no(f"Overwrite file?", default=False):
PRETTY.ignored_file(dst_absolute, "file was written previously")
return
return None
# Destination file is directory
if dst_absolute.exists() and dst_absolute.is_dir():
@ -60,7 +67,7 @@ class Organizer(Location):
shutil.rmtree(dst_absolute)
else:
PRETTY.warning(f"Could not add file {str(dst_absolute)!r}")
return
return None
# Destination file exists
if dst_absolute.exists() and dst_absolute.is_file():
@ -68,9 +75,7 @@ class Organizer(Location):
# Bail out, nothing more to do
PRETTY.ignored_file(dst_absolute, "same file contents")
self.mark(dst)
# Touch it to update the timestamp
dst_absolute.touch()
return
return dst_absolute
self.download_summary.add_modified_file(dst_absolute)
PRETTY.modified_file(dst_absolute)
@ -87,6 +92,8 @@ class Organizer(Location):
self.mark(dst)
return dst_absolute
def mark(self, path: PurePath) -> None:
"""Mark a file as used so it will not get cleaned up."""
absolute_path = self.resolve(path)