Commit 3c1ed92b authored by Robert Schmidt's avatar Robert Schmidt

Return success/failure of copyin()/copyout

- Return code says if copying succeeded
- Improve handling of the recursive code paths, which copy files or
  directories recursively into another directory
- both for RemoteCmd/LocalCmd
parent d5eb5ee0
...@@ -128,10 +128,32 @@ class LocalCmd(Cmd): ...@@ -128,10 +128,32 @@ class LocalCmd(Cmd):
if src[0] != '/' or tgt[0] != '/': if src[0] != '/' or tgt[0] != '/':
raise Exception('support only absolute file paths!') raise Exception('support only absolute file paths!')
opt = '-r' if recursive else '' opt = '-r' if recursive else ''
self.run(f'cp {opt} {src} {tgt}') return self.run(f'cp {opt} {src} {tgt}').returncode == 0
def copyout(self, src, tgt, recursive=False): def copyout(self, src, tgt, recursive=False):
self.copyin(src, tgt, recursive) return self.copyin(src, tgt, recursive)
def PutFile(client, src, tgt):
success = True
sftp = client.open_sftp()
try:
sftp.put(src, tgt)
except FileNotFoundError as error:
logging.error(f"error while putting {src}: {error}")
success = False
sftp.close()
return success
def GetFile(client, src, tgt):
success = True
sftp = client.open_sftp()
try:
sftp.get(src, tgt)
except FileNotFoundError as error:
logging.error(f"error while getting {src}: {error}")
success = False
sftp.close()
return success
class RemoteCmd(Cmd): class RemoteCmd(Cmd):
def __enter__(self): def __enter__(self):
...@@ -205,36 +227,38 @@ class RemoteCmd(Cmd): ...@@ -205,36 +227,38 @@ class RemoteCmd(Cmd):
def getBefore(self): def getBefore(self):
return self.cp.stdout return self.cp.stdout
# if recursive is True, tgt must be a directory (and src is file or directory)
# if recursive is False, tgt and src must be a file name
def copyout(self, src, tgt, recursive=False): def copyout(self, src, tgt, recursive=False):
logging.debug(f"copyout: local:{src} -> remote:{tgt}") logging.debug(f"copyout: local:{src} -> remote:{tgt}")
if recursive: if recursive:
tmpfile = f"{uuid.uuid4()}.tar" tmpfile = f"{uuid.uuid4()}.tar"
abstmpfile = f"/tmp/{tmpfile}" abstmpfile = f"/tmp/{tmpfile}"
cmd = LocalCmd() with LocalCmd() as cmd:
cmd.run(f"tar -cf {abstmpfile} {src}") if cmd.run(f"tar -cf {abstmpfile} {src}").returncode != 0:
sftp = self.client.open_sftp() return False
sftp.put(abstmpfile, abstmpfile) if not PutFile(self.client, abstmpfile, abstmpfile):
sftp.close() return False
cmd.run(f"rm {abstmpfile}") cmd.run(f"rm {abstmpfile}")
self.run(f"mv {abstmpfile} {tgt}; cd {tgt} && tar -xf {tmpfile} && rm {tmpfile}") ret = self.run(f"mv {abstmpfile} {tgt}; cd {tgt} && tar -xf {tmpfile} && rm {tmpfile}")
return ret.returncode == 0
else: else:
sftp = self.client.open_sftp() return PutFile(self.client, src, tgt)
sftp.put(src, tgt)
sftp.close()
# if recursive is True, tgt must be a directory (and src is file or directory)
# if recursive is False, tgt and src must be a file name
def copyin(self, src, tgt, recursive=False): def copyin(self, src, tgt, recursive=False):
logging.debug(f"copyin: remote:{src} -> local:{tgt}") logging.debug(f"copyin: remote:{src} -> local:{tgt}")
if recursive: if recursive:
tmpfile = f"{uuid.uuid4()}.tar" tmpfile = f"{uuid.uuid4()}.tar"
abstmpfile = f"/tmp/{tmpfile}" abstmpfile = f"/tmp/{tmpfile}"
self.run(f"tar -cf {abstmpfile} {src}") if self.run(f"tar -cf {abstmpfile} {src}").returncode != 0:
sftp = self.client.open_sftp() return False
sftp.get(abstmpfile, abstmpfile) if not GetFile(self.client, abstmpfile, abstmpfile):
sftp.close() return False
self.run(f"rm {abstmpfile}") self.run(f"rm {abstmpfile}")
cmd = LocalCmd() with LocalCmd() as cmd:
cmd.run(f"mv {abstmpfile} {tgt}; cd {tgt} && tar -xf {tmpfile} && rm {tmpfile}") ret = cmd.run(f"mv {abstmpfile} {tgt}; cd {tgt} && tar -xf {tmpfile} && rm {tmpfile}")
return ret.returncode == 0
else: else:
sftp = self.client.open_sftp() return GetFile(self.client, src, tgt)
sftp.get(src, tgt)
sftp.close()
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment