Skip to content
This repository was archived by the owner on Dec 27, 2022. It is now read-only.

Commit ea7a384

Browse files
committed
enh/wip: dmriprep-upload CLI to make it easy to upload results to S3
1 parent 4e3cd12 commit ea7a384

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

dmriprep/cli.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,5 +59,50 @@ def data(output_dir):
5959
print('done')
6060

6161

62+
@click.command()
63+
@click.argument('output_dir')
64+
@click.argument('bucket')
65+
@click.option('--access_key', help="your AWS access key")
66+
@click.option('--secret_key', help="your AWS access secret")
67+
@click.option('--provider', default='s3', help="Cloud storage provider. Only S3 is supported right now.")
68+
@click.option('--subject', default=None, help="Subject id to upload (optional)")
69+
def upload(output_dir, bucket, access_key, secret_key, provider='s3', subject=None):
70+
"""
71+
OUTPUT_DIR: The directory where the output files were stored.
72+
73+
BUCKET: The cloud bucket name to upload data to.
74+
"""
75+
76+
import boto3
77+
from glob import glob
78+
79+
output_dir = os.path.abspath(output_dir)
80+
if not output_dir.endswith('/'):
81+
output_dir += '/'
82+
83+
if provider == 's3' or provider == 'S3':
84+
client = boto3.client('s3', aws_access_key_id=access_key, aws_secret_access_key=secret_key)
85+
86+
if subject is not None:
87+
assert os.path.exists(os.path.join(output_dir, subject)), 'this subject id does not exist!'
88+
subjects = [subject]
89+
else:
90+
subjects = [os.path.split(s)[1] for s in glob(os.path.join(output_dir, 'sub-*'))]
91+
92+
for s in subjects:
93+
base_dir = os.path.join(output_dir, s, 'dmriprep')
94+
for root, dirs, files in os.walk(base_dir):
95+
for f in files:
96+
filepath = os.path.join(root, f)
97+
key = root.replace(output_dir, '')
98+
# TODO: progress bar on this!!
99+
client.upload_file(filepath, bucket, os.path.join(key, f))
100+
101+
102+
103+
else:
104+
raise NotImplementedError('Only S3 is the only supported provider for data uploads at the moment')
105+
106+
62107
if __name__ == "__main__":
63108
sys.exit(main()) # pragma: no cover

setup.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@
4141
entry_points={
4242
'console_scripts': [
4343
'dmriprep=dmriprep.cli:main',
44-
'dmriprep-data=dmriprep.cli:data'
44+
'dmriprep-data=dmriprep.cli:data',
45+
'dmriprep-upload=dmriprep.cli:upload',
4546
],
4647
},
4748
install_requires=requirements,

0 commit comments

Comments
 (0)