Skip to content

Commit e0b896c

Browse files
authored
Add rm to CLI (#200)
1 parent 5dcffa3 commit e0b896c

File tree

2 files changed

+79
-0
lines changed

2 files changed

+79
-0
lines changed

python/hdfs_native/cli.py

+47
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,19 @@ def put(args: Namespace):
311311
f.result()
312312

313313

314+
def rm(args: Namespace):
315+
if not args.skip_trash:
316+
raise ValueError(
317+
"Moving files to the trash is not currently supported. Pass --skip-trash to permanently delete the files."
318+
)
319+
320+
for url in args.src:
321+
client = _client_for_url(url)
322+
for path in _glob_path(client, _path_for_url(url)):
323+
if not client.delete(path, args.recursive) and not args.force:
324+
raise FileNotFoundError(f"Failed to delete {path}")
325+
326+
314327
def rmdir(args: Namespace):
315328
for url in args.dir:
316329
client = _client_for_url(url)
@@ -488,6 +501,40 @@ def main(in_args: Optional[Sequence[str]] = None):
488501
)
489502
put_parser.set_defaults(func=put)
490503

504+
rm_parser = subparsers.add_parser(
505+
"rm",
506+
help="Delete files",
507+
description="Delete all files matching the specified file patterns",
508+
)
509+
rm_parser.add_argument(
510+
"-f",
511+
"--force",
512+
action="store_true",
513+
default=False,
514+
help="Ignore if the file does not exist",
515+
)
516+
rm_parser.add_argument(
517+
"-r",
518+
"-R",
519+
"--recursive",
520+
action="store_true",
521+
default=False,
522+
help="Recursively delete directories",
523+
)
524+
rm_parser.add_argument(
525+
"-s",
526+
"--skip-trash",
527+
action="store_true",
528+
default=False,
529+
help="Permanently delete files instead of moving them to the trash",
530+
)
531+
rm_parser.add_argument(
532+
"src",
533+
nargs="+",
534+
help="File patterns to delete",
535+
)
536+
rm_parser.set_defaults(func=rm)
537+
491538
rmdir_parser = subparsers.add_parser(
492539
"rmdir",
493540
help="Delete an empty directory",

python/tests/test_cli.py

+32
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@
1010
from hdfs_native.cli import main as cli_main
1111

1212

13+
def assert_not_exists(client: Client, path: str):
14+
try:
15+
client.get_file_info(path)
16+
pytest.fail(f"Expected file not to exist: {path}")
17+
except FileNotFoundError:
18+
pass
19+
20+
1321
def test_cat(client: Client):
1422
with client.create("/testfile") as file:
1523
file.write(b"1234")
@@ -249,6 +257,30 @@ def test_put(client: Client):
249257
assert file.read() == data
250258

251259

260+
def test_rm(client: Client):
261+
with pytest.raises(ValueError):
262+
cli_main(["rm", "/testfile"])
263+
264+
with pytest.raises(FileNotFoundError):
265+
cli_main(["rm", "-s", "/testfile"])
266+
267+
cli_main(["rm", "-f", "-s", "/testfile"])
268+
269+
client.create("/testfile").close()
270+
cli_main(["rm", "-s", "/testfile"])
271+
assert_not_exists(client, "/testfile")
272+
273+
client.mkdirs("/testdir")
274+
client.create("/testdir/testfile").close()
275+
client.create("/testdir/testfile2").close()
276+
277+
with pytest.raises(RuntimeError):
278+
cli_main(["rm", "-s", "/testdir"])
279+
280+
cli_main(["rm", "-r", "-s", "/testdir"])
281+
assert_not_exists(client, "/testdir")
282+
283+
252284
def test_rmdir(client: Client):
253285
with pytest.raises(FileNotFoundError):
254286
cli_main(["rmdir", "/testdir"])

0 commit comments

Comments
 (0)