Skip to content

Commit

Permalink
implement new command for using fast latch-persistence copies
Browse files Browse the repository at this point in the history
  • Loading branch information
rahuldesai1 committed Feb 13, 2025
1 parent 1e3fccc commit 9469dc5
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/latch_cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -812,6 +812,23 @@ def cp(
chunk_size_mib=chunk_size_mib,
)

@main.command("cp-fast")
@click.argument("src", shell_complete=cp_complete, nargs=-1)
@click.argument("dest", shell_complete=cp_complete, nargs=1)
@requires_login
def cp_fast(
src: List[str],
dest: str,
):
"""
Fast copy of files between Latch Data and local, or between two Latch Data locations.
"""
crash_handler.message = f"Unable to copy {src} to {dest}"
crash_handler.pkg_root = str(Path.cwd())

from latch_cli.services.cp.main import cp_fast

cp_fast(src, dest)

@main.command("mv")
@click.argument("src", shell_complete=remote_complete, nargs=1)
Expand Down
32 changes: 32 additions & 0 deletions src/latch_cli/services/cp/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from latch.ldata._transfer.progress import Progress
from latch.ldata._transfer.remote_copy import remote_copy as _remote_copy
from latch.ldata._transfer.upload import upload as _upload
from latch.ldata.path import LPath
from latch.ldata.type import LatchPathError
from latch_cli.services.cp.glob import expand_pattern
from latch_cli.utils import human_readable_time, with_si_suffix
Expand Down Expand Up @@ -104,3 +105,34 @@ def cp(
except Exception as e:
click.secho(str(e), fg="red")
raise click.exceptions.Exit(1) from e

def cp_fast(
srcs: List[str],
dest: str,
):
dest_remote = is_remote_path(dest)

for src in srcs:
src_remote = is_remote_path(src)

try:
if src_remote and not dest_remote:
LPath(src).download(Path(dest))
elif not src_remote and dest_remote:
LPath(dest).upload_from(Path(src))
elif src_remote and dest_remote:
LPath(src).copy_to(LPath(dest))
else:
raise ValueError(
dedent(f"""
`latch cp-fast` cannot be used for purely local file copying.
Please ensure at least one of your arguments is a remote path (beginning with `latch://`)
""").strip("\n"),
)
except LatchPathError as e:
click.secho(get_path_error(e.remote_path, e.message, e.acc_id), fg="red")
raise click.exceptions.Exit(1) from e
except Exception as e:
click.secho(str(e), fg="red")
raise click.exceptions.Exit(1) from e

0 comments on commit 9469dc5

Please sign in to comment.