[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,
pull/982/head
Mike Fährmann 4 years ago
parent 33fe67b594
commit fd0685d9b5
No known key found for this signature in database
GPG Key ID: 5680CA389D365A88

@ -1,5 +1,7 @@
# Changelog # Changelog
## Unreleased
## 1.14.5 - 2020-08-30 ## 1.14.5 - 2020-08-30
### Additions ### Additions
- [aryion] add username/password support ([#960](https://github.com/mikf/gallery-dl/issues/960)) - [aryion] add username/password support ([#960](https://github.com/mikf/gallery-dl/issues/960))

@ -33,23 +33,23 @@ class ZipPP(PostProcessor):
algorithm) algorithm)
algorithm = "store" algorithm = "store"
self.zfile = None
self.path = job.pathfmt.realdirectory self.path = job.pathfmt.realdirectory
args = (self.path[:-1] + ext, "a", self.args = (self.path[:-1] + ext, "a",
self.COMPRESSION_ALGORITHMS[algorithm], True) self.COMPRESSION_ALGORITHMS[algorithm], True)
if options.get("mode") == "safe": if options.get("mode") == "safe":
self.run = self._write_safe self.run = self._write_safe
self.zfile = None
self.args = args
else: else:
self.run = self._write self.run = self._write
self.zfile = zipfile.ZipFile(*args)
def _write(self, pathfmt, zfile=None): def _write(self, pathfmt, zfile=None):
# 'NameToInfo' is not officially documented, but it's available # 'NameToInfo' is not officially documented, but it's available
# for all supported Python versions and using it directly is a lot # for all supported Python versions and using it directly is a lot
# faster than calling getinfo() # faster than calling getinfo()
if zfile is None: if zfile is None:
if self.zfile is None:
self.zfile = zipfile.ZipFile(*self.args)
zfile = self.zfile zfile = self.zfile
if pathfmt.filename not in zfile.NameToInfo: if pathfmt.filename not in zfile.NameToInfo:
zfile.write(pathfmt.temppath, pathfmt.filename) zfile.write(pathfmt.temppath, pathfmt.filename)

@ -6,4 +6,4 @@
# it under the terms of the GNU General Public License version 2 as # it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation. # published by the Free Software Foundation.
__version__ = "1.14.5" __version__ = "1.15.0-dev"

@ -342,9 +342,20 @@ class ZipTest(BasePostprocessorTest):
self.assertEqual(pp.path, self.pathfmt.realdirectory) self.assertEqual(pp.path, self.pathfmt.realdirectory)
self.assertEqual(pp.run, pp._write) self.assertEqual(pp.run, pp._write)
self.assertEqual(pp.delete, True) self.assertEqual(pp.delete, True)
self.assertFalse(hasattr(pp, "args")) self.assertEqual(pp.args, (
self.assertEqual(pp.zfile.compression, zipfile.ZIP_STORED) pp.path[:-1] + ".zip", "a", zipfile.ZIP_STORED, True,
self.assertTrue(pp.zfile.filename.endswith("/test.zip")) ))
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): def test_zip_options(self):
pp = self._create({ pp = self._create({
@ -353,22 +364,13 @@ class ZipTest(BasePostprocessorTest):
"extension": "cbz", "extension": "cbz",
}) })
self.assertEqual(pp.delete, False) 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, ( 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): def test_zip_write(self):
pp = self._create() pp = self._create()
nti = pp.zfile.NameToInfo
with tempfile.NamedTemporaryFile("w", dir=self.dir.name) as file: with tempfile.NamedTemporaryFile("w", dir=self.dir.name) as file:
file.write("foobar\n") file.write("foobar\n")
@ -382,6 +384,7 @@ class ZipTest(BasePostprocessorTest):
pp.prepare(self.pathfmt) pp.prepare(self.pathfmt)
pp.run(self.pathfmt) pp.run(self.pathfmt)
nti = pp.zfile.NameToInfo
self.assertEqual(len(nti), i+1) self.assertEqual(len(nti), i+1)
self.assertIn(name, nti) self.assertIn(name, nti)

Loading…
Cancel
Save