|
| 1 | +import argparse |
| 2 | +import json |
| 3 | +import os |
| 4 | + |
| 5 | +from smbprotocol.connection import Connection |
| 6 | +from smbprotocol.session import Session |
| 7 | +from smbprotocol.tree import TreeConnect |
| 8 | +from smbprotocol.open import Open, CreateDisposition, FileAttributes |
| 9 | +# from smbprotocol.open import Open, CreateDisposition, FileAttributes, FileInformationClass |
| 10 | +# from smbprotocol.file_info import FileStandardInformation |
| 11 | +# from smbprotocol.exceptions import SMBResponseException |
| 12 | + |
| 13 | + |
| 14 | +def sync_smb_to_local(smb_server, smb_share, smb_username, smb_password, smb_path, local_dir): |
| 15 | + # Establish an SMB connection |
| 16 | + connection = Connection(guid=os.urandom(16), server_name=smb_server, port=445) |
| 17 | + connection.connect() |
| 18 | + |
| 19 | + session = Session(connection, smb_username, smb_password) |
| 20 | + session.connect() |
| 21 | + |
| 22 | + tree = TreeConnect(session, f"\\\\{smb_server}\\{smb_share}") |
| 23 | + tree.connect() |
| 24 | + |
| 25 | + def download_directory(smb_base_path, local_base_path): |
| 26 | + with Open(tree, smb_base_path, create_disposition=CreateDisposition.FILE_OPEN) as smb_dir: |
| 27 | + smb_files = smb_dir.query_directory("*") |
| 28 | + |
| 29 | + # Ensure the local directory exists |
| 30 | + if not os.path.exists(local_base_path): |
| 31 | + os.makedirs(local_base_path) |
| 32 | + |
| 33 | + for smb_file in smb_files: |
| 34 | + file_name = smb_file['file_name'].get_value() |
| 35 | + smb_file_path = os.path.join(smb_base_path, file_name).replace("\\", "/") |
| 36 | + local_file_path = os.path.join(local_base_path, file_name) |
| 37 | + |
| 38 | + # Check if it's a directory or a file |
| 39 | + if smb_file['file_attributes'].has_flag(FileAttributes.FILE_ATTRIBUTE_DIRECTORY): |
| 40 | + if file_name not in [".", ".."]: # Skip '.' and '..' directories |
| 41 | + download_directory(smb_file_path, local_file_path) |
| 42 | + else: |
| 43 | + # Download the file |
| 44 | + with Open(tree, smb_file_path, create_disposition=CreateDisposition.FILE_OPEN) as smb_file_open: |
| 45 | + with open(local_file_path, 'wb') as local_file: |
| 46 | + data = smb_file_open.read(0, smb_file_open.get_maximum_read_size()) |
| 47 | + while data: |
| 48 | + local_file.write(data) |
| 49 | + data = smb_file_open.read(len(data), smb_file_open.get_maximum_read_size()) |
| 50 | + |
| 51 | + # Start downloading the directory structure |
| 52 | + download_directory(smb_path, local_dir) |
| 53 | + |
| 54 | + # Disconnect the SMB connection |
| 55 | + tree.disconnect() |
| 56 | + session.disconnect() |
| 57 | + connection.disconnect() |
| 58 | + |
| 59 | + |
| 60 | +def run_sync(args): |
| 61 | + output_root = "/mnt/lustre-emmy-hdd/projects/nim00007/data/moser/lightsheet/volumes" |
| 62 | + |
| 63 | + smb_server = "wfs-medizin.top.gwdg.de" |
| 64 | + smb_share = "ukon-all$" |
| 65 | + with open("./credentials.json") as f: |
| 66 | + credentials = json.load(f) |
| 67 | + smb_username = credentials["user"] |
| 68 | + smb_password = credentials["password"] |
| 69 | + |
| 70 | + smb_source_path = args.smb_path |
| 71 | + volume_name = args.volume_name |
| 72 | + |
| 73 | + local_directory = os.path.join(output_root, volume_name) |
| 74 | + sync_smb_to_local(smb_server, smb_share, smb_username, smb_password, smb_source_path, local_directory) |
| 75 | + |
| 76 | + |
| 77 | +def main(): |
| 78 | + parser = argparse.ArgumentParser() |
| 79 | + parser.add_argument("smb_path") |
| 80 | + parser.add_argument("volume_name") |
| 81 | + args = parser.parse_args() |
| 82 | + run_sync(args) |
| 83 | + |
| 84 | + |
| 85 | +if __name__ == "__main__": |
| 86 | + main() |
0 commit comments