Skip to content

Commit

Permalink
remove all top level latch.ldata imports
Browse files Browse the repository at this point in the history
Signed-off-by: Ayush Kamat <ayush@latch.bio>
  • Loading branch information
ayushkamat committed Oct 10, 2024
1 parent 158e130 commit af0af19
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 55 deletions.
8 changes: 3 additions & 5 deletions latch_cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,13 @@
import sys
from pathlib import Path
from textwrap import dedent
from typing import Callable, List, Optional, Tuple, TypeVar, Union
from typing import Callable, List, Literal, Optional, Tuple, TypeVar, Union

import click
from packaging.version import parse as parse_version
from typing_extensions import ParamSpec

import latch_cli.click_utils
from latch.ldata._transfer.progress import Progress as _Progress
from latch_cli.click_utils import EnumChoice
from latch_cli.exceptions.handler import CrashHandler
from latch_cli.services.cp.autocomplete import complete as cp_complete
from latch_cli.services.cp.autocomplete import remote_complete
Expand Down Expand Up @@ -693,7 +691,7 @@ def get_executions():
@click.option(
"--progress",
help="Type of progress information to show while copying",
type=EnumChoice(_Progress, case_sensitive=False),
type=click.Choice(["none", "total", "tasks"]),
default="tasks",
show_default=True,
)
Expand Down Expand Up @@ -735,7 +733,7 @@ def get_executions():
def cp(
src: List[str],
dest: str,
progress: _Progress,
progress: Literal["none", "total", "tasks"],
verbose: bool,
force: bool,
no_glob: bool,
Expand Down
16 changes: 10 additions & 6 deletions latch_cli/services/cp/download/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,13 +127,15 @@ def download(
" may be overwritten. Proceed?"
)
):
return
continue

try:
work_dest.unlink(missing_ok=True)
work_queue.put_nowait(Work(gsud["url"], work_dest, chunk_size_mib))
except OSError:
click.echo(f"Cannot write file to {work_dest} - directory exists.")
click.secho(
f"Cannot write file to {work_dest} - directory exists.", fg="red"
)

else:
gsurd: GetSignedUrlsRecursiveData = json["data"]
Expand All @@ -160,17 +162,19 @@ def download(
try:
res.parent.mkdir(exist_ok=True, parents=True)
if res.is_dir():
click.echo(
click.secho(
f"Cannot write file to {work_dest / rel} - directory"
" exists."
" exists.",
fg="red",
)
continue

work_queue.put_nowait(Work(url, work_dest / rel, chunk_size_mib))
except (NotADirectoryError, FileExistsError):
click.echo(
click.secho(
f"Cannot write file to {work_dest / rel} - upstream file"
" exists."
" exists.",
fg="red",
)

tbar = tqdm.tqdm(
Expand Down
59 changes: 19 additions & 40 deletions latch_cli/services/cp/main.py
Original file line number Diff line number Diff line change
@@ -1,57 +1,45 @@
from pathlib import Path
from textwrap import dedent
from typing import List, Optional
from typing import List, Literal, Optional

import click
import gql
from latch_sdk_gql.execute import execute

from latch.ldata._transfer.download import download as _download
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.type import LatchPathError
from latch_cli.services.cp.glob import expand_pattern
from latch_cli.utils import human_readable_time, with_si_suffix
from latch_cli.utils.path import get_path_error, is_remote_path

from .download.main import download
from .upload.main import upload


def _copy_and_print(src: str, dst: str, progress: Progress) -> None:
def _copy_and_print(
src: str, dst: str, progress: Literal["none", "total", "tasks"]
) -> None:
from latch.ldata._transfer.remote_copy import remote_copy as _remote_copy

_remote_copy(src, dst)
if progress != Progress.none:
click.echo(dedent(f"""

if progress != "none":
click.echo(dedent(f"""\
{click.style("Copy Requested.", fg="green")}
{click.style("Source: ", fg="blue")}{(src)}
{click.style("Destination: ", fg="blue")}{(dst)}"""))


def _download_and_print(src: str, dst: Path, progress: Progress, verbose: bool) -> None:
if progress != Progress.none:
click.secho(f"Downloading {dst.name}", fg="blue")
res = _download(src, dst, progress, verbose)
if progress != Progress.none:
click.echo(dedent(f"""
{click.style("Download Complete", fg="green")}
{click.style("Time Elapsed: ", fg="blue")}{human_readable_time(res.total_time)}
{click.style("Files Downloaded: ", fg="blue")}{res.num_files} ({with_si_suffix(res.total_bytes)})
"""))
{click.style("Destination: ", fg="blue")}{(dst)}\
"""))


# todo(ayush): come up with a better behavior scheme than unix cp
def cp(
srcs: List[str],
dest: str,
*,
progress: Progress,
progress: Literal["none", "total", "tasks"],
verbose: bool,
force: bool,
expand_globs: bool,
cores: Optional[int] = None,
chunk_size_mib: Optional[int] = None,
):
from latch.ldata.type import LatchPathError

if chunk_size_mib is not None and chunk_size_mib < 5:
click.secho(
"The chunk size specified by --chunk-size-mib must be at least 5. You"
Expand All @@ -60,17 +48,6 @@ def cp(
)
raise click.exceptions.Exit(1)

# todo(ayush): make this a global thats computed in requires_auth()
acc_info = execute(gql.gql("""
query AccountInfo {
accountInfoCurrent {
id
}
}
"""))["accountInfoCurrent"]

acc_id = acc_info["id"]

dest_is_remote = is_remote_path(dest)
srcs_are_remote = [is_remote_path(src) for src in srcs]

Expand All @@ -79,7 +56,7 @@ def cp(
download(
srcs,
Path(dest),
progress.name,
progress,
verbose,
force,
expand_globs,
Expand All @@ -90,7 +67,7 @@ def cp(
upload(
srcs,
dest,
progress.name,
progress,
verbose,
cores,
chunk_size_mib,
Expand All @@ -115,7 +92,9 @@ def cp(
raise click.exceptions.Exit(1)

except LatchPathError as e:
click.secho(get_path_error(e.remote_path, e.message, acc_id), fg="red")
if e.acc_id is not None:
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")
Expand Down
11 changes: 7 additions & 4 deletions latch_cli/services/cp/upload/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ def upload(
cores: Optional[int],
chunk_size_mib: Optional[int],
):
from latch.ldata.type import LatchPathError

if cores is None:
cores = 4
if chunk_size_mib is None:
Expand Down Expand Up @@ -58,7 +56,12 @@ def upload(
root = normalized
elif src_path.is_dir():
if not dest_is_dir:
raise LatchPathError("not a directory", dest)
click.secho(
f"Failed to upload directory {src_path}: destination {dest} is not"
" a directory",
fg="red",
)
continue
if src.endswith("/"):
root = normalized
else:
Expand All @@ -83,7 +86,7 @@ def upload(
try:
total_bytes += rel.resolve().stat().st_size
except FileNotFoundError:
print(f"WARNING: file {rel} not found, skipping...")
click.secho(f"File {rel} not found, skipping...", fg="yellow")
continue

num_files += 1
Expand Down

0 comments on commit af0af19

Please sign in to comment.