Skip to content

Commit adbe319

Browse files
Implement data transfer for moser data
1 parent 4703502 commit adbe319

File tree

3 files changed

+130
-0
lines changed

3 files changed

+130
-0
lines changed

scripts/data_transfer/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
credentials.json

scripts/data_transfer/README.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Data Transfer Moser
2+
3+
## Transfer via smbclient
4+
5+
Current approach to the data transfer:
6+
- Log in to SCC login node:
7+
$
8+
- Go to `/scratch1/projects/cca/data/moser`
9+
- Create subfolder <NAME> for cochlea to be copied
10+
- Log in via
11+
```
12+
$ smbclient \\\\wfs-medizin.top.gwdg.de\\ukon-all\$\\ukon100 -U GWDG\\pape41"
13+
```
14+
- Go to the folder with the cochlea to copy (cd works)
15+
- Copy the folder via:
16+
- recurse ON
17+
- prompt OFF
18+
- mget *
19+
- Copy this to HLRN by logging into it and running
20+
```
21+
$ rsync -e "ssh -i ~/.ssh/id_rsa_hlrn" -avz [email protected]:/scratch1/projects/cca/data/mose
22+
r/<NAME> /mnt/lustre-emmy-hdd/projects/nim00007/data/moser/lightsheet/volumes/<NAME>
23+
```
24+
- Remove on SCC
25+
26+
## Next files
27+
28+
- UKON100\archiv\imaging\Lightsheet\Huiskengroup_CTLSM\2024\M171_2R_converted_n5
29+
- unclear what the converted data is
30+
- UKON100\archiv\imaging\Lightsheet\Huiskengroup_CTLSM\2024\155_1L_converted_n5\BDVexport.n5
31+
- Copied to SCC, need to rsync.
32+
- UKON100\archiv\imaging\Lightsheet\Huiskengroup_CTLSM\2024\MLR151_2R_converted_n5
33+
- UKON100\archiv\imaging\Lightsheet\Huiskengroup_CTLSM\2024\G11_1L_converted_n5
34+
35+
## Improvements
36+
37+
Try to automate via https://github.com/jborean93/smbprotocol see `sync_smb.py` for ChatGPT's inital version.
38+
Connection not possible from HLRN.
39+
40+
## Transfer Back
41+
42+
For transfering back MoBIE results.
43+
...

scripts/data_transfer/sync_smb.py

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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

Comments
 (0)