Skip to content

Commit

Permalink
Add rm to CLI (#200)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kimahriman authored Jan 21, 2025
1 parent 5dcffa3 commit e0b896c
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 0 deletions.
47 changes: 47 additions & 0 deletions python/hdfs_native/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,19 @@ def put(args: Namespace):
f.result()


def rm(args: Namespace):
if not args.skip_trash:
raise ValueError(
"Moving files to the trash is not currently supported. Pass --skip-trash to permanently delete the files."
)

for url in args.src:
client = _client_for_url(url)
for path in _glob_path(client, _path_for_url(url)):
if not client.delete(path, args.recursive) and not args.force:
raise FileNotFoundError(f"Failed to delete {path}")


def rmdir(args: Namespace):
for url in args.dir:
client = _client_for_url(url)
Expand Down Expand Up @@ -488,6 +501,40 @@ def main(in_args: Optional[Sequence[str]] = None):
)
put_parser.set_defaults(func=put)

rm_parser = subparsers.add_parser(
"rm",
help="Delete files",
description="Delete all files matching the specified file patterns",
)
rm_parser.add_argument(
"-f",
"--force",
action="store_true",
default=False,
help="Ignore if the file does not exist",
)
rm_parser.add_argument(
"-r",
"-R",
"--recursive",
action="store_true",
default=False,
help="Recursively delete directories",
)
rm_parser.add_argument(
"-s",
"--skip-trash",
action="store_true",
default=False,
help="Permanently delete files instead of moving them to the trash",
)
rm_parser.add_argument(
"src",
nargs="+",
help="File patterns to delete",
)
rm_parser.set_defaults(func=rm)

rmdir_parser = subparsers.add_parser(
"rmdir",
help="Delete an empty directory",
Expand Down
32 changes: 32 additions & 0 deletions python/tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@
from hdfs_native.cli import main as cli_main


def assert_not_exists(client: Client, path: str):
try:
client.get_file_info(path)
pytest.fail(f"Expected file not to exist: {path}")
except FileNotFoundError:
pass


def test_cat(client: Client):
with client.create("/testfile") as file:
file.write(b"1234")
Expand Down Expand Up @@ -249,6 +257,30 @@ def test_put(client: Client):
assert file.read() == data


def test_rm(client: Client):
with pytest.raises(ValueError):
cli_main(["rm", "/testfile"])

with pytest.raises(FileNotFoundError):
cli_main(["rm", "-s", "/testfile"])

cli_main(["rm", "-f", "-s", "/testfile"])

client.create("/testfile").close()
cli_main(["rm", "-s", "/testfile"])
assert_not_exists(client, "/testfile")

client.mkdirs("/testdir")
client.create("/testdir/testfile").close()
client.create("/testdir/testfile2").close()

with pytest.raises(RuntimeError):
cli_main(["rm", "-s", "/testdir"])

cli_main(["rm", "-r", "-s", "/testdir"])
assert_not_exists(client, "/testdir")


def test_rmdir(client: Client):
with pytest.raises(FileNotFoundError):
cli_main(["rmdir", "/testdir"])
Expand Down

0 comments on commit e0b896c

Please sign in to comment.