From 9704c041725168485add3d1aa2db8fa7096039ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20F=C3=A4hrmann?= Date: Thu, 14 Jul 2022 11:55:39 +0200 Subject: [PATCH] [postprocessor:zip] ensure target directory exists (#2758) --- gallery_dl/postprocessor/zip.py | 31 ++++++++++++++++++++----------- test/test_postprocessor.py | 18 +++++++++--------- 2 files changed, 29 insertions(+), 20 deletions(-) diff --git a/gallery_dl/postprocessor/zip.py b/gallery_dl/postprocessor/zip.py index 1c4bd03b..ff97add2 100644 --- a/gallery_dl/postprocessor/zip.py +++ b/gallery_dl/postprocessor/zip.py @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- -# Copyright 2018-2021 Mike Fährmann +# Copyright 2018-2022 Mike Fährmann # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License version 2 as @@ -11,6 +11,7 @@ from .common import PostProcessor from .. import util import zipfile +import os class ZipPP(PostProcessor): @@ -34,30 +35,38 @@ class ZipPP(PostProcessor): algorithm = "store" self.zfile = None - self.path = job.pathfmt.realdirectory - self.args = (self.path[:-1] + ext, "a", + self.path = job.pathfmt.realdirectory[:-1] + self.args = (self.path + ext, "a", self.COMPRESSION_ALGORITHMS[algorithm], True) job.register_hooks({ - "file": - self.write_safe if options.get("mode") == "safe" else self.write, + "file": (self.write_safe if options.get("mode") == "safe" else + self.write_fast), }, options) job.hooks["finalize"].append(self.finalize) - def write(self, pathfmt, zfile=None): + def open(self): + try: + return zipfile.ZipFile(*self.args) + except FileNotFoundError: + os.makedirs(os.path.dirname(self.path)) + return zipfile.ZipFile(*self.args) + + def write(self, pathfmt, zfile): # '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) pathfmt.delete = self.delete + def write_fast(self, pathfmt): + if self.zfile is None: + self.zfile = self.open() + self.write(pathfmt, self.zfile) + def write_safe(self, pathfmt): - with zipfile.ZipFile(*self.args) as zfile: + with self.open() as zfile: self.write(pathfmt, zfile) def finalize(self, pathfmt, status): diff --git a/test/test_postprocessor.py b/test/test_postprocessor.py index fbe6d76b..7a216bbc 100644 --- a/test/test_postprocessor.py +++ b/test/test_postprocessor.py @@ -375,21 +375,21 @@ class ZipTest(BasePostprocessorTest): def test_zip_default(self): pp = self._create() - self.assertEqual(self.job.hooks["file"][0], pp.write) - self.assertEqual(pp.path, self.pathfmt.realdirectory) + self.assertEqual(self.job.hooks["file"][0], pp.write_fast) + self.assertEqual(pp.path, self.pathfmt.realdirectory[:-1]) self.assertEqual(pp.delete, True) self.assertEqual(pp.args, ( - pp.path[:-1] + ".zip", "a", zipfile.ZIP_STORED, True, + pp.path + ".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(self.job.hooks["file"][0], pp.write_safe) - self.assertEqual(pp.path, self.pathfmt.realdirectory) + self.assertEqual(pp.path, self.pathfmt.realdirectory[:-1]) self.assertEqual(pp.delete, True) self.assertEqual(pp.args, ( - pp.path[:-1] + ".zip", "a", zipfile.ZIP_STORED, True, + pp.path + ".zip", "a", zipfile.ZIP_STORED, True, )) self.assertTrue(pp.args[0].endswith("/test.zip")) @@ -401,7 +401,7 @@ class ZipTest(BasePostprocessorTest): }) self.assertEqual(pp.delete, False) self.assertEqual(pp.args, ( - pp.path[:-1] + ".cbz", "a", zipfile.ZIP_DEFLATED, True, + pp.path + ".cbz", "a", zipfile.ZIP_DEFLATED, True, )) self.assertTrue(pp.args[0].endswith("/test.cbz")) @@ -440,9 +440,9 @@ class ZipTest(BasePostprocessorTest): with zipfile.ZipFile(pp.zfile.filename) as file: nti = file.NameToInfo self.assertEqual(len(pp.zfile.NameToInfo), 3) - self.assertIn("file0.ext", pp.zfile.NameToInfo) - self.assertIn("file1.ext", pp.zfile.NameToInfo) - self.assertIn("file2.ext", pp.zfile.NameToInfo) + self.assertIn("file0.ext", nti) + self.assertIn("file1.ext", nti) + self.assertIn("file2.ext", nti) os.unlink(pp.zfile.filename)