From 14cd430bac66a9361d390b380bc27509425e3a73 Mon Sep 17 00:00:00 2001 From: Andrew Rogl Date: Sat, 2 Jul 2022 15:18:36 +1000 Subject: [PATCH 1/4] Preserve mtimes from archives #4392 --- beets/importer.py | 13 +++++++++++++ docs/changelog.rst | 2 ++ 2 files changed, 15 insertions(+) diff --git a/beets/importer.py b/beets/importer.py index 561cedd2c..6e0d62367 100644 --- a/beets/importer.py +++ b/beets/importer.py @@ -1090,6 +1090,19 @@ class ArchiveImportTask(SentinelImportTask): archive = handler_class(util.py3_path(self.toppath), mode='r') try: archive.extractall(extract_to) + + # From here: + # https://stackoverflow.com/questions/9813243/extract-files-from-zip-file-and-retain-mod-date + # fixing #4392 + + for f in archive.infolist(): + # path to this extracted f-item + fullpath = os.path.join(extract_to, f.filename) + # still need to adjust the dt o/w item will have the current dt + date_time = time.mktime(f.date_time + (0, 0, -1)) + # update date_time + os.utime(fullpath, (date_time, date_time)) + finally: archive.close() self.extracted = True diff --git a/docs/changelog.rst b/docs/changelog.rst index 3c61b3bf8..7a7fe15e5 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -32,6 +32,8 @@ New features: * :ref:`import-options`: Add support for re-running the importer on paths in log files that were created with the ``-l`` (or ``--logfile``) argument. :bug:`4379` :bug:`4387` +* Preserve mtimes from archives + :bug:`4392` Bug fixes: From 4338ef3e3531039767d0c0db75151afb724c76d9 Mon Sep 17 00:00:00 2001 From: Andrew Rogl Date: Sat, 25 Mar 2023 13:36:27 +1000 Subject: [PATCH 2/4] Address comments from @sampsyo --- beets/importer.py | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/beets/importer.py b/beets/importer.py index 6e0d62367..5c65b57c1 100644 --- a/beets/importer.py +++ b/beets/importer.py @@ -1091,16 +1091,19 @@ class ArchiveImportTask(SentinelImportTask): try: archive.extractall(extract_to) - # From here: - # https://stackoverflow.com/questions/9813243/extract-files-from-zip-file-and-retain-mod-date - # fixing #4392 + # Adjust the files' mtimes to match the information from the archive. Inspired by: + # https://stackoverflow.com/q/9813243 for f in archive.infolist(): - # path to this extracted f-item - fullpath = os.path.join(extract_to, f.filename) # still need to adjust the dt o/w item will have the current dt - date_time = time.mktime(f.date_time + (0, 0, -1)) # update date_time + # Can you give a clarification why you add (0, 0, -1) to the date_time? + # Is the current a second off? + # (0, 0, -1) is added because time.mktime expects a 9-element tuple. + # The -1 indicates that the DST flag is unknown. + + date_time = time.mktime(f.date_time + (0, 0, -1)) + fullpath = os.path.join(extract_to, f.filename) os.utime(fullpath, (date_time, date_time)) finally: From e5f2e6d37bb5fc888013f19f4900babc0b500607 Mon Sep 17 00:00:00 2001 From: Andrew Rogl Date: Sun, 26 Mar 2023 18:35:29 +1000 Subject: [PATCH 3/4] Updated comments to attempt to be more self contained --- beets/importer.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/beets/importer.py b/beets/importer.py index 5c65b57c1..c8542ba82 100644 --- a/beets/importer.py +++ b/beets/importer.py @@ -1095,11 +1095,10 @@ class ArchiveImportTask(SentinelImportTask): # https://stackoverflow.com/q/9813243 for f in archive.infolist(): - # still need to adjust the dt o/w item will have the current dt - # update date_time - # Can you give a clarification why you add (0, 0, -1) to the date_time? - # Is the current a second off? - # (0, 0, -1) is added because time.mktime expects a 9-element tuple. + # The date_time will need to adjusted otherwise + # the item will have the current date_time of extraction. + # The (0, 0, -1) is added to date_time because the + # function time.mktime expects a 9-element tuple. # The -1 indicates that the DST flag is unknown. date_time = time.mktime(f.date_time + (0, 0, -1)) From 6777b49bcff3f12013dc78ec0845483ff0c787f9 Mon Sep 17 00:00:00 2001 From: Adrian Sampson Date: Sat, 1 Apr 2023 15:46:02 -0700 Subject: [PATCH 4/4] Appease flake8 --- beets/importer.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/beets/importer.py b/beets/importer.py index 4b448c557..96d1f17df 100644 --- a/beets/importer.py +++ b/beets/importer.py @@ -1123,16 +1123,14 @@ class ArchiveImportTask(SentinelImportTask): try: archive.extractall(extract_to) - # Adjust the files' mtimes to match the information from the archive. Inspired by: - # https://stackoverflow.com/q/9813243 - + # Adjust the files' mtimes to match the information from the + # archive. Inspired by: https://stackoverflow.com/q/9813243 for f in archive.infolist(): # The date_time will need to adjusted otherwise # the item will have the current date_time of extraction. # The (0, 0, -1) is added to date_time because the # function time.mktime expects a 9-element tuple. # The -1 indicates that the DST flag is unknown. - date_time = time.mktime(f.date_time + (0, 0, -1)) fullpath = os.path.join(extract_to, f.filename) os.utime(fullpath, (date_time, date_time))