From fd0685d9b54dcd3bcd3812a442b01e40e1fc94c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20F=C3=A4hrmann?= Date: Mon, 31 Aug 2020 21:53:18 +0200 Subject: [PATCH] [postprocessor:zip] defer zip file creation (fixes #968) don't try to create zip files on postprocessor construction, wait until directory creation during file download, --- CHANGELOG.md | 2 ++ gallery_dl/postprocessor/zip.py | 10 +++++----- gallery_dl/version.py | 2 +- test/test_postprocessor.py | 31 +++++++++++++++++-------------- 4 files changed, 25 insertions(+), 20 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b38c9c80..217c6322 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,7 @@ # Changelog +## Unreleased + ## 1.14.5 - 2020-08-30 ### Additions - [aryion] add username/password support ([#960](https://github.com/mikf/gallery-dl/issues/960)) diff --git a/gallery_dl/postprocessor/zip.py b/gallery_dl/postprocessor/zip.py index 6970e957..a6e5bc32 100644 --- a/gallery_dl/postprocessor/zip.py +++ b/gallery_dl/postprocessor/zip.py @@ -33,23 +33,23 @@ class ZipPP(PostProcessor): algorithm) algorithm = "store" + self.zfile = None self.path = job.pathfmt.realdirectory - args = (self.path[:-1] + ext, "a", - self.COMPRESSION_ALGORITHMS[algorithm], True) + self.args = (self.path[:-1] + ext, "a", + self.COMPRESSION_ALGORITHMS[algorithm], True) if options.get("mode") == "safe": self.run = self._write_safe - self.zfile = None - self.args = args else: self.run = self._write - self.zfile = zipfile.ZipFile(*args) def _write(self, pathfmt, zfile=None): # 'NameToInfo' is not officially documented, but it's available # for all supported Python versions and using it directly is a lot # faster than calling getinfo() if zfile is None: + if self.zfile is None: + self.zfile = zipfile.ZipFile(*self.args) zfile = self.zfile if pathfmt.filename not in zfile.NameToInfo: zfile.write(pathfmt.temppath, pathfmt.filename) diff --git a/gallery_dl/version.py b/gallery_dl/version.py index 9af9a43f..c92413f2 100644 --- a/gallery_dl/version.py +++ b/gallery_dl/version.py @@ -6,4 +6,4 @@ # it under the terms of the GNU General Public License version 2 as # published by the Free Software Foundation. -__version__ = "1.14.5" +__version__ = "1.15.0-dev" diff --git a/test/test_postprocessor.py b/test/test_postprocessor.py index 5da31319..ff98477f 100644 --- a/test/test_postprocessor.py +++ b/test/test_postprocessor.py @@ -342,9 +342,20 @@ class ZipTest(BasePostprocessorTest): self.assertEqual(pp.path, self.pathfmt.realdirectory) self.assertEqual(pp.run, pp._write) self.assertEqual(pp.delete, True) - self.assertFalse(hasattr(pp, "args")) - self.assertEqual(pp.zfile.compression, zipfile.ZIP_STORED) - self.assertTrue(pp.zfile.filename.endswith("/test.zip")) + self.assertEqual(pp.args, ( + pp.path[:-1] + ".zip", "a", zipfile.ZIP_STORED, True, + )) + self.assertTrue(pp.args[0].endswith("/test.zip")) + + def test_zip_safe(self): + pp = self._create({"mode": "safe"}) + self.assertEqual(pp.path, self.pathfmt.realdirectory) + self.assertEqual(pp.run, pp._write_safe) + self.assertEqual(pp.delete, True) + self.assertEqual(pp.args, ( + pp.path[:-1] + ".zip", "a", zipfile.ZIP_STORED, True, + )) + self.assertTrue(pp.args[0].endswith("/test.zip")) def test_zip_options(self): pp = self._create({ @@ -353,22 +364,13 @@ class ZipTest(BasePostprocessorTest): "extension": "cbz", }) self.assertEqual(pp.delete, False) - self.assertEqual(pp.zfile.compression, zipfile.ZIP_DEFLATED) - self.assertTrue(pp.zfile.filename.endswith("/test.cbz")) - - def test_zip_safe(self): - pp = self._create({"mode": "safe"}) - self.assertEqual(pp.delete, True) - self.assertEqual(pp.path, self.pathfmt.realdirectory) - self.assertEqual(pp.run, pp._write_safe) self.assertEqual(pp.args, ( - pp.path[:-1] + ".zip", "a", zipfile.ZIP_STORED, True, + pp.path[:-1] + ".cbz", "a", zipfile.ZIP_DEFLATED, True, )) - self.assertTrue(pp.args[0].endswith("/test.zip")) + self.assertTrue(pp.args[0].endswith("/test.cbz")) def test_zip_write(self): pp = self._create() - nti = pp.zfile.NameToInfo with tempfile.NamedTemporaryFile("w", dir=self.dir.name) as file: file.write("foobar\n") @@ -382,6 +384,7 @@ class ZipTest(BasePostprocessorTest): pp.prepare(self.pathfmt) pp.run(self.pathfmt) + nti = pp.zfile.NameToInfo self.assertEqual(len(nti), i+1) self.assertIn(name, nti)