Skip to content

Commit

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


def rmdir(args: Namespace):
for url in args.dir:
client = _client_for_url(url)
for path in _glob_path(client, _path_for_url(url)):
status = client.get_file_info(path)
if not status.isdir:
raise ValueError(f"{path} is not a directory")

client.delete(path)


def main(in_args: Optional[Sequence[str]] = None):
parser = ArgumentParser(
description="""Command line utility for interacting with HDFS using hdfs-native.
Expand Down Expand Up @@ -477,6 +488,18 @@ def main(in_args: Optional[Sequence[str]] = None):
)
put_parser.set_defaults(func=put)

rmdir_parser = subparsers.add_parser(
"rmdir",
help="Delete an empty directory",
description="Delete an empty directory",
)
rmdir_parser.add_argument(
"dir",
nargs="+",
help="Source patterns to copy",
)
rmdir_parser.set_defaults(func=rmdir)

args = parser.parse_args(in_args)
args.func(args)

Expand Down
21 changes: 21 additions & 0 deletions python/tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,3 +247,24 @@ def test_put(client: Client):
assert file.read() == data
with client.read("/testdir/testfile2") as file:
assert file.read() == data


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

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

with pytest.raises(RuntimeError):
cli_main(["rmdir", "/testdir"])

client.delete("/testdir/testfile")

cli_main(["rmdir", "/testdir"])

try:
client.get_file_info("/testdir")
pytest.fail("Directory was not removed")
except FileNotFoundError:
pass

0 comments on commit 5dcffa3

Please sign in to comment.