Skip to content

Commit

Permalink
use latch_persistence in LPath
Browse files Browse the repository at this point in the history
Signed-off-by: Ayush Kamat <[email protected]>
  • Loading branch information
ayushkamat committed Feb 7, 2025
1 parent 5493f7e commit ed5f0a1
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 20 deletions.
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ dependencies = [
"setuptools>=75.3.0",
"pyxattr>=0.8.1",
"orjson>=3.10.12",
"latch-persistence>=0.1.1",
]
classifiers = [
"Development Status :: 4 - Beta",
Expand Down
51 changes: 32 additions & 19 deletions src/latch/ldata/path.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import re
import shutil
import sys
import warnings
from collections.abc import Iterator
from dataclasses import dataclass, field
from pathlib import Path
Expand All @@ -20,16 +21,13 @@
Scalar,
)
from flytekit.extend import TypeEngine, TypeTransformer
from typing_extensions import Self
from latch_persistence import LatchPersistence

from latch.ldata.type import LatchPathError, LDataNodeType
from latch_cli.utils import urljoins

from ._transfer.download import download as _download
from ._transfer.node import get_node_data as _get_node_data
from ._transfer.progress import Progress as _Progress
from ._transfer.remote_copy import remote_copy as _remote_copy
from ._transfer.upload import upload as _upload
from ._transfer.utils import query_with_retry

node_id_regex = re.compile(r"^latch://(?P<id>[0-9]+)\.node$")
Expand Down Expand Up @@ -73,6 +71,14 @@ class LPath:
default_factory=_Cache, init=False, repr=False, hash=False, compare=False
)

_persistence: LatchPersistence = field(
default_factory=LatchPersistence,
init=False,
repr=False,
hash=False,
compare=False,
)

path: str

def __post_init__(self):
Expand Down Expand Up @@ -199,7 +205,7 @@ def version_id(self, *, load_if_missing: bool = True) -> Optional[str]:
def is_dir(self, *, load_if_missing: bool = True) -> bool:
return self.type(load_if_missing=load_if_missing) in _dir_types

def iterdir(self) -> Iterator[Self]:
def iterdir(self) -> Iterator["LPath"]:
"""Yield LPaths objects contained within the directory.
Should only be called on directories. Does not recursively list directories.
Expand Down Expand Up @@ -289,13 +295,17 @@ def upload_from(self, src: Path, *, show_progress_bar: bool = False) -> None:
src: The source path.
show_progress_bar: Whether to show a progress bar during the upload.
"""
_upload(
os.fspath(src),
self.path,
progress=_Progress.tasks if show_progress_bar else _Progress.none,
verbose=False,
create_parents=True,
)
if show_progress_bar:
warnings.warn(
"argument `show_progress_bar` is deprecated and will be removed in a future release of `latch`.",
stacklevel=2,
)

if src.is_dir():
self._persistence.upload_directory(str(src), self.path)
else:
self._persistence.upload(str(src), self.path)

self._clear_cache()

def download(
Expand All @@ -312,6 +322,12 @@ def download(
downloaded there. The temprary directory is deleted when the program exits.
show_progress_bar: Whether to show a progress bar during the download.
"""
if show_progress_bar:
warnings.warn(
"argument `show_progress_bar` is deprecated and will be removed in a future release of `latch`.",
stacklevel=2,
)

if dst is None:
global _download_idx
tmp_dir = Path.home() / ".latch" / "lpath" / str(_download_idx)
Expand All @@ -336,13 +352,10 @@ def download(
):
return dst

_download(
self.path,
dst,
progress=_Progress.tasks if show_progress_bar else _Progress.none,
verbose=False,
confirm_overwrite=False,
)
if self.is_dir():
self._persistence.download_directory(self.path, str(dst))
else:
self._persistence.download(self.path, str(dst))

if not_windows and version_id is not None:
xattr.setxattr(dst_str, "user.version_id", version_id)
Expand Down
16 changes: 15 additions & 1 deletion uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit ed5f0a1

Please sign in to comment.