From 45de6e7b6de7993ab5af6d09d77a663151c056a7 Mon Sep 17 00:00:00 2001 From: Simon Conseil Date: Fri, 4 Oct 2019 00:01:41 -0300 Subject: [PATCH] Make sure that read-only files can be copied (fix #375) --- sigal/utils.py | 8 +++++++- tests/test_utils.py | 10 ++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/sigal/utils.py b/sigal/utils.py index 1b48192..c828856 100644 --- a/sigal/utils.py +++ b/sigal/utils.py @@ -50,7 +50,13 @@ def copy(src, dst, symlink=False, rellink=False): if rellink: # relative symlink from dst func(os.path.relpath(src, os.path.dirname(dst)), dst) else: - func(src, dst) + try: + func(src, dst) + except PermissionError: + # this can happen if the file is not writable, so we try to remove + # it first + os.remove(dst) + func(src, dst) def check_or_create_dir(path): diff --git a/tests/test_utils.py b/tests/test_utils.py index 3795679..693e16c 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -1,4 +1,5 @@ import os +from pathlib import Path from sigal import utils CURRENT_DIR = os.path.dirname(__file__) @@ -33,6 +34,15 @@ def test_copy(tmpdir): assert os.path.join(os.path.dirname(CURRENT_DIR)), os.readlink(dst) == src # get absolute path of the current dir plus the relative dir + src = str(tmpdir.join('foo.txt')) + dst = str(tmpdir.join('bar.txt')) + p = Path(src) + p.touch() + p.chmod(0o444) + utils.copy(src, dst) + utils.copy(src, dst) + + def test_check_or_create_dir(tmpdir): path = str(tmpdir.join('new_directory')) utils.check_or_create_dir(path)