Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add rm to CLI #200

Merged
merged 1 commit into from
Jan 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading