-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathcli.py
348 lines (288 loc) · 10.1 KB
/
cli.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
import functools
import os
import re
import shutil
import sys
from argparse import ArgumentParser, Namespace
from concurrent.futures import ThreadPoolExecutor, as_completed
from typing import List, Optional, Sequence, Tuple
from urllib.parse import urlparse
from hdfs_native import Client
@functools.cache
def _get_client(connection_url: Optional[str] = None):
return Client(connection_url)
def _client_for_url(url: str) -> Client:
parsed = urlparse(url)
if parsed.scheme:
connection_url = f"{parsed.scheme}://{parsed.hostname}"
if parsed.port:
connection_url += f":{parsed.port}"
return _get_client(connection_url)
elif parsed.hostname or parsed.port:
raise ValueError(
f"Cannot provide host or port without scheme: {parsed.hostname}"
)
else:
return _get_client()
def _verify_nameservices_match(url: str, *urls: str) -> None:
first = urlparse(url)
for url in urls:
parsed = urlparse(url)
if first.scheme != parsed.scheme or first.hostname != parsed.hostname:
raise ValueError(
f"Protocol and host must match: {first.scheme}://{first.hostname} != {parsed.scheme}://{parsed.hostname}"
)
def _path_for_url(url: str) -> str:
return urlparse(url).path
def _glob_path(client: Client, glob: str) -> List[str]:
# TODO: Actually implement this, for now just pretend we have multiple results
return [glob]
def _download_file(
client: Client,
remote_src: str,
local_dst: str,
force: bool = False,
preserve: bool = False,
) -> None:
if not force and os.path.exists(local_dst):
raise FileExistsError(f"{local_dst} already exists, use --force to overwrite")
with client.read(remote_src) as remote_file:
with open(local_dst, "wb") as local_file:
for chunk in remote_file.read_range_stream(0, len(remote_file)):
local_file.write(chunk)
if preserve:
status = client.get_file_info(remote_src)
os.utime(
local_dst,
(status.access_time / 1000, status.modification_time / 1000),
)
os.chmod(local_dst, status.permission)
def _upload_file(
client: Client,
local_src: str,
remote_dst: str,
force: bool = False,
preserve: bool = False,
) -> None:
with open(local_src, "rb") as local_file:
with client.create(remote_dst) as remote_file:
shutil.copyfileobj(local_file, remote_file)
def cat(args: Namespace):
for src in args.src:
client = _client_for_url(src)
for path in _glob_path(client, _path_for_url(src)):
with client.read(path) as file:
while chunk := file.read(1024 * 1024):
sys.stdout.buffer.write(chunk)
sys.stdout.buffer.flush()
def chmod(args: Namespace):
if not re.fullmatch(r"1?[0-7]{3}", args.octalmode):
raise ValueError(f"Invalid mode supplied: {args.octalmode}")
permission = int(args.octalmode, base=8)
for url in args.path:
client = _client_for_url(url)
for path in _glob_path(client, _path_for_url(url)):
if args.recursive:
for status in client.list_status(path, True):
client.set_permission(status.path, permission)
else:
client.set_permission(path, permission)
def chown(args: Namespace):
split = args.owner.split(":")
if len(split) > 2:
raise ValueError(f"Invalid owner and group pattern: {args.owner}")
if len(split) == 1:
owner = split[0]
group = None
else:
owner = split[0] if split[0] else None
group = split[1]
for url in args.path:
client = _client_for_url(url)
for path in _glob_path(client, _path_for_url(url)):
if args.recursive:
for status in client.list_status(path, True):
client.set_owner(status.path, owner, group)
else:
client.set_owner(path, owner, group)
def get(args: Namespace):
paths: List[Tuple[Client, str]] = []
for url in args.src:
client = _client_for_url(url)
for path in _glob_path(client, _path_for_url(url)):
paths.append((client, path))
dst_is_dir = os.path.isdir(args.localdst)
if len(paths) > 1 and not dst_is_dir:
raise ValueError("Destination must be directory when copying multiple files")
elif not dst_is_dir:
_download_file(
paths[0][0],
paths[0][1],
args.localdst,
force=args.force,
preserve=args.preserve,
)
else:
with ThreadPoolExecutor(args.threads) as executor:
futures = []
for client, path in paths:
filename = os.path.basename(path)
futures.append(
executor.submit(
_download_file,
client,
path,
os.path.join(args.localdst, filename),
force=args.force,
preserve=args.preserve,
)
)
# Iterate to raise any exceptions thrown
for f in as_completed(futures):
f.result()
def mkdir(args: Namespace):
create_parent = args.parent
for url in args.path:
client = _client_for_url(url)
client.mkdirs(_path_for_url(url), create_parent=create_parent)
def mv(args: Namespace):
_verify_nameservices_match(args.dst, *args.src)
client = _client_for_url(args.dst)
dst_path = _path_for_url(args.dst)
dst_isdir = False
try:
dst_isdir = client.get_file_info(dst_path).isdir
except FileNotFoundError:
pass
resolved_src = [
path
for pattern in args.src
for path in _glob_path(client, _path_for_url(pattern))
]
if len(resolved_src) > 1 and not dst_isdir:
raise ValueError(
"destination must be a directory if multiple sources are provided"
)
for src in resolved_src:
src_path = _path_for_url(src)
if dst_isdir:
target_path = os.path.join(dst_path, os.path.basename(src_path))
else:
target_path = dst_path
client.rename(src_path, target_path)
def main(in_args: Optional[Sequence[str]] = None):
parser = ArgumentParser(
description="""Command line utility for interacting with HDFS using hdfs-native.
Globs are not currently supported, all file paths are treated as exact paths."""
)
subparsers = parser.add_subparsers(title="Subcommands", required=True)
cat_parser = subparsers.add_parser(
"cat",
help="Print the contents of a file",
description="Print the contents of a file to stdout",
)
cat_parser.add_argument("src", nargs="+", help="File pattern to print")
cat_parser.set_defaults(func=cat)
chmod_parser = subparsers.add_parser(
"chmod",
help="Changes permissions of a file",
description="Changes permissions of a file. Only octal permissions are supported.",
)
chmod_parser.add_argument(
"-R",
"--recursive",
action="store_true",
help="Modify files recursively",
)
chmod_parser.add_argument(
"octalmode",
help="The mode to set the permission to in octal format",
)
chmod_parser.add_argument("path", nargs="+", help="File pattern to update")
chmod_parser.set_defaults(func=chmod)
chown_parser = subparsers.add_parser(
"chown",
help="Changes owner and group of a file",
description="Changes owner and group of a file",
)
chown_parser.add_argument(
"-R",
"--recursive",
action="store_true",
help="Modify files recursively",
)
chown_parser.add_argument(
"owner",
metavar="[owner[:group] | :group]",
help="Owner and group to set for the file",
)
chown_parser.add_argument("path", nargs="+", help="File pattern to modify")
chown_parser.set_defaults(func=chown)
get_parser = subparsers.add_parser(
"get",
aliases=["copyToLocal"],
help="Copy files to a local destination",
description="""Copy files matching a pattern to a local destination.
When copying multiple files, the destination must be a directory""",
)
get_parser.add_argument(
"-p",
"--preserve",
action="store_true",
default=False,
help="Preserve timestamps and the mode",
)
get_parser.add_argument(
"-f",
"--force",
action="store_true",
default=False,
help="Overwrite the destination if it already exists",
)
get_parser.add_argument(
"-t",
"--threads",
type=int,
help="Number of threads to use",
default=1,
)
get_parser.add_argument(
"src",
nargs="+",
help="Source patterns to copy",
)
get_parser.add_argument(
"localdst",
help="Local destination to write to",
)
get_parser.set_defaults(func=get)
mkdir_parser = subparsers.add_parser(
"mkdir",
help="Create a directory",
description="Create a directory in a specified path",
)
mkdir_parser.add_argument(
"path",
nargs="+",
help="Path for the directory to create",
)
mkdir_parser.add_argument(
"-p",
"--parent",
action="store_true",
help="Create any missing parent directories",
)
mkdir_parser.set_defaults(func=mkdir)
mv_parser = subparsers.add_parser(
"mv",
help="Move files or directories",
description="""Move a file or directory from <src> to <dst>. Must be part of the same name service.
If multiple src are provided, dst must be a directory""",
)
mv_parser.add_argument("src", nargs="+", help="Files or directories to move")
mv_parser.add_argument("dst", help="Target destination of file or directory")
mv_parser.set_defaults(func=mv)
args = parser.parse_args(in_args)
args.func(args)
if __name__ == "__main__":
main()