Skip to content

Commit

Permalink
Copy permissions of converted files
Browse files Browse the repository at this point in the history
  • Loading branch information
17451k committed Jan 12, 2021
1 parent 3a43c24 commit 2a45ea6
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
2 changes: 1 addition & 1 deletion clade/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,7 @@ def add_file_to_storage(self, file, storage_filename=None, encoding=None):
option
"""

self.Storage.add_file(file, storage_filename=storage_filename, encoding=encoding)
return self.Storage.add_file(file, storage_filename=storage_filename, encoding=encoding)

def get_storage_path(self, path):
"""Get path to the file or directory from the storage."""
Expand Down
9 changes: 8 additions & 1 deletion clade/extensions/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def add_file(self, filename, storage_filename=None, encoding=None):
else self.extensions["Path"].normalize_abs_path(filename)
)

dst = self.work_dir + os.sep + storage_filename
dst = os.path.normpath(self.work_dir + os.sep + storage_filename)

if self.__path_exists(dst):
return
Expand All @@ -89,6 +89,8 @@ def add_file(self, filename, storage_filename=None, encoding=None):
except shutil.SameFileError:
pass

return dst

@functools.lru_cache(maxsize=30000)
def __path_exists(self, path):
return os.path.exists(path)
Expand Down Expand Up @@ -145,6 +147,11 @@ def __copy_file(self, filename, dst, encoding=None):
content_bytes = content_bytes.replace(b"\r\n", b"\n")
f.write(content_bytes)

try:
shutil.copymode(filename, f.name)
except Exception:
self.warning("Couldn't set permissions for {!r}".format(filename))

try:
os.replace(f.name, dst)
except OSError:
Expand Down
11 changes: 10 additions & 1 deletion tests/test_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import os
import pytest
import shutil
import stat
import unittest.mock

from clade import Clade
Expand All @@ -26,14 +27,15 @@
def test_storage(tmpdir):
c = Clade(tmpdir)

c.add_file_to_storage(__file__)
returned_storage_path = c.add_file_to_storage(__file__)
c.add_file_to_storage("do_not_exist.c")

storage_path = c.get_storage_path(__file__)

assert storage_path
assert os.path.exists(storage_path)
assert storage_path.startswith(c.storage_dir)
assert returned_storage_path == storage_path

# Test possible race condition
with unittest.mock.patch("shutil.copyfile") as copyfile_mock:
Expand Down Expand Up @@ -78,3 +80,10 @@ def test_storage_encoding(tmpdir, encoding):
fh.write(bstr)

c.add_file_to_storage(test_file, encoding=encoding)


@pytest.mark.parametrize("convert", [False, True])
def test_storage_permissions(tmpdir, convert):
c = Clade(tmpdir, conf={"Storage.convert_to_utf8": convert})
storage_path = c.add_file_to_storage(__file__)
assert os.stat(storage_path)[stat.ST_MODE] == os.stat(__file__)[stat.ST_MODE]

0 comments on commit 2a45ea6

Please sign in to comment.