| 1 |
#!/usr/bin/env python |
| 2 |
|
| 3 |
# -*- coding: utf-8 -*- |
| 4 |
# coding=utf-8 |
| 5 |
# -------------------------------------------------------------------------- |
| 6 |
# Copyright (c) Microsoft Corporation. All rights reserved. |
| 7 |
# Licensed under the MIT License. See License.txt in the project root for |
| 8 |
# license information. |
| 9 |
# -------------------------------------------------------------------------- |
| 10 |
|
| 11 |
""" |
| 12 |
An interface to be run from the command line/powershell. |
| 13 |
|
| 14 |
This file is the only executable in the project. |
| 15 |
""" |
| 16 |
|
| 17 |
from __future__ import print_function |
| 18 |
from __future__ import unicode_literals |
| 19 |
|
| 20 |
import argparse |
| 21 |
import cmd |
| 22 |
from datetime import datetime |
| 23 |
import os |
| 24 |
import stat |
| 25 |
import sys |
| 26 |
|
| 27 |
from azure.datalake.store.core import AzureDLFileSystem |
| 28 |
from azure.datalake.store.multithread import ADLDownloader, ADLUploader |
| 29 |
from azure.datalake.store.utils import write_stdout |
| 30 |
|
| 31 |
|
| 32 |
class AzureDataLakeFSCommand(cmd.Cmd, object): |
| 33 |
"""Accept commands via an interactive prompt or the command line.""" |
| 34 |
|
| 35 |
prompt = 'azure> ' |
| 36 |
undoc_header = None |
| 37 |
_hidden_methods = ('do_EOF',) |
| 38 |
|
| 39 |
def __init__(self, fs): |
| 40 |
super(AzureDataLakeFSCommand, self).__init__() |
| 41 |
self._fs = fs |
| 42 |
|
| 43 |
def get_names(self): |
| 44 |
return [n for n in dir(self.__class__) if n not in self._hidden_methods] |
| 45 |
|
| 46 |
def do_close(self, line): |
| 47 |
return True |
| 48 |
|
| 49 |
def help_close(self): |
| 50 |
print("close\n") |
| 51 |
print("Exit the application") |
| 52 |
|
| 53 |
def do_cat(self, line): |
| 54 |
parser = argparse.ArgumentParser(prog="cat", add_help=False) |
| 55 |
parser.add_argument('files', type=str, nargs='+') |
| 56 |
args = parser.parse_args(line.split()) |
| 57 |
|
| 58 |
for f in args.files: |
| 59 |
write_stdout(self._fs.cat(f)) |
| 60 |
|
| 61 |
def help_cat(self): |
| 62 |
print("cat file ...\n") |
| 63 |
print("Display contents of files") |
| 64 |
|
| 65 |
def do_chgrp(self, line): |
| 66 |
parser = argparse.ArgumentParser(prog="chgrp", add_help=False) |
| 67 |
parser.add_argument('group', type=str) |
| 68 |
parser.add_argument('files', type=str, nargs='+') |
| 69 |
args = parser.parse_args(line.split()) |
| 70 |
|
| 71 |
for f in args.files: |
| 72 |
self._fs.chown(f, group=args.group) |
| 73 |
|
| 74 |
def help_chgrp(self): |
| 75 |
print("chgrp group file ...\n") |
| 76 |
print("Change file group") |
| 77 |
|
| 78 |
def do_chmod(self, line): |
| 79 |
parser = argparse.ArgumentParser(prog="chmod", add_help=False) |
| 80 |
parser.add_argument('mode', type=str) |
| 81 |
parser.add_argument('files', type=str, nargs='+') |
| 82 |
args = parser.parse_args(line.split()) |
| 83 |
|
| 84 |
for f in args.files: |
| 85 |
self._fs.chmod(f, args.mode) |
| 86 |
|
| 87 |
def help_chmod(self): |
| 88 |
print("chmod mode file ...\n") |
| 89 |
print("Change file permissions") |
| 90 |
|
| 91 |
def _parse_ownership(self, ownership): |
| 92 |
if ':' in ownership: |
| 93 |
owner, group = ownership.split(':') |
| 94 |
if not owner: |
| 95 |
owner = None |
| 96 |
else: |
| 97 |
owner = ownership |
| 98 |
group = None |
| 99 |
return owner, group |
| 100 |
|
| 101 |
def do_chown(self, line): |
| 102 |
parser = argparse.ArgumentParser(prog="chown", add_help=False) |
| 103 |
parser.add_argument('ownership', type=str) |
| 104 |
parser.add_argument('files', type=str, nargs='+') |
| 105 |
args = parser.parse_args(line.split()) |
| 106 |
|
| 107 |
owner, group = self._parse_ownership(args.ownership) |
| 108 |
|
| 109 |
for f in args.files: |
| 110 |
self._fs.chown(f, owner=owner, group=group) |
| 111 |
|
| 112 |
def help_chown(self): |
| 113 |
print("chown owner[:group] file ...") |
| 114 |
print("chown :group file ...\n") |
| 115 |
print("Change file owner and group") |
| 116 |
|
| 117 |
def _display_dict(self, d): |
| 118 |
width = max([len(k) for k in d.keys()]) |
| 119 |
for k, v in sorted(list(d.items())): |
| 120 |
print("{0:{width}} = {1}".format(k, v, width=width)) |
| 121 |
|
| 122 |
def do_df(self, line): |
| 123 |
parser = argparse.ArgumentParser(prog="df", add_help=False) |
| 124 |
parser.add_argument('path', type=str, nargs='?', default='.') |
| 125 |
args = parser.parse_args(line.split()) |
| 126 |
|
| 127 |
self._display_dict(self._fs.df(args.path)) |
| 128 |
|
| 129 |
def help_df(self): |
| 130 |
print("df [path]\n") |
| 131 |
print("Display Azure account statistics of a path") |
| 132 |
|
| 133 |
def _truncate(self, num, fmt): |
| 134 |
return '{:{fmt}}'.format(num, fmt=fmt).rstrip('0').rstrip('.') |
| 135 |
|
| 136 |
def _format_size(self, num): |
| 137 |
for unit in ['B', 'K', 'M', 'G', 'T']: |
| 138 |
if abs(num) < 1024.0: |
| 139 |
return '{:>4s}{}'.format(self._truncate(num, '3.1f'), unit) |
| 140 |
num /= 1024.0 |
| 141 |
return self._truncate(num, '.1f') + 'P' |
| 142 |
|
| 143 |
def _display_path_with_size(self, name, size, human_readable): |
| 144 |
if human_readable: |
| 145 |
print("{:7s} {}".format(self._format_size(size), name)) |
| 146 |
else: |
| 147 |
print("{:<9d} {}".format(size, name)) |
| 148 |
|
| 149 |
def do_du(self, line): |
| 150 |
parser = argparse.ArgumentParser(prog="du", add_help=False) |
| 151 |
parser.add_argument('files', type=str, nargs='*', default=['']) |
| 152 |
parser.add_argument('-c', '--total', action='store_true') |
| 153 |
parser.add_argument('-h', '--human-readable', action='store_true') |
| 154 |
parser.add_argument('-r', '--recursive', action='store_true') |
| 155 |
args = parser.parse_args(line.split()) |
| 156 |
|
| 157 |
total = 0 |
| 158 |
for f in args.files: |
| 159 |
items = sorted(list(self._fs.du(f, deep=args.recursive).items())) |
| 160 |
for name, size in items: |
| 161 |
total += size |
| 162 |
self._display_path_with_size(name, size, args.human_readable) |
| 163 |
if args.total: |
| 164 |
self._display_path_with_size("total", total, args.human_readable) |
| 165 |
|
| 166 |
def help_du(self): |
| 167 |
print("du [-c | --total] [-r | --recursive] [-h | --human-readable] [file ...]\n") |
| 168 |
print("Display disk usage statistics") |
| 169 |
|
| 170 |
def do_exists(self, line): |
| 171 |
parser = argparse.ArgumentParser(prog="exists", add_help=False) |
| 172 |
parser.add_argument('file', type=str) |
| 173 |
args = parser.parse_args(line.split()) |
| 174 |
|
| 175 |
print(self._fs.exists(args.file, invalidate_cache=False)) |
| 176 |
|
| 177 |
def help_exists(self): |
| 178 |
print("exists file\n") |
| 179 |
print("Check if file/directory exists") |
| 180 |
|
| 181 |
def do_get(self, line): |
| 182 |
parser = argparse.ArgumentParser(prog="get", add_help=False) |
| 183 |
parser.add_argument('remote_path', type=str) |
| 184 |
parser.add_argument('local_path', type=str, nargs='?', default='.') |
| 185 |
parser.add_argument('-b', '--chunksize', type=int, default=2**28) |
| 186 |
parser.add_argument('-c', '--threads', type=int, default=None) |
| 187 |
parser.add_argument('-f', '--force', action='store_true') |
| 188 |
args = parser.parse_args(line.split()) |
| 189 |
|
| 190 |
ADLDownloader(self._fs, args.remote_path, args.local_path, |
| 191 |
nthreads=args.threads, chunksize=args.chunksize, |
| 192 |
overwrite=args.force) |
| 193 |
|
| 194 |
def help_get(self): |
| 195 |
print("get [option]... remote-path [local-path]\n") |
| 196 |
print("Retrieve the remote path and store it locally\n") |
| 197 |
print("Options:") |
| 198 |
print(" -b <int>") |
| 199 |
print(" --chunksize <int>") |
| 200 |
print(" Set size of chunk to retrieve atomically, in bytes.\n") |
| 201 |
print(" -c <int>") |
| 202 |
print(" --threads <int>") |
| 203 |
print(" Set number of multiple requests to perform at a time.") |
| 204 |
print(" -f") |
| 205 |
print(" --force") |
| 206 |
print(" Overwrite an existing file or directory.") |
| 207 |
|
| 208 |
def do_head(self, line): |
| 209 |
parser = argparse.ArgumentParser(prog="head", add_help=False) |
| 210 |
parser.add_argument('files', type=str, nargs='+') |
| 211 |
parser.add_argument('-c', '--bytes', type=int, default=1024) |
| 212 |
args = parser.parse_args(line.split()) |
| 213 |
|
| 214 |
for f in args.files: |
| 215 |
write_stdout(self._fs.head(f, size=args.bytes)) |
| 216 |
|
| 217 |
def help_head(self): |
| 218 |
print("head [-c bytes | --bytes bytes] file ...\n") |
| 219 |
print("Display first bytes of a file") |
| 220 |
|
| 221 |
def do_info(self, line): |
| 222 |
parser = argparse.ArgumentParser(prog="info", add_help=False) |
| 223 |
parser.add_argument('files', type=str, nargs='+') |
| 224 |
args = parser.parse_args(line.split()) |
| 225 |
|
| 226 |
for f in args.files: |
| 227 |
self._display_dict(self._fs.info(f, invalidate_cache=False)) |
| 228 |
|
| 229 |
def help_info(self): |
| 230 |
print("info file ...\n") |
| 231 |
print("Display file information") |
| 232 |
|
| 233 |
def _display_item(self, item, human_readable): |
| 234 |
mode = int(item['permission'], 8) |
| 235 |
|
| 236 |
if item['type'] == 'DIRECTORY': |
| 237 |
permissions = "d" |
| 238 |
elif item['type'] == 'SYMLINK': |
| 239 |
permissions = "l" |
| 240 |
else: |
| 241 |
permissions = "-" |
| 242 |
|
| 243 |
permissions += "r" if bool(mode & stat.S_IRUSR) else "-" |
| 244 |
permissions += "w" if bool(mode & stat.S_IWUSR) else "-" |
| 245 |
permissions += "x" if bool(mode & stat.S_IXUSR) else "-" |
| 246 |
permissions += "r" if bool(mode & stat.S_IRGRP) else "-" |
| 247 |
permissions += "w" if bool(mode & stat.S_IWGRP) else "-" |
| 248 |
permissions += "x" if bool(mode & stat.S_IXGRP) else "-" |
| 249 |
permissions += "r" if bool(mode & stat.S_IROTH) else "-" |
| 250 |
permissions += "w" if bool(mode & stat.S_IWOTH) else "-" |
| 251 |
permissions += "x" if bool(mode & stat.S_IXOTH) else "-" |
| 252 |
|
| 253 |
timestamp = item['modificationTime'] // 1000 |
| 254 |
modified_at = datetime.fromtimestamp(timestamp).strftime('%b %d %H:%M') |
| 255 |
|
| 256 |
if human_readable: |
| 257 |
size = "{:5s}".format(self._format_size(item['length'])) |
| 258 |
else: |
| 259 |
size = "{:9d}".format(item['length']) |
| 260 |
|
| 261 |
print("{} {} {} {} {} {}".format( |
| 262 |
permissions, |
| 263 |
item['owner'][:8], |
| 264 |
item['group'][:8], |
| 265 |
size, |
| 266 |
modified_at, |
| 267 |
os.path.basename(item['name']))) |
| 268 |
|
| 269 |
def do_ls(self, line): |
| 270 |
parser = argparse.ArgumentParser(prog="ls", add_help=False) |
| 271 |
parser.add_argument('dirs', type=str, nargs='*', default=['']) |
| 272 |
parser.add_argument('-h', '--human-readable', action='store_true') |
| 273 |
parser.add_argument('-l', '--detail', action='store_true') |
| 274 |
args = parser.parse_args(line.split()) |
| 275 |
|
| 276 |
for d in args.dirs: |
| 277 |
for item in self._fs.ls(d, detail=args.detail, invalidate_cache=False): |
| 278 |
if args.detail: |
| 279 |
self._display_item(item, args.human_readable) |
| 280 |
else: |
| 281 |
print(os.path.basename(item)) |
| 282 |
|
| 283 |
def help_ls(self): |
| 284 |
print("ls [-h | --human-readable] [-l | --detail] [file ...]\n") |
| 285 |
print("List directory contents") |
| 286 |
|
| 287 |
def do_mkdir(self, line): |
| 288 |
parser = argparse.ArgumentParser(prog="mkdir", add_help=False) |
| 289 |
parser.add_argument('dirs', type=str, nargs='+') |
| 290 |
args = parser.parse_args(line.split()) |
| 291 |
|
| 292 |
for d in args.dirs: |
| 293 |
self._fs.mkdir(d) |
| 294 |
|
| 295 |
def help_mkdir(self): |
| 296 |
print("mkdir directory ...\n") |
| 297 |
print("Create directories") |
| 298 |
|
| 299 |
def do_mv(self, line): |
| 300 |
parser = argparse.ArgumentParser(prog="mv", add_help=False) |
| 301 |
parser.add_argument('files', type=str, nargs='+') |
| 302 |
args = parser.parse_args(line.split()) |
| 303 |
|
| 304 |
self._fs.mv(args.files[0], args.files[1]) |
| 305 |
|
| 306 |
def help_mv(self): |
| 307 |
print("mv from-path to-path\n") |
| 308 |
print("Rename from-path to to-path") |
| 309 |
|
| 310 |
def do_put(self, line): |
| 311 |
parser = argparse.ArgumentParser(prog="put", add_help=False) |
| 312 |
parser.add_argument('local_path', type=str) |
| 313 |
parser.add_argument('remote_path', type=str, nargs='?', default='.') |
| 314 |
parser.add_argument('-b', '--chunksize', type=int, default=2**28) |
| 315 |
parser.add_argument('-c', '--threads', type=int, default=None) |
| 316 |
parser.add_argument('-f', '--force', action='store_true') |
| 317 |
args = parser.parse_args(line.split()) |
| 318 |
|
| 319 |
ADLUploader(self._fs, args.remote_path, args.local_path, |
| 320 |
nthreads=args.threads, chunksize=args.chunksize, |
| 321 |
overwrite=args.force) |
| 322 |
|
| 323 |
def help_put(self): |
| 324 |
print("put [option]... local-path [remote-path]\n") |
| 325 |
print("Store a local file on the remote machine\n") |
| 326 |
print("Options:") |
| 327 |
print(" -b <int>") |
| 328 |
print(" --chunksize <int>") |
| 329 |
print(" Set size of chunk to store atomically, in bytes.\n") |
| 330 |
print(" -c <int>") |
| 331 |
print(" --threads <int>") |
| 332 |
print(" Set number of multiple requests to perform at a time.") |
| 333 |
print(" -f") |
| 334 |
print(" --force") |
| 335 |
print(" Overwrite an existing file or directory.") |
| 336 |
|
| 337 |
def do_quit(self, line): |
| 338 |
return True |
| 339 |
|
| 340 |
def help_quit(self): |
| 341 |
print("quit\n") |
| 342 |
print("Exit the application") |
| 343 |
|
| 344 |
def do_rm(self, line): |
| 345 |
parser = argparse.ArgumentParser(prog="rm", add_help=False) |
| 346 |
parser.add_argument('files', type=str, nargs='+') |
| 347 |
parser.add_argument('-r', '--recursive', action='store_true') |
| 348 |
args = parser.parse_args(line.split()) |
| 349 |
|
| 350 |
for f in args.files: |
| 351 |
self._fs.rm(f, recursive=args.recursive) |
| 352 |
|
| 353 |
def help_rm(self): |
| 354 |
print("rm [-r | --recursive] file ...\n") |
| 355 |
print("Remove directory entries") |
| 356 |
|
| 357 |
def do_rmdir(self, line): |
| 358 |
parser = argparse.ArgumentParser(prog="rmdir", add_help=False) |
| 359 |
parser.add_argument('dirs', type=str, nargs='+') |
| 360 |
args = parser.parse_args(line.split()) |
| 361 |
|
| 362 |
for d in args.dirs: |
| 363 |
self._fs.rmdir(d) |
| 364 |
|
| 365 |
def help_rmdir(self): |
| 366 |
print("rmdir directory ...\n") |
| 367 |
print("Remove directories") |
| 368 |
|
| 369 |
def do_tail(self, line): |
| 370 |
parser = argparse.ArgumentParser(prog="tail", add_help=False) |
| 371 |
parser.add_argument('files', type=str, nargs='+') |
| 372 |
parser.add_argument('-c', '--bytes', type=int, default=1024) |
| 373 |
args = parser.parse_args(line.split()) |
| 374 |
|
| 375 |
for f in args.files: |
| 376 |
write_stdout(self._fs.tail(f, size=args.bytes)) |
| 377 |
|
| 378 |
def help_tail(self): |
| 379 |
print("tail [-c bytes | --bytes bytes] file ...\n") |
| 380 |
print("Display last bytes of a file") |
| 381 |
|
| 382 |
def do_touch(self, line): |
| 383 |
parser = argparse.ArgumentParser(prog="touch", add_help=False) |
| 384 |
parser.add_argument('files', type=str, nargs='+') |
| 385 |
args = parser.parse_args(line.split()) |
| 386 |
|
| 387 |
for f in args.files: |
| 388 |
self._fs.touch(f) |
| 389 |
|
| 390 |
def help_touch(self): |
| 391 |
print("touch file ...\n") |
| 392 |
print("Change file access and modification times") |
| 393 |
|
| 394 |
def do_EOF(self, line): |
| 395 |
return True |
| 396 |
|
| 397 |
def do_list_uploads(self, line): |
| 398 |
print(ADLUploader.load()) |
| 399 |
|
| 400 |
def help_list_uploads(self): |
| 401 |
print("Shows interrupted but persisted downloads") |
| 402 |
|
| 403 |
def do_clear_uploads(self, line): |
| 404 |
ADLUploader.clear_saved() |
| 405 |
|
| 406 |
def help_clear_uploads(self): |
| 407 |
print("Forget all persisted uploads") |
| 408 |
|
| 409 |
def do_resume_upload(self, line): |
| 410 |
try: |
| 411 |
up = ADLUploader.load()[line] |
| 412 |
up.run() |
| 413 |
except KeyError: |
| 414 |
print("No such upload") |
| 415 |
|
| 416 |
def help_resume_upload(self): |
| 417 |
print("resume_upload name") |
| 418 |
print() |
| 419 |
print("Restart the upload designated by <name> and run until done.") |
| 420 |
|
| 421 |
def do_list_downloads(self, line): |
| 422 |
print(ADLDownloader.load()) |
| 423 |
|
| 424 |
def help_list_downloads(self): |
| 425 |
print("Shows interrupted but persisted uploads") |
| 426 |
|
| 427 |
def do_clear_downloads(self, line): |
| 428 |
ADLDownloader.clear_saved() |
| 429 |
|
| 430 |
def help_clear_downloads(self): |
| 431 |
print("Forget all persisted downloads") |
| 432 |
|
| 433 |
def do_resume_download(self, line): |
| 434 |
try: |
| 435 |
up = ADLDownloader.load()[line] |
| 436 |
up.run() |
| 437 |
except KeyError: |
| 438 |
print("No such download") |
| 439 |
|
| 440 |
def help_resume_download(self): |
| 441 |
print("resume_download name") |
| 442 |
print() |
| 443 |
print("Restart the download designated by <name> and run until done.") |
| 444 |
|
| 445 |
|
| 446 |
def setup_logging(default_level='WARNING'): |
| 447 |
""" Setup logging configuration |
| 448 |
|
| 449 |
The logging configuration can be overridden with one environment variable: |
| 450 |
|
| 451 |
ADLFS_LOG_LEVEL (defines logging level) |
| 452 |
""" |
| 453 |
import logging |
| 454 |
import os |
| 455 |
import sys |
| 456 |
|
| 457 |
log_level = os.environ.get('ADLFS_LOG_LEVEL', default_level) |
| 458 |
|
| 459 |
levels = dict( |
| 460 |
CRITICAL=logging.CRITICAL, |
| 461 |
ERROR=logging.ERROR, |
| 462 |
WARNING=logging.WARNING, |
| 463 |
INFO=logging.INFO, |
| 464 |
DEBUG=logging.DEBUG) |
| 465 |
|
| 466 |
if log_level in levels: |
| 467 |
log_level = levels[log_level] |
| 468 |
else: |
| 469 |
sys.exit("invalid ADLFS_LOG_LEVEL '{0}'".format(log_level)) |
| 470 |
|
| 471 |
logging.basicConfig(level=log_level) |
| 472 |
|
| 473 |
|
| 474 |
if __name__ == '__main__': |
| 475 |
setup_logging() |
| 476 |
fs = AzureDLFileSystem() |
| 477 |
if len(sys.argv) > 1: |
| 478 |
AzureDataLakeFSCommand(fs).onecmd(' '.join(sys.argv[1:])) |
| 479 |
else: |
| 480 |
AzureDataLakeFSCommand(fs).cmdloop() |