[postprocessor:zip] ensure target directory exists (#2758)
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
# -*- coding: utf-8 -*-
|
# -*- 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
|
# 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
|
# it under the terms of the GNU General Public License version 2 as
|
||||||
@@ -11,6 +11,7 @@
|
|||||||
from .common import PostProcessor
|
from .common import PostProcessor
|
||||||
from .. import util
|
from .. import util
|
||||||
import zipfile
|
import zipfile
|
||||||
|
import os
|
||||||
|
|
||||||
|
|
||||||
class ZipPP(PostProcessor):
|
class ZipPP(PostProcessor):
|
||||||
@@ -34,30 +35,38 @@ class ZipPP(PostProcessor):
|
|||||||
algorithm = "store"
|
algorithm = "store"
|
||||||
|
|
||||||
self.zfile = None
|
self.zfile = None
|
||||||
self.path = job.pathfmt.realdirectory
|
self.path = job.pathfmt.realdirectory[:-1]
|
||||||
self.args = (self.path[:-1] + ext, "a",
|
self.args = (self.path + ext, "a",
|
||||||
self.COMPRESSION_ALGORITHMS[algorithm], True)
|
self.COMPRESSION_ALGORITHMS[algorithm], True)
|
||||||
|
|
||||||
job.register_hooks({
|
job.register_hooks({
|
||||||
"file":
|
"file": (self.write_safe if options.get("mode") == "safe" else
|
||||||
self.write_safe if options.get("mode") == "safe" else self.write,
|
self.write_fast),
|
||||||
}, options)
|
}, options)
|
||||||
job.hooks["finalize"].append(self.finalize)
|
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
|
# '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 self.zfile is None:
|
|
||||||
self.zfile = zipfile.ZipFile(*self.args)
|
|
||||||
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)
|
||||||
pathfmt.delete = self.delete
|
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):
|
def write_safe(self, pathfmt):
|
||||||
with zipfile.ZipFile(*self.args) as zfile:
|
with self.open() as zfile:
|
||||||
self.write(pathfmt, zfile)
|
self.write(pathfmt, zfile)
|
||||||
|
|
||||||
def finalize(self, pathfmt, status):
|
def finalize(self, pathfmt, status):
|
||||||
|
|||||||
@@ -375,21 +375,21 @@ class ZipTest(BasePostprocessorTest):
|
|||||||
|
|
||||||
def test_zip_default(self):
|
def test_zip_default(self):
|
||||||
pp = self._create()
|
pp = self._create()
|
||||||
self.assertEqual(self.job.hooks["file"][0], pp.write)
|
self.assertEqual(self.job.hooks["file"][0], pp.write_fast)
|
||||||
self.assertEqual(pp.path, self.pathfmt.realdirectory)
|
self.assertEqual(pp.path, self.pathfmt.realdirectory[:-1])
|
||||||
self.assertEqual(pp.delete, True)
|
self.assertEqual(pp.delete, True)
|
||||||
self.assertEqual(pp.args, (
|
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"))
|
self.assertTrue(pp.args[0].endswith("/test.zip"))
|
||||||
|
|
||||||
def test_zip_safe(self):
|
def test_zip_safe(self):
|
||||||
pp = self._create({"mode": "safe"})
|
pp = self._create({"mode": "safe"})
|
||||||
self.assertEqual(self.job.hooks["file"][0], pp.write_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.delete, True)
|
||||||
self.assertEqual(pp.args, (
|
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"))
|
self.assertTrue(pp.args[0].endswith("/test.zip"))
|
||||||
|
|
||||||
@@ -401,7 +401,7 @@ class ZipTest(BasePostprocessorTest):
|
|||||||
})
|
})
|
||||||
self.assertEqual(pp.delete, False)
|
self.assertEqual(pp.delete, False)
|
||||||
self.assertEqual(pp.args, (
|
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"))
|
self.assertTrue(pp.args[0].endswith("/test.cbz"))
|
||||||
|
|
||||||
@@ -440,9 +440,9 @@ class ZipTest(BasePostprocessorTest):
|
|||||||
with zipfile.ZipFile(pp.zfile.filename) as file:
|
with zipfile.ZipFile(pp.zfile.filename) as file:
|
||||||
nti = file.NameToInfo
|
nti = file.NameToInfo
|
||||||
self.assertEqual(len(pp.zfile.NameToInfo), 3)
|
self.assertEqual(len(pp.zfile.NameToInfo), 3)
|
||||||
self.assertIn("file0.ext", pp.zfile.NameToInfo)
|
self.assertIn("file0.ext", nti)
|
||||||
self.assertIn("file1.ext", pp.zfile.NameToInfo)
|
self.assertIn("file1.ext", nti)
|
||||||
self.assertIn("file2.ext", pp.zfile.NameToInfo)
|
self.assertIn("file2.ext", nti)
|
||||||
|
|
||||||
os.unlink(pp.zfile.filename)
|
os.unlink(pp.zfile.filename)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user