From 4034cf9a1af52cb26ec551cea6af90683677e132 Mon Sep 17 00:00:00 2001 From: jvoisin Date: Sun, 13 Oct 2019 11:54:47 +0200 Subject: [PATCH] Copy file permissions Mat2 (the cli) will now copy the input file permissions to the output file. --- mat2 | 2 ++ tests/test_climat2.py | 28 ++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/mat2 b/mat2 index e67fea0..01b834b 100755 --- a/mat2 +++ b/mat2 @@ -1,6 +1,7 @@ #!/usr/bin/env python3 import os +import shutil from typing import Tuple, List, Union, Set import sys import mimetypes @@ -136,6 +137,7 @@ def clean_meta(filename: str, is_lightweight: bool, inplace: bool, sandbox: bool try: logging.debug('Cleaning %s…', filename) ret = p.remove_all() + shutil.copymode(filename, p.output_filename) if inplace is True: os.rename(p.output_filename, filename) return ret diff --git a/tests/test_climat2.py b/tests/test_climat2.py index 9d816b1..17fce82 100644 --- a/tests/test_climat2.py +++ b/tests/test_climat2.py @@ -1,6 +1,7 @@ import random import os import shutil +import stat import subprocess import unittest import glob @@ -132,6 +133,33 @@ class TestCleanMeta(unittest.TestCase): self.assertNotIn(b'Comment: Created with GIMP', stdout) os.remove('./tests/data/clean.jpg') + os.remove('./tests/data/clean.cleaned.jpg') + + +class TestCopyPermissions(unittest.TestCase): + def test_jpg_777(self): + shutil.copy('./tests/data/dirty.jpg', './tests/data/clean.jpg') + os.chmod('./tests/data/clean.jpg', 0o777) + + proc = subprocess.Popen(mat2_binary + ['--show', './tests/data/clean.jpg'], + stdout=subprocess.PIPE) + stdout, _ = proc.communicate() + self.assertIn(b'Comment: Created with GIMP', stdout) + + proc = subprocess.Popen(mat2_binary + ['./tests/data/clean.jpg'], + stdout=subprocess.PIPE) + stdout, _ = proc.communicate() + + proc = subprocess.Popen(mat2_binary + ['--show', './tests/data/clean.cleaned.jpg'], + stdout=subprocess.PIPE) + stdout, _ = proc.communicate() + self.assertNotIn(b'Comment: Created with GIMP', stdout) + + permissions = os.stat('./tests/data/clean.cleaned.jpg')[stat.ST_MODE] + self.assertEqual(permissions, 0o100777) + + os.remove('./tests/data/clean.jpg') + os.remove('./tests/data/clean.cleaned.jpg') class TestIsSupported(unittest.TestCase):