|
| 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() |
0 commit comments