Skip to content

Commit

Permalink
Merge pull request #3 from paulo-coutinho/fix-copy-dir
Browse files Browse the repository at this point in the history
fix copy dir
  • Loading branch information
Paulo Coutinho authored Jan 7, 2022
2 parents b9b15a9 + 3fbdade commit 874d774
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 33 deletions.
2 changes: 1 addition & 1 deletion pygemstones/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.0.7"
__version__ = "0.0.8"
60 changes: 30 additions & 30 deletions pygemstones/io/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -536,43 +536,43 @@ def copy_dir(src, dst, symlinks=False, ignore=None, ignore_file=None):
s = os.path.join(src, item)
d = os.path.join(dst, item)

if os.path.isdir(s):
copy_dir(s, d, symlinks, ignore, ignore_file)
else:
can_copy = True

if os.path.islink(s):
if symlinks:
if ignore_file is None:
ignored_file = False
else:
ignored_file = ignore_file(s)

if not ignored_file:
if os.path.lexists(d):
os.remove(d)

os.symlink(os.readlink(s), d)

if hasattr(os, "lchmod"):
st = os.lstat(s)
mode = stat.S_IMODE(st.st_mode)
os.lchmod(d, mode)
else:
# ignore this symlink
can_copy = False
can_copy = True

if can_copy:
if os.path.islink(s):
if symlinks:
if ignore_file is None:
ignored_file = False
else:
ignored_file = ignore_file(s)

if not ignored_file:
try:
shutil.copy2(s, d)
except IOError:
pass
if os.path.lexists(d):
os.remove(d)

os.symlink(os.readlink(s), d)

if hasattr(os, "lchmod"):
st = os.lstat(s)
mode = stat.S_IMODE(st.st_mode)
os.lchmod(d, mode)
else:
# ignore this symlink
can_copy = False
elif os.path.isdir(s):
copy_dir(s, d, symlinks, ignore, ignore_file)
can_copy = False

if can_copy:
if ignore_file is None:
ignored_file = False
else:
ignored_file = ignore_file(s)

if not ignored_file:
try:
shutil.copy2(s, d)
except IOError:
pass


# -----------------------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[tool.poetry]
description = "Python package that group a lot of classes and functions that help software development."
name = "pygemstones"
version = "0.0.7"
version = "0.0.8"

authors = ["Paulo Coutinho <[email protected]>"]
license = "MIT"
Expand Down
28 changes: 28 additions & 0 deletions tests/io/test_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,34 @@ def test_copy_dir_with_symbolic_links(tmp_path):
assert len(file_list) == 3


# -----------------------------------------------------------------------------
def test_copy_dir_with_symbolic_links_as_directory(tmp_path):
target_path = os.path.join(tmp_path, "new-dir")
dst_path = os.path.join(tmp_path, "dst-dir")

f.set_file_content(os.path.join(target_path, "file1.txt"), "test")
f.set_file_content(os.path.join(target_path, "file2.txt"), "test")
f.create_dir(os.path.join(target_path, "my-folder"))
os.symlink(
os.path.join(target_path, "my-folder"),
os.path.join(target_path, "my-symbolic-folder"),
)

f.copy_dir(target_path, dst_path, symlinks=True)

path_is_link = os.path.islink(os.path.join(dst_path, "my-symbolic-folder"))
path_is_dir = os.path.isdir(os.path.join(dst_path, "my-symbolic-folder"))

assert path_is_link
assert path_is_dir

path_is_link = os.path.islink(os.path.join(dst_path, "my-folder"))
path_is_dir = os.path.isdir(os.path.join(dst_path, "my-folder"))

assert path_is_dir
assert path_is_link == False


# -----------------------------------------------------------------------------
def test_copy_dir_with_ignore_and_symbolic_links(tmp_path):
target_path = os.path.join(tmp_path, "new-dir")
Expand Down
2 changes: 1 addition & 1 deletion tests/test_pygems.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@

# -----------------------------------------------------------------------------
def test_version():
assert __version__ == "0.0.7"
assert __version__ == "0.0.8"

0 comments on commit 874d774

Please sign in to comment.