Skip to content

Commit

Permalink
[postprocessor:zip] defer zip file creation (fixes #968)
Browse files Browse the repository at this point in the history
don't try to create zip files on postprocessor construction,
wait until directory creation during file download,
  • Loading branch information
mikf committed Aug 31, 2020
1 parent 33fe67b commit fd0685d
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 20 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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))
Expand Down
10 changes: 5 additions & 5 deletions gallery_dl/postprocessor/zip.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,23 +33,23 @@ def __init__(self, job, options):
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)
Expand Down
2 changes: 1 addition & 1 deletion gallery_dl/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
31 changes: 17 additions & 14 deletions test/test_postprocessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -342,9 +342,20 @@ def test_zip_default(self):
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({
Expand All @@ -353,22 +364,13 @@ def test_zip_options(self):
"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")
Expand All @@ -382,6 +384,7 @@ def test_zip_write(self):
pp.prepare(self.pathfmt)
pp.run(self.pathfmt)

nti = pp.zfile.NameToInfo
self.assertEqual(len(nti), i+1)
self.assertIn(name, nti)

Expand Down

0 comments on commit fd0685d

Please sign in to comment.