Skip to content

Commit

Permalink
Enhance scp.py for copy folders
Browse files Browse the repository at this point in the history
Enhance scp.py to keep identical behavior with native scp command:
1. When scp.py meets non-regular file, it reports error to user.
2. After reporting error, scp.py continues to copy all the remaining regular files.
  • Loading branch information
liliangnike authored Nov 30, 2023
1 parent 7f91c95 commit 3bf3bda
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions scp.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import os
import re
from socket import timeout as SocketTimeout

import logging

SCP_COMMAND = b'scp'

Expand Down Expand Up @@ -140,6 +140,7 @@ def __init__(self, transport, buff_size=16384, socket_timeout=10.0,
self.socket_timeout = socket_timeout
self.channel = None
self.preserve_times = False
self.recursive = False
if progress is not None and progress4 is not None:
raise TypeError("You may only set one of progress, progress4")
elif progress4 is not None:
Expand Down Expand Up @@ -270,6 +271,7 @@ def get(self, remote_path, local_path='',
asunicode(self._recv_dir))
rcsv = (b'', b' -r')[recursive]
prsv = (b'', b' -p')[preserve_times]
self.recursive = recursive
self.channel = self._open()
self._pushed = 0
self.channel.settimeout(self.socket_timeout)
Expand Down Expand Up @@ -428,11 +430,20 @@ def _recv_all(self):
msg = self.channel.recv(1024)
if not msg: # chan closed while receiving
break
assert msg[-1:] == b'\n'
msg = msg[:-1]

if msg[-1:] == b'\n':
msg = msg[:-1]
else:
continue

code = msg[0:1]
if code not in command:
raise SCPException(asunicode(msg[1:]))
if self.recursive:
self.channel._log(logging.ERROR, str(msg))
continue
else:
raise SCPException(asunicode(msg[1:]))

command[code](msg[1:])
# directory times can't be set until we're done writing files
self._set_dirtimes()
Expand Down

0 comments on commit 3bf3bda

Please sign in to comment.