From 34152fbe54729a7082ff8696c12130ed19aac29e Mon Sep 17 00:00:00 2001 From: I-Al-Istannen Date: Mon, 13 Jul 2020 13:12:01 +0200 Subject: [PATCH] Set mtime and atime to ILIAS dates where possible --- PFERD/ilias/downloader.py | 12 +++++++++++- PFERD/organizer.py | 23 +++++++++++++++-------- 2 files changed, 26 insertions(+), 9 deletions(-) diff --git a/PFERD/ilias/downloader.py b/PFERD/ilias/downloader.py index dca9435..f685f7a 100644 --- a/PFERD/ilias/downloader.py +++ b/PFERD/ilias/downloader.py @@ -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() diff --git a/PFERD/organizer.py b/PFERD/organizer.py index 67bf83d..2fe4316 100644 --- a/PFERD/organizer.py +++ b/PFERD/organizer.py @@ -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)