Skip to content
Open
Changes from all commits
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
20 changes: 13 additions & 7 deletions scp.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ def __exit__(self, type, value, traceback):
self.close()

def put(self, files, remote_path=b'.',
recursive=False, preserve_times=False):
recursive=False, preserve_times=False, chmod=None):
"""
Transfer files to remote host.

Expand All @@ -135,6 +135,10 @@ def put(self, files, remote_path=b'.',
@param preserve_times: preserve mtime and atime of transfered files
and directories.
@type preserve_times: bool
@param chmod: Specify a new mode for the destination files. The
default value of None will use the current mode of the source
file.
@type chmod: numeric (octal, hex, int, etc.)
"""
self.preserve_times = preserve_times
self.channel = self._open()
Expand All @@ -149,9 +153,9 @@ def put(self, files, remote_path=b'.',
files = [files]

if recursive:
self._send_recursive(files)
self._send_recursive(files, chmod)
else:
self._send_files(files)
self._send_files(files, chmod)

self.close()

Expand Down Expand Up @@ -222,12 +226,14 @@ def _read_stats(self, name):
mtime = int(stats.st_mtime)
return (mode, size, mtime, atime)

def _send_files(self, files):
def _send_files(self, files, chmod=None):
for name in files:
basename = asbytes(os.path.basename(name))
(mode, size, mtime, atime) = self._read_stats(name)
if self.preserve_times:
self._send_time(mtime, atime)
if chmod is not None:
mode = chmod
file_hdl = open(name, 'rb')

# The protocol can't handle \n in the filename.
Expand Down Expand Up @@ -273,16 +279,16 @@ def _chdir(self, from_dir, to_dir):
# now we're in our common base directory, so on
self._send_pushd(to_dir)

def _send_recursive(self, files):
def _send_recursive(self, files, chmod=None):
for base in files:
if not os.path.isdir(base):
# filename mixed into the bunch
self._send_files([base])
self._send_files([base], chmod)
continue
last_dir = asbytes(base)
for root, dirs, fls in os.walk(base):
self._chdir(last_dir, asbytes(root))
self._send_files([os.path.join(root, f) for f in fls])
self._send_files([os.path.join(root, f) for f in fls], chmod)
last_dir = asbytes(root)
# back out of the directory
while self._pushed > 0:
Expand Down