2023-05-10 18:17:35 +09:00
|
|
|
import os
|
2023-07-19 21:47:47 +09:00
|
|
|
from argparse import Namespace
|
2023-05-10 18:17:35 +09:00
|
|
|
from concurrent.futures import ThreadPoolExecutor
|
2023-07-19 21:47:47 +09:00
|
|
|
from typing import Any
|
2023-05-10 18:17:35 +09:00
|
|
|
|
|
|
|
from pydantic.dataclasses import dataclass
|
|
|
|
|
2023-12-12 20:05:46 +09:00
|
|
|
from mdrsclient.api import FilesApi, FoldersApi
|
2023-05-10 18:17:35 +09:00
|
|
|
from mdrsclient.commands.base import BaseCommand
|
|
|
|
from mdrsclient.connection import MDRSConnection
|
|
|
|
from mdrsclient.exceptions import IllegalArgumentException, MDRSException
|
|
|
|
from mdrsclient.models import Folder
|
|
|
|
from mdrsclient.settings import CONCURRENT
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
|
|
class UploadFileInfo:
|
|
|
|
folder: Folder
|
|
|
|
path: str
|
|
|
|
|
|
|
|
|
|
|
|
class UploadCommand(BaseCommand):
|
|
|
|
@classmethod
|
2023-07-19 21:47:47 +09:00
|
|
|
def register(cls, parsers: Any) -> None:
|
2023-05-10 18:17:35 +09:00
|
|
|
upload_parser = parsers.add_parser("upload", help="upload the file or directory")
|
|
|
|
upload_parser.add_argument(
|
|
|
|
"-r", "--recursive", help="upload directories and their contents recursive", action="store_true"
|
|
|
|
)
|
2024-02-13 10:48:01 +09:00
|
|
|
upload_parser.add_argument(
|
|
|
|
"-s",
|
|
|
|
"--skip-if-exists",
|
|
|
|
help="skip the upload if file is already uploaded and file size is the same",
|
|
|
|
action="store_true",
|
|
|
|
)
|
2023-05-10 18:17:35 +09:00
|
|
|
upload_parser.add_argument("local_path", help="local file path (/foo/bar/data.txt)")
|
|
|
|
upload_parser.add_argument("remote_path", help="remote folder path (remote:/lab/path/)")
|
2023-07-19 21:47:47 +09:00
|
|
|
upload_parser.set_defaults(func=cls.func)
|
2023-05-10 18:17:35 +09:00
|
|
|
|
2023-07-19 21:47:47 +09:00
|
|
|
@classmethod
|
|
|
|
def func(cls, args: Namespace) -> None:
|
|
|
|
local_path = str(args.local_path)
|
|
|
|
remote_path = str(args.remote_path)
|
|
|
|
is_recursive = bool(args.recursive)
|
2024-02-13 10:48:01 +09:00
|
|
|
is_skip_if_exists = bool(args.skip_if_exists)
|
|
|
|
cls.upload(local_path, remote_path, is_recursive, is_skip_if_exists)
|
2023-07-19 21:47:47 +09:00
|
|
|
|
|
|
|
@classmethod
|
2024-02-13 10:48:01 +09:00
|
|
|
def upload(cls, local_path: str, remote_path: str, is_recursive: bool, is_skip_if_exists: bool) -> None:
|
2023-07-19 21:47:47 +09:00
|
|
|
(remote, laboratory_name, r_path) = cls._parse_remote_host_with_path(remote_path)
|
2023-12-20 19:42:06 +09:00
|
|
|
l_path = os.path.abspath(local_path)
|
2023-05-10 18:17:35 +09:00
|
|
|
if not os.path.exists(l_path):
|
2023-07-19 21:47:47 +09:00
|
|
|
raise IllegalArgumentException(f"File or directory `{local_path}` not found.")
|
|
|
|
connection = cls._create_connection(remote)
|
|
|
|
laboratory = cls._find_laboratory(connection, laboratory_name)
|
|
|
|
folder = cls._find_folder(connection, laboratory, r_path)
|
2023-05-10 18:17:35 +09:00
|
|
|
infos: list[UploadFileInfo] = []
|
|
|
|
if os.path.isdir(l_path):
|
2023-07-19 21:47:47 +09:00
|
|
|
if not is_recursive:
|
|
|
|
raise IllegalArgumentException(f"Cannot upload `{local_path}`: Is a directory.")
|
2023-12-12 20:05:46 +09:00
|
|
|
folder_api = FoldersApi(connection)
|
2023-05-10 18:17:35 +09:00
|
|
|
folder_map: dict[str, Folder] = {}
|
|
|
|
folder_map[r_path] = folder
|
|
|
|
l_basename = os.path.basename(l_path)
|
2023-12-20 19:42:06 +09:00
|
|
|
for dirpath, _, filenames in os.walk(l_path, followlinks=True):
|
|
|
|
sub = l_basename if dirpath == l_path else os.path.join(l_basename, os.path.relpath(dirpath, l_path))
|
|
|
|
d_dirname = os.path.join(r_path, sub)
|
2023-05-10 18:17:35 +09:00
|
|
|
d_basename = os.path.basename(d_dirname)
|
|
|
|
# prepare destination parent path
|
|
|
|
d_parent_dirname = os.path.dirname(d_dirname)
|
|
|
|
if folder_map.get(d_parent_dirname) is None:
|
2023-07-19 21:47:47 +09:00
|
|
|
folder_map[d_parent_dirname] = cls._find_folder(connection, laboratory, d_parent_dirname)
|
2023-05-10 18:17:35 +09:00
|
|
|
# prepare destination path
|
|
|
|
if folder_map.get(d_dirname) is None:
|
|
|
|
d_folder = folder_map[d_parent_dirname].find_sub_folder(d_basename)
|
|
|
|
if d_folder is None:
|
|
|
|
d_folder_id = folder_api.create(d_basename, folder_map[d_parent_dirname].id)
|
|
|
|
else:
|
|
|
|
d_folder_id = d_folder.id
|
|
|
|
print(d_dirname)
|
|
|
|
folder_map[d_dirname] = folder_api.retrieve(d_folder_id)
|
|
|
|
if d_folder is None:
|
|
|
|
folder_map[d_parent_dirname].sub_folders.append(folder_map[d_dirname])
|
|
|
|
# register upload file list
|
|
|
|
for filename in filenames:
|
|
|
|
infos.append(UploadFileInfo(folder_map[d_dirname], os.path.join(dirpath, filename)))
|
|
|
|
else:
|
|
|
|
infos.append(UploadFileInfo(folder, l_path))
|
2024-02-13 10:48:01 +09:00
|
|
|
cls.__multiple_upload(connection, infos, is_skip_if_exists)
|
2023-05-10 18:17:35 +09:00
|
|
|
|
2023-07-19 21:47:47 +09:00
|
|
|
@classmethod
|
2024-02-13 10:48:01 +09:00
|
|
|
def __multiple_upload(
|
|
|
|
cls, connection: MDRSConnection, infos: list[UploadFileInfo], is_skip_if_exists: bool
|
|
|
|
) -> None:
|
2023-12-12 20:05:46 +09:00
|
|
|
file_api = FilesApi(connection)
|
2023-05-10 18:17:35 +09:00
|
|
|
with ThreadPoolExecutor(max_workers=CONCURRENT) as pool:
|
2024-02-13 10:48:01 +09:00
|
|
|
pool.map(lambda x: cls.__multiple_upload_worker(file_api, x, is_skip_if_exists), infos)
|
2023-05-10 18:17:35 +09:00
|
|
|
|
2023-07-19 21:47:47 +09:00
|
|
|
@classmethod
|
2024-02-13 10:48:01 +09:00
|
|
|
def __multiple_upload_worker(cls, file_api: FilesApi, info: UploadFileInfo, is_skip_if_exists: bool) -> None:
|
2023-05-10 18:17:35 +09:00
|
|
|
basename = os.path.basename(info.path)
|
|
|
|
file = info.folder.find_file(basename)
|
|
|
|
try:
|
|
|
|
if file is None:
|
|
|
|
file_api.create(info.folder.id, info.path)
|
2024-02-13 10:48:01 +09:00
|
|
|
elif not is_skip_if_exists or file.size != os.path.getsize(info.path):
|
2023-05-10 18:17:35 +09:00
|
|
|
file_api.update(file, info.path)
|
|
|
|
print(os.path.join(info.folder.path, basename))
|
|
|
|
except MDRSException as e:
|
2024-07-04 12:33:57 +09:00
|
|
|
print(f"Error: {e}")
|