Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding support for local to remote operations in shutil.py::copytree #227

Merged
merged 2 commits into from
Jul 19, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/smbclient/_os.py
Original file line number Diff line number Diff line change
Expand Up @@ -1172,6 +1172,24 @@ def _set_basic_information(
set_info(transaction, basic_info)


class LocalDirEntry:
"""Mimics the structure of SMBDirEntry, but instead encapsulates a directory/file on a local filesystem"""

def __init__(self, path) -> None:
self.path = path

@property
def name(self):
"""The entry's base filename, relative to the os.listdir() path argument."""
return os.path.basename(self.path)

def is_symlink(self):
return os.path.islink(self.path)

def is_dir(self):
return os.path.isdir(self.path)


class SMBDirEntry:
def __init__(self, raw, dir_info, connection_cache=None):
self._smb_raw = raw
Expand Down
12 changes: 8 additions & 4 deletions src/smbclient/shutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import sys

from smbclient._io import SMBFileTransaction, SMBRawIO, query_info, set_info
from smbclient._os import SMBDirEntry
from smbclient._os import LocalDirEntry, SMBDirEntry
from smbclient._os import copyfile as smbclient_copyfile
from smbclient._os import (
is_remote_path,
Expand Down Expand Up @@ -281,8 +281,6 @@ def copytree(
source path and the destination path as arguments. By default copy() is used, but any function that supports the
same signature (like copy()) can be used.

In this current form, copytree() only supports remote to remote copies over SMB, or remote to local copies.

:param src: The source directory to copy.
:param dst: The destination directory to copy to.
:param symlinks: Whether to attempt to copy a symlink from the source tree to the dest tree, if False the symlink
Expand All @@ -295,7 +293,10 @@ def copytree(
:param kwargs: Common arguments used to build the SMB Session for any UNC paths.
:return: The dst path.
"""
dir_entries = list(scandir(src, **kwargs))
if is_remote_path(src):
dir_entries = list(scandir(src, **kwargs))
else:
dir_entries = [LocalDirEntry(os.path.join(src, result)) for result in os.listdir(src)]

if is_remote_path(dst):
makedirs(dst, exist_ok=dirs_exist_ok, **kwargs)
Expand All @@ -316,6 +317,9 @@ def copytree(

try:
if dir_entry.is_symlink():
if not isinstance(dir_entry, SMBDirEntry):
raise AssertionError("copytree doesn't yet support symlinks for local to remote operations")

link_target = readlink(src_path, **kwargs)
if symlinks:
symlink(link_target, dst_path, **kwargs)
Expand Down
27 changes: 27 additions & 0 deletions tests/test_smbclient_shutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -1176,6 +1176,33 @@ def test_copytree_with_local_dst(smb_share, tmp_path):
assert fd.read() == "file3.txt"


def test_copytree_with_local_src(smb_share, tmp_path):
src_dirname = str(tmp_path / "source")
dst_dirname = "%s\\target" % smb_share

os.makedirs(os.path.join(src_dirname, "dir1", "subdir1"))
with open(os.path.join(src_dirname, "file1.txt"), mode="w") as fd:
fd.write("file1.txt")
with open(os.path.join(src_dirname, "dir1", "file2.txt"), mode="w") as fd:
fd.write("file2.txt")
with open(os.path.join(src_dirname, "dir1", "subdir1", "file3.txt"), mode="w") as fd:
fd.write("file3.txt")

actual = copytree(src_dirname, dst_dirname)
assert actual == dst_dirname

assert sorted(list(listdir(dst_dirname))) == ["dir1", "file1.txt"]
assert sorted(list(listdir("%s\\dir1" % dst_dirname))) == ["file2.txt", "subdir1"]
assert sorted(list(listdir("%s\\dir1\\subdir1" % dst_dirname))) == ["file3.txt"]

with open_file("%s\\file1.txt" % dst_dirname) as fd:
assert fd.read() == "file1.txt"
with open_file("%s\\dir1\\file2.txt" % dst_dirname) as fd:
assert fd.read() == "file2.txt"
with open_file("%s\\dir1\\subdir1\\file3.txt" % dst_dirname) as fd:
assert fd.read() == "file3.txt"


@pytest.mark.skipif(
os.name != "nt" and not os.environ.get("SMB_FORCE", False), reason="Samba does not update timestamps"
)
Expand Down