From a114a1696fc33d269e3d23321e9206a876cab531 Mon Sep 17 00:00:00 2001 From: Erick Martins Ratamero Date: Thu, 21 Mar 2024 10:29:08 -0400 Subject: [PATCH] adding `--ignore_errors` option --- src/omero_cli_transfer.py | 72 ++++++++++++++++++++++++--------------- 1 file changed, 44 insertions(+), 28 deletions(-) diff --git a/src/omero_cli_transfer.py b/src/omero_cli_transfer.py index d5f038e..38fe28c 100644 --- a/src/omero_cli_transfer.py +++ b/src/omero_cli_transfer.py @@ -223,6 +223,10 @@ def _configure(self, parser): pack.add_argument( "--simple", help="Pack into a human-readable package file", action="store_true") + pack.add_argument( + "--ignore_errors", help="Ignores any download/export errors " + "during the pack process", + action="store_true") pack.add_argument( "--metadata", choices=['all', 'none', 'img_id', 'timestamp', @@ -309,7 +313,7 @@ def _get_path_to_repo(self) -> List[str]: return mrepos def _copy_files(self, id_list: Dict[str, Any], folder: str, - conn: BlitzGateway): + ignore_errors: bool, conn: BlitzGateway): if not isinstance(id_list, dict): raise TypeError("id_list must be a dict") if not all(isinstance(item, str) for item in id_list.keys()): @@ -336,26 +340,34 @@ def _copy_files(self, id_list: Dict[str, Any], folder: str, if rel_path == "pixel_images" or fileset is None: filepath = str(Path(subfolder) / (str(clean_id) + ".tiff")) - try: - cli.invoke(['export', '--file', filepath, id], - strict=True) - except NonZeroReturnCode: - print("A file could not be exported - this is " - "generally due to a server not allowing" - " binary downloads.") - shutil.rmtree(folder) - raise NonZeroReturnCode(1, "Download not allowed") + if not ignore_errors: + try: + cli.invoke(['export', '--file', filepath, id], + strict=True) + except NonZeroReturnCode: + print("A file could not be exported - this is " + "generally due to a server not allowing" + " binary downloads.") + shutil.rmtree(folder) + raise NonZeroReturnCode(1, "Download not \ + allowed") + else: + cli.invoke(['export', '--file', filepath, id]) downloaded_ids.append(id) else: - try: - cli.invoke(['download', id, subfolder], - strict=True) - except NonZeroReturnCode: - print("A file could not be downloaded - this is " - "generally due to a server not allowing" - " binary downloads.") - shutil.rmtree(folder) - raise NonZeroReturnCode(1, "Download not allowed") + if not ignore_errors: + try: + cli.invoke(['download', id, subfolder], + strict=True) + except NonZeroReturnCode: + print("A file could not be downloaded - this " + "is generally due to a server not " + "allowing binary downloads.") + shutil.rmtree(folder) + raise NonZeroReturnCode(1, "Download not \ + allowed") + else: + cli.invoke(['download', id, subfolder]) for fs_image in fileset.copyImages(): downloaded_ids.append(fs_image.getId()) else: @@ -365,14 +377,17 @@ def _copy_files(self, id_list: Dict[str, Any], folder: str, ann_folder = str(Path(subfolder).parent) os.makedirs(ann_folder, mode=DIR_PERM, exist_ok=True) id = "File" + id - try: - cli.invoke(['download', id, subfolder], strict=True) - except NonZeroReturnCode: - print("A file could not be downloaded - this is " - "generally due to a server not allowing" - " binary downloads.") - shutil.rmtree(folder) - raise NonZeroReturnCode(1, "Download not allowed") + if not ignore_errors: + try: + cli.invoke(['download', id, subfolder], strict=True) + except NonZeroReturnCode: + print("A file could not be downloaded - this is " + "generally due to a server not allowing" + " binary downloads.") + shutil.rmtree(folder) + raise NonZeroReturnCode(1, "Download not allowed") + else: + cli.invoke(['download', id, subfolder]) def _package_files(self, tar_path: str, zip: bool, folder: str): if zip: @@ -492,7 +507,8 @@ def __pack(self, args): if args.binaries == "all": print("Starting file copy...") - self._copy_files(path_id_dict, folder, self.gateway) + self._copy_files(path_id_dict, folder, args.ignore_errors, + self.gateway) if args.simple: self._fix_pixels_image_simple(ome, folder, md_fp)