Skip to content

Commit

Permalink
feat: support deleting 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 653684e
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 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
48 changes: 48 additions & 0 deletions tensorbay/cli/rm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#!/usr/bin/env python3
#
# Copyright 2021 Graviti. Licensed under MIT License.
#

"""Implementation of gas rm."""

import sys
from typing import Dict

import click

from .ls import _filter_data
from .tbrn import TBRN, TBRNType
from .utility import 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)
filter_data = list(_filter_data(segment.list_data_paths(), info.remote_path, is_recursive))
if not filter_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(filter_data)

0 comments on commit 653684e

Please sign in to comment.