From 3c1ed92b8060a27a0c2b786b3531417672209f7f Mon Sep 17 00:00:00 2001 From: Robert Schmidt Date: Tue, 27 Feb 2024 18:02:42 +0100 Subject: [PATCH] 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 --- ci-scripts/cls_cmd.py | 66 +++++++++++++++++++++++++++++-------------- 1 file changed, 45 insertions(+), 21 deletions(-) diff --git a/ci-scripts/cls_cmd.py b/ci-scripts/cls_cmd.py index e2a735fa35..4eb64e2abe 100644 --- a/ci-scripts/cls_cmd.py +++ b/ci-scripts/cls_cmd.py @@ -128,10 +128,32 @@ class LocalCmd(Cmd): if src[0] != '/' or tgt[0] != '/': raise Exception('support only absolute file paths!') 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): - 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): def __enter__(self): @@ -205,36 +227,38 @@ class RemoteCmd(Cmd): def getBefore(self): 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): logging.debug(f"copyout: local:{src} -> remote:{tgt}") if recursive: tmpfile = f"{uuid.uuid4()}.tar" abstmpfile = f"/tmp/{tmpfile}" - cmd = LocalCmd() - cmd.run(f"tar -cf {abstmpfile} {src}") - sftp = self.client.open_sftp() - sftp.put(abstmpfile, abstmpfile) - sftp.close() - cmd.run(f"rm {abstmpfile}") - self.run(f"mv {abstmpfile} {tgt}; cd {tgt} && tar -xf {tmpfile} && rm {tmpfile}") + with LocalCmd() as cmd: + if cmd.run(f"tar -cf {abstmpfile} {src}").returncode != 0: + return False + if not PutFile(self.client, abstmpfile, abstmpfile): + return False + cmd.run(f"rm {abstmpfile}") + ret = self.run(f"mv {abstmpfile} {tgt}; cd {tgt} && tar -xf {tmpfile} && rm {tmpfile}") + return ret.returncode == 0 else: - sftp = self.client.open_sftp() - sftp.put(src, tgt) - sftp.close() + return PutFile(self.client, src, tgt) + # 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): logging.debug(f"copyin: remote:{src} -> local:{tgt}") if recursive: tmpfile = f"{uuid.uuid4()}.tar" abstmpfile = f"/tmp/{tmpfile}" - self.run(f"tar -cf {abstmpfile} {src}") - sftp = self.client.open_sftp() - sftp.get(abstmpfile, abstmpfile) - sftp.close() + if self.run(f"tar -cf {abstmpfile} {src}").returncode != 0: + return False + if not GetFile(self.client, abstmpfile, abstmpfile): + return False self.run(f"rm {abstmpfile}") - cmd = LocalCmd() - cmd.run(f"mv {abstmpfile} {tgt}; cd {tgt} && tar -xf {tmpfile} && rm {tmpfile}") + with LocalCmd() as cmd: + ret = cmd.run(f"mv {abstmpfile} {tgt}; cd {tgt} && tar -xf {tmpfile} && rm {tmpfile}") + return ret.returncode == 0 else: - sftp = self.client.open_sftp() - sftp.get(src, tgt) - sftp.close() + return GetFile(self.client, src, tgt)