From bdf06862198cb594893bb90d88ac7bb4ddc4a2e1 Mon Sep 17 00:00:00 2001 From: jmoore Date: Fri, 4 Sep 2020 08:41:55 +0200 Subject: [PATCH] Restore downloading without --output --- ome_zarr/utils.py | 12 +++++++++--- tests/test_cli.py | 3 ++- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/ome_zarr/utils.py b/ome_zarr/utils.py index 575780ae..200c387e 100644 --- a/ome_zarr/utils.py +++ b/ome_zarr/utils.py @@ -58,9 +58,10 @@ def download(input_path: str, output_dir: str = ".") -> None: nodes.append(node) paths.append(node.zarr.zarr_path) - strip_common_prefix(paths) + common = strip_common_prefix(paths) + root = os.path.join(output_dir, common) - assert not os.path.exists(output_dir), f"{output_dir} already exists!" + assert not os.path.exists(root), f"{root} already exists!" print("downloading...") for path in paths: print(" ", path) @@ -92,9 +93,10 @@ def download(input_path: str, output_dir: str = ".") -> None: f.write(json.dumps(metadata)) -def strip_common_prefix(paths: List[str]) -> None: +def strip_common_prefix(paths: List[str]) -> str: """Find and remove the prefix common to all strings. + Returns the last element of the common prefix. An exception is thrown if no common prefix exists. >>> paths = ["a/b", "a/b/c"] @@ -118,7 +120,11 @@ def strip_common_prefix(paths: List[str]) -> None: for path in parts: msg += f"{path}\n" raise Exception(msg) + else: + common = parts[0][first_mismatch - 1] for idx, path in enumerate(parts): base = os.path.sep.join(path[first_mismatch - 1 :]) paths[idx] = base + + return common diff --git a/tests/test_cli.py b/tests/test_cli.py index 8f516a81..16de2d49 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -55,7 +55,8 @@ def _rotate_and_test(self, *hierarchy: Path, reverse: bool = True): firstpass.rotate(1) copy = [str(x) for x in firstpass] - strip_common_prefix(copy) + common = strip_common_prefix(copy) + assert "d" == common assert set(copy) == set(results) if reverse: