From ab258f8b47887ace7e5997d0a3fb7bb1ca38eb64 Mon Sep 17 00:00:00 2001 From: Red_M Date: Sun, 29 Dec 2019 14:29:14 +1000 Subject: [PATCH] Make `redssh.sftp.SFTP().put_folder()` only take directories and make sure to at least try to create that initial directory. Update CI script. --- redssh/sftp.py | 38 ++++++++++++++++++++++---------------- tests/scripts/install.sh | 9 ++++++--- 2 files changed, 28 insertions(+), 19 deletions(-) diff --git a/redssh/sftp.py b/redssh/sftp.py index c459fa5..c2bbe09 100644 --- a/redssh/sftp.py +++ b/redssh/sftp.py @@ -159,6 +159,7 @@ def put_folder(self,local_path,remote_path): ''' Upload an entire folder via SFTP to the remote session. Similar to ``cp -r /files/* /target`` Also retains file permissions. + Local path must be a directory to upload, if a path to a file is provided, nothing will happen. :param local_path: The local path, on the machine where your code is running from, to upload from. :type local_path: ``str`` @@ -166,22 +167,27 @@ def put_folder(self,local_path,remote_path): :type remote_path: ``str`` ''' if self.caller.__check_for_attr__('sftp'): - for (dirpath,dirnames,filenames) in os.walk(local_path): - for dirname in sorted(dirnames): - local_dir_path = os.path.join(dirpath,dirname) - tmp_rpath = local_dir_path[len(local_path):] - if tmp_rpath.startswith(os.path.sep): - tmp_rpath = tmp_rpath[1:] - remote_dir_path = os.path.join(remote_path,tmp_rpath) - if not dirname in self.list_dir(remote_path).readdir(): - self.mkdir(remote_dir_path,os.stat(local_dir_path).st_mode) - for filename in filenames: - local_file_path = os.path.join(dirpath,filename) - remote_file_base = local_file_path[len(local_path):0-len(filename)] - if remote_file_base.startswith('/'): - remote_file_base = remote_file_base[1:] - remote_file_path = os.path.join(os.path.join(remote_path,remote_file_base),filename) - self.put_file(local_file_path,remote_file_path) + if os.path.isdir(local_path)==True: + try: + self.mkdir(remote_path,os.stat(local_path).st_mode) + except libssh2.exceptions.SFTPProtocolError: + pass + for (dirpath,dirnames,filenames) in os.walk(local_path): + for dirname in sorted(dirnames): + local_dir_path = os.path.join(dirpath,dirname) + tmp_rpath = local_dir_path[len(local_path):] + if tmp_rpath.startswith(os.path.sep): + tmp_rpath = tmp_rpath[1:] + remote_dir_path = os.path.join(remote_path,tmp_rpath) + if not dirname in self.list_dir(remote_path).readdir(): + self.mkdir(remote_dir_path,os.stat(local_dir_path).st_mode) + for filename in filenames: + local_file_path = os.path.join(dirpath,filename) + remote_file_base = local_file_path[len(local_path):0-len(filename)] + if remote_file_base.startswith('/'): + remote_file_base = remote_file_base[1:] + remote_file_path = os.path.join(os.path.join(remote_path,remote_file_base),filename) + self.put_file(local_file_path,remote_file_path) def put_file(self,local_path,remote_path): ''' diff --git a/tests/scripts/install.sh b/tests/scripts/install.sh index 3b23470..0f96751 100755 --- a/tests/scripts/install.sh +++ b/tests/scripts/install.sh @@ -2,12 +2,15 @@ CI_SYSTEM=${1} PYTHON_MAJOR_VERSION=${2} PYTHON_MINOR_VERSION=${3} -if [ ! -z PYTHON_MAJOR_VERSION ] && [ ! -z PYTHON_MINOR_VERSION ]; then - PYTHON_VERSION=${PYTHON_MAJOR_VERSION}"."${PYTHON_MINOR_VERSION} -elif [ ! -z PYTHON_MAJOR_VERSION ]; then + +if [ ! -z PYTHON_MAJOR_VERSION ]; then PYTHON_VERSION=${PYTHON_MAJOR_VERSION} fi +if [ ! -z PYTHON_MINOR_VERSION ]; then + PYTHON_VERSION=${PYTHON_VERSION}"."${PYTHON_MINOR_VERSION} +fi + apt update apt install -y make curl wget openssh-client git openssh-server cmake libssl-dev zlib1g-dev