Skip to content
This repository has been archived by the owner on Jan 8, 2025. It is now read-only.

Commit

Permalink
Merge pull request #139 from mhearne-usgs/scpfix
Browse files Browse the repository at this point in the history
Line length fixes, and adding a remote user option to scp sender.
  • Loading branch information
mhearne-usgs authored Jan 27, 2021
2 parents 52c3868 + 1b2b485 commit c9200c4
Showing 1 changed file with 21 additions and 8 deletions.
29 changes: 21 additions & 8 deletions impactutils/transfer/securesender.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# stdlib imports
import os.path
import datetime
import getpass

# depends on https://github.com/jbardin/scp.py
# pip install git+git://github.com/jbardin/scp.py.git
Expand Down Expand Up @@ -71,7 +72,8 @@ def send(self):
# make sure the remote system has directory or it can be created.
res = self._make_remote_folder(scp, ssh, remote_folder)
if not res:
msg = f'Unable to create remote folder {remote_folder} on host {remote_host}'
msg = (f'Unable to create remote folder {remote_folder} '
f'on host {remote_host}')
raise Exception(msg)

# do the copying
Expand Down Expand Up @@ -101,7 +103,8 @@ def send(self):
for r, d, files in os.walk(self._local_directory)])
scp.close()
ssh.close()
return (nfiles, f'{int(nfiles):d} files sent to remote host {remote_host}')
msg = f'{int(nfiles):d} files sent to remote host {remote_host}'
return (nfiles, msg)

def cancel(self, cancel_content=None):
"""
Expand All @@ -126,10 +129,13 @@ def cancel(self, cancel_content=None):
exists = not int(stdout.read().decode('utf-8').strip())
if not exists:
err = stderr1.read().decode('utf-8').strip()
fmt = f'Could not create {cancelfile} file on remote_host {remote_host} due to error: {err}'
fmt = (f'Could not create {cancelfile} file on '
f'remote_host {remote_host} due to error: {err}')
raise Exception(fmt)

return (f'A .cancel file has been placed in remote directory {remote_folder}.')
msg = ('A .cancel file has been placed in remote '
f'directory {remote_folder}.')
return (msg)

def _connect(self):
"""Initiate an ssh connection with properties passed to constructor.
Expand All @@ -144,13 +150,19 @@ def _connect(self):
# load hosts found in ~/.ssh/known_hosts
# should we not assume that the user has these configured already?
ssh.load_system_host_keys()
remote_user = getpass.getuser()
if 'remote_user' in self._properties:
remote_user = self._properties['remote_user']
try:
ssh.connect(self._properties['remote_host'],
username=remote_user,
key_filename=self._properties['private_key'],
compress=True)
except Exception as obj:
raise Exception(
f"Could not connect with private key file {self._properties['private_key']}")
msg = ("Could not connect with private key "
f"file {self._properties['private_key']}: "
f"Error '{str(obj)}")
raise Exception(msg)
return ssh

def _copy_file_with_path(self, scp, ssh, local_file, remote_folder,
Expand Down Expand Up @@ -196,7 +208,8 @@ def _copy_file_with_path(self, scp, ssh, local_file, remote_folder,
if not isdir:
res = self._make_remote_folder(scp, ssh, root)
if not res:
fmt = (f'Could not copy local file {local_file} to folder {remote_folder} '
fmt = (f'Could not copy local file {local_file} '
f'to folder {remote_folder} '
f'on host {remote_host}')
raise Exception(fmt)

Expand Down Expand Up @@ -241,7 +254,7 @@ def _make_remote_folder(self, scp, ssh, remote_folder):
Boolean indicating success or failure.
"""
exists, isdir = self._check_remote_folder(ssh, remote_folder)
chk_cmd2 = f'[ -d {remote_folder} ];echo $?'
chk_cmd1 = f'[ -d {remote_folder} ];echo $?'
if not isdir:
if exists:
rm_cmd = f'rm {remote_folder}'
Expand Down

0 comments on commit c9200c4

Please sign in to comment.