Skip to content

Commit 06bac9b

Browse files
authored
Add initial CLI with mv command (#174)
1 parent 7b0a2ef commit 06bac9b

File tree

4 files changed

+135
-0
lines changed

4 files changed

+135
-0
lines changed

python/hdfs_native/cli.py

+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
import os
2+
from argparse import ArgumentParser, Namespace
3+
from typing import Optional, Sequence
4+
from urllib.parse import urlparse
5+
6+
from hdfs_native import Client
7+
8+
9+
def _client_for_url(url: str) -> Client:
10+
parsed = urlparse(url)
11+
12+
if parsed.scheme:
13+
connection_url = f"{parsed.scheme}://{parsed.hostname}"
14+
if parsed.port:
15+
connection_url += f":{parsed.port}"
16+
return Client(connection_url)
17+
elif parsed.hostname or parsed.port:
18+
raise ValueError(
19+
f"Cannot provide host or port without scheme: {parsed.hostname}"
20+
)
21+
else:
22+
return Client()
23+
24+
25+
def _verify_nameservices_match(url: str, *urls: str) -> None:
26+
first = urlparse(url)
27+
28+
for url in urls:
29+
parsed = urlparse(url)
30+
if first.scheme != parsed.scheme or first.hostname != parsed.hostname:
31+
raise ValueError(
32+
f"Protocol and host must match: {first.scheme}://{first.hostname} != {parsed.scheme}://{parsed.hostname}"
33+
)
34+
35+
36+
def _path_for_url(url: str) -> str:
37+
return urlparse(url).path
38+
39+
40+
def mv(args: Namespace):
41+
_verify_nameservices_match(args.dst, *args.src)
42+
43+
client = _client_for_url(args.dst)
44+
dst_path = _path_for_url(args.dst)
45+
46+
dst_isdir = False
47+
try:
48+
dst_isdir = client.get_file_info(dst_path).isdir
49+
except FileNotFoundError:
50+
pass
51+
52+
if len(args.src) > 1 and not dst_isdir:
53+
raise ValueError(
54+
"destination must be a directory if multiple sources are provided"
55+
)
56+
57+
for src in args.src:
58+
src_path = _path_for_url(src)
59+
if dst_isdir:
60+
target_path = os.path.join(dst_path, os.path.basename(src_path))
61+
else:
62+
target_path = dst_path
63+
64+
client.rename(src_path, target_path)
65+
66+
67+
def main(in_args: Optional[Sequence[str]] = None):
68+
parser = ArgumentParser(
69+
description="""Command line utility for interacting with HDFS using hdfs-native.
70+
Globs are not currently supported, all file paths are treated as exact paths."""
71+
)
72+
73+
subparsers = parser.add_subparsers(title="Subcommands", required=True)
74+
75+
mv_parser = subparsers.add_parser(
76+
"mv",
77+
help="Move files or directories",
78+
description="""Move a file or directory from <src> to <dst>. Must be part of the same name service.
79+
If multiple src are provided, dst must be a directory""",
80+
)
81+
mv_parser.add_argument("src", nargs="+", help="Files or directories to move")
82+
mv_parser.add_argument("dst", help="Target destination of file or directory")
83+
mv_parser.set_defaults(func=mv)
84+
85+
args = parser.parse_args(in_args)
86+
args.func(args)
87+
88+
89+
if __name__ == "__main__":
90+
main()

python/hdfs_native/py.typed

Whitespace-only changes.

python/pyproject.toml

+3
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ devel = [
2929
[project.urls]
3030
repository = "https://github.com/Kimahriman/hdfs-native"
3131

32+
[project.scripts]
33+
hdfsn = "hdfs_native.cli:main"
34+
3235
[project.entry-points."fsspec.specs"]
3336
hdfs = "hdfs_native.fsspec.HdfsFileSystem"
3437
viewfs = "hdfs_native.fsspec.HdfsFileSystem"

python/tests/test_cli.py

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import pytest
2+
3+
from hdfs_native import Client
4+
from hdfs_native.cli import main as cli_main
5+
6+
7+
def test_cli(minidfs: str):
8+
client = Client(minidfs)
9+
10+
def qualify(path: str) -> str:
11+
return f"{minidfs}{path}"
12+
13+
# mv
14+
client.create("/testfile").close()
15+
client.mkdirs("/testdir")
16+
17+
cli_main(["mv", qualify("/testfile"), qualify("/testfile2")])
18+
19+
client.get_file_info("/testfile2")
20+
21+
with pytest.raises(ValueError):
22+
cli_main(["mv", qualify("/testfile2"), "hdfs://badnameservice/testfile"])
23+
24+
with pytest.raises(RuntimeError):
25+
cli_main(["mv", qualify("/testfile2"), qualify("/nonexistent/testfile")])
26+
27+
cli_main(["mv", qualify("/testfile2"), qualify("/testdir")])
28+
29+
client.get_file_info("/testdir/testfile2")
30+
31+
client.rename("/testdir/testfile2", "/testfile1")
32+
client.create("/testfile2").close()
33+
34+
with pytest.raises(ValueError):
35+
cli_main(
36+
["mv", qualify("/testfile1"), qualify("/testfile2"), qualify("/testfile3")]
37+
)
38+
39+
cli_main(["mv", qualify("/testfile1"), qualify("/testfile2"), qualify("/testdir/")])
40+
41+
client.get_file_info("/testdir/testfile1")
42+
client.get_file_info("/testdir/testfile2")

0 commit comments

Comments
 (0)