Skip to content

Commit

Permalink
feat: support removing data in "gas rm" command
Browse files Browse the repository at this point in the history
  • Loading branch information
rexzheng324-c committed May 17, 2021
1 parent 26898fd commit 2227b6e
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 27 deletions.
22 changes: 22 additions & 0 deletions tensorbay/cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,5 +232,27 @@ def cp( # pylint: disable=invalid-name, too-many-arguments
_implement_cp(obj, local_paths, tbrn, is_recursive, jobs, skip_uploaded_files)


@cli.command()
@click.argument("tbrn", type=str)
@click.option(
"-r", "--recursive", "is_recursive", is_flag=True, help="Remove directories recursively."
)
@click.pass_obj
def rm( # pylint: disable=invalid-name, too-many-arguments
obj: Dict[str, str], tbrn: str, is_recursive: bool
) -> None:
"""Remove the remote data.
Arguments:
obj: A dict contains config information.
tbrn: The path to be removed, like "tb:KITTI#1".
is_recursive: Whether remove directories recursively.
""" # noqa: D301,D415
from .rm import _implement_rm

_implement_rm(obj, tbrn, is_recursive)


if __name__ == "__main__":
cli() # pylint: disable=no-value-for-parameter
29 changes: 3 additions & 26 deletions tensorbay/cli/ls.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
"""Implementation of gas ls."""

import sys
from typing import Dict, Iterable, Iterator, Optional, Union
from typing import Dict, Iterable, Optional, Union

import click

from ..client import GAS
from ..client.segment import FusionSegmentClient, SegmentClient
from .tbrn import TBRN, TBRNType
from .utility import get_dataset_client, get_gas
from .utility import filter_data, get_dataset_client, get_gas


def _echo_segment(
Expand Down Expand Up @@ -199,29 +199,6 @@ def _ls_fusion_file(
click.echo(info)


def _filter_data(
data_list: Iterable[str], remote_path: str, is_recursive: bool = True
) -> Iterator[str]:
"""Get a list of paths under the remote_path.
Arguments:
data_list: A list of candidate paths.
remote_path: The remote path to filter data.
is_recursive: Whether to filter data recursively.
Returns:
A list of paths under the given remote_path.
"""
if is_recursive:
return (
filter(lambda x: x.startswith(remote_path), data_list)
if remote_path.endswith("/")
else filter(lambda x: x.startswith(remote_path + "/") or x == remote_path, data_list)
)
return filter(lambda x: x == remote_path, data_list)


def _ls_normal_file( # pylint: disable=unused-argument
gas: GAS, info: TBRN, list_all_files: bool
) -> None:
Expand All @@ -232,7 +209,7 @@ def _ls_normal_file( # pylint: disable=unused-argument
info.draft_number,
info.revision,
info.segment_name,
_filter_data(segment_client.list_data_paths(), info.remote_path),
filter_data(segment_client.list_data_paths(), info.remote_path),
)


Expand Down
47 changes: 47 additions & 0 deletions tensorbay/cli/rm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#!/usr/bin/env python3
#
# Copyright 2021 Graviti. Licensed under MIT License.
#

"""Implementation of gas rm."""

import sys
from typing import Dict

import click

from .tbrn import TBRN, TBRNType
from .utility import filter_data, get_dataset_client, get_gas


def _implement_rm(obj: Dict[str, str], tbrn: str, is_recursive: bool) -> None:
gas = get_gas(**obj)
info = TBRN(tbrn=tbrn)
dataset_client = get_dataset_client(gas, info, is_fusion=False)

if info.type not in (TBRNType.SEGMENT, TBRNType.NORMAL_FILE):
click.echo(f'"{tbrn}" is an invalid path to remove', err=True)
sys.exit(1)

if not info.is_draft:
click.echo(
f'To remove the data, "{info}" must be in draft status, like "{info}#1', err=True
)
sys.exit(1)

if info.type == TBRNType.SEGMENT:
if not is_recursive:
click.echo("Please use -r option to remove the whole segment", err=True)
sys.exit(1)
dataset_client.delete_segment(info.segment_name)
elif info.remote_path.endswith("/") and not is_recursive:
click.echo("Please use -r option to remove recursively", err=True)
sys.exit(1)
else:
segment = dataset_client.get_segment(info.segment_name)
data = filter_data(segment.list_data_paths(), info.remote_path, is_recursive)
if not data:
echo_info = "file or directory" if is_recursive else "file"
click.echo(f'No such {echo_info} "{tbrn}" ', err=True)
sys.exit(1)
segment.delete_data(data)
25 changes: 24 additions & 1 deletion tensorbay/cli/utility.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import os
import sys
from configparser import ConfigParser
from typing import Literal, Optional, Tuple, overload
from typing import Iterable, Iterator, Literal, Optional, Tuple, overload

import click

Expand Down Expand Up @@ -138,3 +138,26 @@ def get_dataset_client(gas: GAS, info: TBRN, is_fusion: Optional[bool] = None) -
elif info.revision is not None:
dataset_client.checkout(revision=info.revision)
return dataset_client


def filter_data(
data_list: Iterable[str], remote_path: str, is_recursive: bool = True
) -> Iterator[str]:
"""Get a list of paths under the remote_path.
Arguments:
data_list: A list of candidate paths.
remote_path: The remote path to filter data.
is_recursive: Whether to filter data recursively.
Returns:
A list of paths under the given remote_path.
"""
if is_recursive:
return (
filter(lambda x: x.startswith(remote_path), data_list)
if remote_path.endswith("/")
else filter(lambda x: x.startswith(remote_path + "/") or x == remote_path, data_list)
)
return filter(lambda x: x == remote_path, data_list)

0 comments on commit 2227b6e

Please sign in to comment.