From 0d561cb2531d293ff8f4cee2f73a242e56547139 Mon Sep 17 00:00:00 2001 From: Samo Penic <samo.penic@gmail.com> Date: Fri, 20 Jan 2017 21:03:21 +0000 Subject: [PATCH] Now commiting correctly. Really. I promise --- trisurf/Remote.py | 32 ++++++++++++++++++++++++++++++-- 1 files changed, 30 insertions(+), 2 deletions(-) diff --git a/trisurf/Remote.py b/trisurf/Remote.py index 8da08be..977b96a 100644 --- a/trisurf/Remote.py +++ b/trisurf/Remote.py @@ -1,6 +1,5 @@ import paramiko - - +import os.path class Connection: def __init__(self, hostname, port=22, username=None, password=None): @@ -70,4 +69,33 @@ sftp.mkdir(directory) sftp.close() + def mkdir_p(self,sftp, remote_directory): + """Change to this directory, recursively making new folders if needed. + Returns True if any folders were created. Recursive algorithm.""" + if remote_directory == '/': + # absolute path so change directory to root + sftp.chdir('/') + return + if remote_directory == '': + # top-level relative directory must exist + return + try: + sftp.chdir(remote_directory) # sub-directory exists + except IOError: + dirname, basename = os.path.split(remote_directory.rstrip('/')) + self.mkdir_p(sftp, dirname) # make parent directories + sftp.mkdir(basename) # sub-directory missing, so created it + sftp.chdir(basename) + return True + def send_multiple_files_in_directory(self,local_files,directory): + sftp=self.ssh.open_sftp() +# try: +# sftp.chdir(directory) # Test if remote_path exists +# except (IOError,FileNotFoundError): +# sftp.mkdir(directory) # Create remote_path +# sftp.chdir(directory) + self.mkdir_p(sftp, directory) + for f in set(local_files): + sftp.put(f, f) + sftp.close() -- Gitblit v1.9.3