Skip to content

Commit 67c880a

Browse files
committed
Improve handling of TransferData
1 parent 5a6c7f0 commit 67c880a

File tree

2 files changed

+69
-36
lines changed

2 files changed

+69
-36
lines changed

zstash/globus.py

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@
1010

1111
from .globus_utils import (
1212
HPSS_ENDPOINT_MAP,
13+
add_file_to_TransferData,
1314
check_state_files,
15+
create_TransferData,
16+
get_label,
1417
get_local_endpoint_id,
1518
get_transfer_client_with_auth,
16-
set_up_TransferData,
1719
submit_transfer_with_checks,
1820
)
1921
from .settings import logger
@@ -122,14 +124,38 @@ def globus_transfer( # noqa: C901
122124
raise RuntimeError(
123125
"The transfer manager should always have at least one batch by the time globus_transfer is called, however, the batch list is empty."
124126
)
125-
transfer_data = set_up_TransferData(
127+
128+
if transfer_manager.globus_config.local_endpoint:
129+
local_endpoint: str = transfer_manager.globus_config.local_endpoint
130+
else:
131+
raise ValueError("Local endpoint ID is not set.")
132+
if transfer_manager.globus_config.remote_endpoint:
133+
remote_endpoint: str = transfer_manager.globus_config.remote_endpoint
134+
else:
135+
raise ValueError("Remote endpoint ID is not set.")
136+
label = get_label(remote_path, name)
137+
138+
transfer_data: TransferData
139+
if mrb.transfer_data:
140+
# We already have a TransferData for this batch.
141+
transfer_data = mrb.transfer_data
142+
else:
143+
# We need to create a new TransferData for this batch.
144+
transfer_data = create_TransferData(
145+
transfer_type,
146+
local_endpoint,
147+
remote_endpoint,
148+
transfer_manager.globus_config.transfer_client,
149+
label,
150+
)
151+
add_file_to_TransferData(
126152
transfer_type,
127-
transfer_manager.globus_config.local_endpoint,
128-
transfer_manager.globus_config.remote_endpoint,
153+
local_endpoint,
154+
remote_endpoint,
129155
remote_path,
130156
name,
131-
transfer_manager.globus_config.transfer_client,
132-
mrb.transfer_data,
157+
transfer_data,
158+
label,
133159
)
134160

135161
task: GlobusHTTPResponse
@@ -215,9 +241,6 @@ def globus_transfer( # noqa: C901
215241
error_str = "transfer_manager has no batches"
216242
logger.error(error_str)
217243
raise RuntimeError(error_str)
218-
219-
# Nullify the submitted transfer data structure so that a new one will be created on next call.
220-
transfer_data = None
221244
except TransferAPIError as e:
222245
if e.code == "NoCredException":
223246
logger.error(

zstash/globus_utils.py

Lines changed: 37 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -217,51 +217,61 @@ def save_tokens(token_response):
217217

218218

219219
# Primarily used by globus_transfer ###########################################
220-
def set_up_TransferData(
220+
def get_label(remote_path: str, name: str) -> str:
221+
subdir = os.path.basename(os.path.normpath(remote_path))
222+
subdir_label = re.sub("[^A-Za-z0-9_ -]", "", subdir)
223+
filename = name.split(".")[0]
224+
label = subdir_label + " " + filename
225+
return label
226+
227+
228+
def create_TransferData(
229+
transfer_type: str,
230+
local_endpoint: str,
231+
remote_endpoint: str,
232+
transfer_client: TransferClient,
233+
label: str,
234+
) -> TransferData:
235+
if transfer_type == "get":
236+
src_ep = remote_endpoint
237+
dst_ep = local_endpoint
238+
else:
239+
src_ep = local_endpoint
240+
dst_ep = remote_endpoint
241+
transfer_data = TransferData(
242+
transfer_client,
243+
src_ep,
244+
dst_ep,
245+
label=label,
246+
verify_checksum=True,
247+
preserve_timestamp=True,
248+
fail_on_quota_errors=True,
249+
)
250+
return transfer_data
251+
252+
253+
def add_file_to_TransferData(
221254
transfer_type: str,
222255
local_endpoint: Optional[str],
223256
remote_endpoint: Optional[str],
224257
remote_path: str,
225258
name: str,
226-
transfer_client: TransferClient,
227-
transfer_data: Optional[TransferData] = None,
228-
) -> TransferData:
229-
"""
230-
Set up the TransferData object, creating one if not provided.
231-
"""
259+
transfer_data: TransferData,
260+
label: str,
261+
):
232262
if not local_endpoint:
233263
raise ValueError("Local endpoint ID is not set.")
234264
if not remote_endpoint:
235265
raise ValueError("Remote endpoint ID is not set.")
236266
if transfer_type == "get":
237-
src_ep = remote_endpoint
238267
src_path = os.path.join(remote_path, name)
239-
dst_ep = local_endpoint
240268
dst_path = os.path.join(os.getcwd(), name)
241269
else:
242-
src_ep = local_endpoint
243270
src_path = os.path.join(os.getcwd(), name)
244-
dst_ep = remote_endpoint
245271
dst_path = os.path.join(remote_path, name)
246272

247-
subdir = os.path.basename(os.path.normpath(remote_path))
248-
subdir_label = re.sub("[^A-Za-z0-9_ -]", "", subdir)
249-
filename = name.split(".")[0]
250-
label = subdir_label + " " + filename
251-
252-
if not transfer_data:
253-
transfer_data = TransferData(
254-
transfer_client,
255-
src_ep,
256-
dst_ep,
257-
label=label,
258-
verify_checksum=True,
259-
preserve_timestamp=True,
260-
fail_on_quota_errors=True,
261-
)
262273
transfer_data.add_item(src_path, dst_path)
263274
transfer_data["label"] = label
264-
return transfer_data
265275

266276

267277
def submit_transfer_with_checks(transfer_client, transfer_data) -> GlobusHTTPResponse:

0 commit comments

Comments
 (0)