mirror of
https://github.com/mikf/gallery-dl.git
synced 2024-11-25 04:02:32 +01:00
[postprocessor:zip] ensure target directory exists (#2758)
This commit is contained in:
parent
836402bf58
commit
9704c04172
@ -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):
|
||||
|
@ -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)
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user