Skip to content

Commit

Permalink
Upload split session to cache path first
Browse files Browse the repository at this point in the history
  • Loading branch information
andylassiter committed Mar 21, 2024
1 parent 4420859 commit 9f28311
Showing 1 changed file with 32 additions and 5 deletions.
37 changes: 32 additions & 5 deletions run.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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'
Expand All @@ -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')
Expand Down

0 comments on commit 9f28311

Please sign in to comment.