diff --git a/python/hdfs_native/cli.py b/python/hdfs_native/cli.py index 38ccdd5..5f7bafb 100644 --- a/python/hdfs_native/cli.py +++ b/python/hdfs_native/cli.py @@ -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. @@ -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) diff --git a/python/tests/test_cli.py b/python/tests/test_cli.py index 425d1e2..29a3463 100644 --- a/python/tests/test_cli.py +++ b/python/tests/test_cli.py @@ -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