33# stdlib imports
44import os .path
55import datetime
6+ import getpass
67
78# depends on https://github.com/jbardin/scp.py
89# pip install git+git://github.com/jbardin/scp.py.git
@@ -71,7 +72,8 @@ def send(self):
7172 # make sure the remote system has directory or it can be created.
7273 res = self ._make_remote_folder (scp , ssh , remote_folder )
7374 if not res :
74- msg = f'Unable to create remote folder { remote_folder } on host { remote_host } '
75+ msg = (f'Unable to create remote folder { remote_folder } '
76+ f'on host { remote_host } ' )
7577 raise Exception (msg )
7678
7779 # do the copying
@@ -101,7 +103,8 @@ def send(self):
101103 for r , d , files in os .walk (self ._local_directory )])
102104 scp .close ()
103105 ssh .close ()
104- return (nfiles , f'{ int (nfiles ):d} files sent to remote host { remote_host } ' )
106+ msg = f'{ int (nfiles ):d} files sent to remote host { remote_host } '
107+ return (nfiles , msg )
105108
106109 def cancel (self , cancel_content = None ):
107110 """
@@ -126,10 +129,13 @@ def cancel(self, cancel_content=None):
126129 exists = not int (stdout .read ().decode ('utf-8' ).strip ())
127130 if not exists :
128131 err = stderr1 .read ().decode ('utf-8' ).strip ()
129- fmt = f'Could not create { cancelfile } file on remote_host { remote_host } due to error: { err } '
132+ fmt = (f'Could not create { cancelfile } file on '
133+ f'remote_host { remote_host } due to error: { err } ' )
130134 raise Exception (fmt )
131135
132- return (f'A .cancel file has been placed in remote directory { remote_folder } .' )
136+ msg = ('A .cancel file has been placed in remote '
137+ f'directory { remote_folder } .' )
138+ return (msg )
133139
134140 def _connect (self ):
135141 """Initiate an ssh connection with properties passed to constructor.
@@ -144,13 +150,19 @@ def _connect(self):
144150 # load hosts found in ~/.ssh/known_hosts
145151 # should we not assume that the user has these configured already?
146152 ssh .load_system_host_keys ()
153+ remote_user = getpass .getuser ()
154+ if 'remote_user' in self ._properties :
155+ remote_user = self ._properties ['remote_user' ]
147156 try :
148157 ssh .connect (self ._properties ['remote_host' ],
158+ username = remote_user ,
149159 key_filename = self ._properties ['private_key' ],
150160 compress = True )
151161 except Exception as obj :
152- raise Exception (
153- f"Could not connect with private key file { self ._properties ['private_key' ]} " )
162+ msg = ("Could not connect with private key "
163+ f"file { self ._properties ['private_key' ]} : "
164+ f"Error '{ str (obj )} " )
165+ raise Exception (msg )
154166 return ssh
155167
156168 def _copy_file_with_path (self , scp , ssh , local_file , remote_folder ,
@@ -196,7 +208,8 @@ def _copy_file_with_path(self, scp, ssh, local_file, remote_folder,
196208 if not isdir :
197209 res = self ._make_remote_folder (scp , ssh , root )
198210 if not res :
199- fmt = (f'Could not copy local file { local_file } to folder { remote_folder } '
211+ fmt = (f'Could not copy local file { local_file } '
212+ f'to folder { remote_folder } '
200213 f'on host { remote_host } ' )
201214 raise Exception (fmt )
202215
@@ -241,7 +254,7 @@ def _make_remote_folder(self, scp, ssh, remote_folder):
241254 Boolean indicating success or failure.
242255 """
243256 exists , isdir = self ._check_remote_folder (ssh , remote_folder )
244- chk_cmd2 = f'[ -d { remote_folder } ];echo $?'
257+ chk_cmd1 = f'[ -d { remote_folder } ];echo $?'
245258 if not isdir :
246259 if exists :
247260 rm_cmd = f'rm { remote_folder } '
0 commit comments