Skip to content

Commit 502b498

Browse files
committed
fix string condition, move bounded-wannabe method as function
1 parent aab85cb commit 502b498

File tree

1 file changed

+32
-29
lines changed

1 file changed

+32
-29
lines changed

nipype/interfaces/io.py

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,35 @@ def add_traits(base, names, trait_type=None):
123123
return base
124124

125125

126+
def get_head_bucket(s3_resource, bucket_name):
127+
""" Try to get the header info of a bucket, in order to
128+
check if it exists and its permissions
129+
"""
130+
131+
import botocore
132+
133+
# Try fetch the bucket with the name argument
134+
try:
135+
s3_resource.meta.client.head_bucket(Bucket=bucket_name)
136+
except botocore.exceptions.ClientError as exc:
137+
error_code = int(exc.response['Error']['Code'])
138+
if error_code == 403:
139+
err_msg = 'Access to bucket: %s is denied; check credentials'\
140+
% bucket_name
141+
raise Exception(err_msg)
142+
elif error_code == 404:
143+
err_msg = 'Bucket: %s does not exist; check spelling and try '\
144+
'again' % bucket_name
145+
raise Exception(err_msg)
146+
else:
147+
err_msg = 'Unable to connect to bucket: %s. Error message:\n%s'\
148+
% (bucket_name, exc)
149+
except Exception as exc:
150+
err_msg = 'Unable to connect to bucket: %s. Error message:\n%s'\
151+
% (bucket_name, exc)
152+
raise Exception(err_msg)
153+
154+
126155
class IOBase(BaseInterface):
127156
def _run_interface(self, runtime):
128157
return runtime
@@ -548,15 +577,15 @@ def _fetch_bucket(self, bucket_name):
548577

549578
# And try fetch the bucket with the name argument
550579
try:
551-
self._get_head_bucket(s3_resource, bucket_name)
580+
get_head_bucket(s3_resource, bucket_name)
552581
except Exception as exc:
553582

554583
# Try to connect anonymously
555584
s3_resource.meta.client.meta.events.register(
556585
'choose-signer.s3.*', botocore.handlers.disable_signing)
557586

558587
iflogger.info('Connecting to AWS: %s anonymously...', bucket_name)
559-
self._get_head_bucket(s3_resource, bucket_name)
588+
get_head_bucket(s3_resource, bucket_name)
560589

561590
# Explicitly declare a secure SSL connection for bucket object
562591
bucket = s3_resource.Bucket(bucket_name)
@@ -565,32 +594,6 @@ def _fetch_bucket(self, bucket_name):
565594
return bucket
566595

567596

568-
def _get_head_bucket(self, s3_resource, bucket_name):
569-
570-
import botocore
571-
572-
# And try fetch the bucket with the name argument
573-
try:
574-
s3_resource.meta.client.head_bucket(Bucket=bucket_name)
575-
except botocore.exceptions.ClientError as exc:
576-
error_code = int(exc.response['Error']['Code'])
577-
if error_code == 403:
578-
err_msg = 'Access to bucket: %s is denied; check credentials'\
579-
% bucket_name
580-
raise Exception(err_msg)
581-
elif error_code == 404:
582-
err_msg = 'Bucket: %s does not exist; check spelling and try '\
583-
'again' % bucket_name
584-
raise Exception(err_msg)
585-
else:
586-
err_msg = 'Unable to connect to bucket: %s. Error message:\n%s'\
587-
% (bucket_name, exc)
588-
except Exception as exc:
589-
err_msg = 'Unable to connect to bucket: %s. Error message:\n%s'\
590-
% (bucket_name, exc)
591-
raise Exception(err_msg)
592-
593-
594597
# Send up to S3 method
595598
def _upload_to_s3(self, bucket, src, dst):
596599
'''
@@ -610,7 +613,7 @@ def _upload_to_s3(self, bucket, src, dst):
610613
s3_prefix = s3_str + bucket.name
611614

612615
# Explicitly lower-case the "s3"
613-
if dst[:len(s3_str)].lower().startswith(s3_str):
616+
if dst[:len(s3_str)].lower() == s3_str:
614617
dst = s3_str + dst[len(s3_str):]
615618

616619
# If src is a directory, collect files (this assumes dst is a dir too)

0 commit comments

Comments
 (0)