From 9f28311393681d6ab4268e7819d1a00abcd131fe Mon Sep 17 00:00:00 2001 From: Andy Lassiter Date: Thu, 21 Mar 2024 15:46:54 -0500 Subject: [PATCH] Upload split session to cache path first --- run.py | 37 ++++++++++++++++++++++++++++++++----- 1 file changed, 32 insertions(+), 5 deletions(-) diff --git a/run.py b/run.py index 96cb990..22a8c8e 100644 --- a/run.py +++ b/run.py @@ -5,6 +5,8 @@ import zipfile from pathlib import Path from typing import Union +from urllib.parse import quote +from datetime import datetime import requests from requests.auth import HTTPBasicAuth @@ -138,6 +140,29 @@ def send_to_xnat(xnat_username: str, xnat_password: str, xnat_host: str, project: str, subject: str, experiment: str, zip_file: Union[Path, str]): + # First upload to user cache + now = datetime.now() + upload_id = now.strftime("%Y_%m_%d_%H_%M_%S") + cache_path = f'/user/cache/resources/${upload_id}/files/${zip_file.name}' + encoded_cached_path = quote(cache_path, safe='') + url = f'{xnat_host}/data/{encoded_cached_path}' + + logging.info(f'Uploading {zip_file} to cache') + logging.debug(f'Cache URL: {url}') + + r = requests.put( + url, + auth=HTTPBasicAuth(xnat_username, xnat_password), + files={'file': open(zip_file, 'rb')} + ) + + if r.ok: + logging.info('Upload to cache successful') + else: + logging.error(f'Upload to cache failed: {r.text}') + raise Exception(f'Upload to cache failed for {zip_file}') + + # Then import from cache to pre-archive experiment = experiment + "_split_" + subject url = f'{xnat_host}/data/services/import' @@ -146,15 +171,17 @@ def send_to_xnat(xnat_username: str, xnat_password: str, xnat_host: str, 'overwrite': 'append', 'PROJECT_ID': project, 'SUBJECT_ID': subject, - 'EXPT_LABEL': experiment + 'EXPT_LABEL': experiment, + 'cachePath': f'{upload_id}/{zip_file.name}' } logging.info(f'Uploading {zip_file} for project {project}, subject {subject}, experiment {experiment} to {url}') - r = requests.post(url, - auth=HTTPBasicAuth(xnat_username, xnat_password), - params=params, - files={'file': open(zip_file, 'rb')}) + r = requests.post( + url, + auth=HTTPBasicAuth(xnat_username, xnat_password), + params=params + ) if r.ok: logging.info('Upload successful')