Skip to content

Commit 85c8dcd

Browse files
tomwhitethodson-usgs
authored andcommitted
Add better test for local paths (#532)
1 parent 5e54646 commit 85c8dcd

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

cubed/core/plan.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,13 @@
1616
from cubed.runtime.pipeline import visit_nodes
1717
from cubed.runtime.types import ComputeEndEvent, ComputeStartEvent, CubedPipeline
1818
from cubed.storage.zarr import LazyZarrArray
19-
from cubed.utils import chunk_memory, extract_stack_summaries, join_path, memory_repr
19+
from cubed.utils import (
20+
chunk_memory,
21+
extract_stack_summaries,
22+
is_local_path,
23+
join_path,
24+
memory_repr,
25+
)
2026

2127
# A unique ID with sensible ordering, used for making directory names
2228
CONTEXT_ID = f"cubed-{datetime.now().strftime('%Y%m%dT%H%M%S')}-{uuid.uuid4()}"
@@ -26,7 +32,7 @@
2632

2733

2834
def delete_on_exit(context_dir: str) -> None:
29-
if context_dir not in CONTEXT_DIRS and context_dir.startswith("/"):
35+
if context_dir not in CONTEXT_DIRS and is_local_path(context_dir):
3036
atexit.register(lambda: shutil.rmtree(context_dir, ignore_errors=True))
3137
CONTEXT_DIRS.add(context_dir)
3238

cubed/tests/test_utils.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
block_id_to_offset,
1313
broadcast_trick,
1414
extract_stack_summaries,
15+
is_local_path,
1516
join_path,
1617
map_nested,
1718
memory_repr,
@@ -69,6 +70,15 @@ def test_join_path():
6970
assert join_path("http://host/a path", "subpath") == "http://host/a%20path/subpath"
7071

7172

73+
def test_is_local_path():
74+
assert is_local_path("relative_path/path")
75+
assert is_local_path("/absolute_path/path")
76+
assert is_local_path("file:relative_path/path")
77+
assert is_local_path("file://absolute_path/path")
78+
assert is_local_path("file:///absolute_path/path")
79+
assert not is_local_path("s3://host/path")
80+
81+
7282
def test_memory_repr():
7383
assert memory_repr(0) == "0 bytes"
7484
assert memory_repr(1) == "1 bytes"

cubed/utils.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,11 @@ def join_path(dir_url: PathType, child_path: str) -> str:
7474
return urlunsplit(split_parts)
7575

7676

77+
def is_local_path(path: str):
78+
"""Determine if a path string is for the local filesystem."""
79+
return urlsplit(path).scheme in ("", "file")
80+
81+
7782
def memory_repr(num: int) -> str:
7883
"""Convert bytes to a human-readable string in decimal form.
7984
1 KB is 1,000 bytes, 1 MB is 1,000,000 bytes, and so on.

0 commit comments

Comments
 (0)