mirror of
https://github.com/python-pillow/Pillow.git
synced 2025-05-02 23:13:40 +03:00
Simplify temporary directory cleanup
Co-Authored-By: Jon Dufresne <jon.dufresne@gmail.com>
This commit is contained in:
parent
84e53e3757
commit
3a34081db5
|
@ -162,13 +162,10 @@ class TestFilePdf(PillowTestCase):
|
||||||
|
|
||||||
def test_pdf_append_fails_on_nonexistent_file(self):
|
def test_pdf_append_fails_on_nonexistent_file(self):
|
||||||
im = hopper("RGB")
|
im = hopper("RGB")
|
||||||
temp_dir = tempfile.mkdtemp()
|
with tempfile.TemporaryDirectory() as temp_dir:
|
||||||
try:
|
|
||||||
self.assertRaises(
|
self.assertRaises(
|
||||||
IOError, im.save, os.path.join(temp_dir, "nonexistent.pdf"), append=True
|
IOError, im.save, os.path.join(temp_dir, "nonexistent.pdf"), append=True
|
||||||
)
|
)
|
||||||
finally:
|
|
||||||
os.rmdir(temp_dir)
|
|
||||||
|
|
||||||
def check_pdf_pages_consistency(self, pdf):
|
def check_pdf_pages_consistency(self, pdf):
|
||||||
pages_info = pdf.read_indirect(pdf.pages_ref)
|
pages_info = pdf.read_indirect(pdf.pages_ref)
|
||||||
|
|
|
@ -314,41 +314,40 @@ def _save(im, fp, filename):
|
||||||
fp.flush()
|
fp.flush()
|
||||||
|
|
||||||
# create the temporary set of pngs
|
# create the temporary set of pngs
|
||||||
iconset = tempfile.mkdtemp(".iconset")
|
with tempfile.TemporaryDirectory(".iconset") as iconset:
|
||||||
provided_images = {im.width: im for im in im.encoderinfo.get("append_images", [])}
|
provided_images = {
|
||||||
last_w = None
|
im.width: im for im in im.encoderinfo.get("append_images", [])
|
||||||
second_path = None
|
}
|
||||||
for w in [16, 32, 128, 256, 512]:
|
last_w = None
|
||||||
prefix = "icon_{}x{}".format(w, w)
|
second_path = None
|
||||||
|
for w in [16, 32, 128, 256, 512]:
|
||||||
|
prefix = "icon_{}x{}".format(w, w)
|
||||||
|
|
||||||
first_path = os.path.join(iconset, prefix + ".png")
|
first_path = os.path.join(iconset, prefix + ".png")
|
||||||
if last_w == w:
|
if last_w == w:
|
||||||
shutil.copyfile(second_path, first_path)
|
shutil.copyfile(second_path, first_path)
|
||||||
else:
|
else:
|
||||||
im_w = provided_images.get(w, im.resize((w, w), Image.LANCZOS))
|
im_w = provided_images.get(w, im.resize((w, w), Image.LANCZOS))
|
||||||
im_w.save(first_path)
|
im_w.save(first_path)
|
||||||
|
|
||||||
second_path = os.path.join(iconset, prefix + "@2x.png")
|
second_path = os.path.join(iconset, prefix + "@2x.png")
|
||||||
im_w2 = provided_images.get(w * 2, im.resize((w * 2, w * 2), Image.LANCZOS))
|
im_w2 = provided_images.get(w * 2, im.resize((w * 2, w * 2), Image.LANCZOS))
|
||||||
im_w2.save(second_path)
|
im_w2.save(second_path)
|
||||||
last_w = w * 2
|
last_w = w * 2
|
||||||
|
|
||||||
# iconutil -c icns -o {} {}
|
# iconutil -c icns -o {} {}
|
||||||
|
|
||||||
convert_cmd = ["iconutil", "-c", "icns", "-o", filename, iconset]
|
convert_cmd = ["iconutil", "-c", "icns", "-o", filename, iconset]
|
||||||
convert_proc = subprocess.Popen(
|
convert_proc = subprocess.Popen(
|
||||||
convert_cmd, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL
|
convert_cmd, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL
|
||||||
)
|
)
|
||||||
|
|
||||||
convert_proc.stdout.close()
|
convert_proc.stdout.close()
|
||||||
|
|
||||||
retcode = convert_proc.wait()
|
retcode = convert_proc.wait()
|
||||||
|
|
||||||
# remove the temporary files
|
if retcode:
|
||||||
shutil.rmtree(iconset)
|
raise subprocess.CalledProcessError(retcode, convert_cmd)
|
||||||
|
|
||||||
if retcode:
|
|
||||||
raise subprocess.CalledProcessError(retcode, convert_cmd)
|
|
||||||
|
|
||||||
|
|
||||||
Image.register_open(IcnsImageFile.format, IcnsImageFile, lambda x: x[:4] == b"icns")
|
Image.register_open(IcnsImageFile.format, IcnsImageFile, lambda x: x[:4] == b"icns")
|
||||||
|
|
Loading…
Reference in New Issue
Block a user