Don't catch too much in zuul_swift_upload

Avoid catching errors inside of magic libraries by not using
Except and instead checking if the 'from_file' attribute exists
to determine which library we might be using.

From feedback on
https://review.openstack.org/#/c/104411/1/modules/openstack_project/files/slave_scripts/zuul_swift_upload.py

Change-Id: If3386e5903e1fb2ff65345c4e7825ae86b7a414c
This commit is contained in:
Joshua Hesketh 2014-07-03 12:11:11 +10:00
parent cf58a52c6e
commit 6698608e18

View File

@ -56,6 +56,21 @@ def make_index_file(file_list, logserver_prefix, swift_destination_prefix,
return os.path.join(tempdir, index_filename)
def get_file_mime(file_path):
"""Get the file mime using libmagic"""
if not os.path.isfile(file_path):
return None
if hasattr(magic, 'from_file'):
return magic.from_file(file_path, mime=True)
else:
# no magic.from_file, we might be using the libmagic bindings
m = magic.open(magic.MIME)
m.load()
return m.file(file_path).split(';')[0]
def swift_form_post_submit(file_list, url, hmac_body, signature):
"""Send the files to swift via the FormPost middleware"""
@ -78,16 +93,8 @@ def swift_form_post_submit(file_list, url, hmac_body, signature):
files = {}
for i, f in enumerate(file_list):
try:
file_mime = magic.from_file(f['path'], mime=True)
except AttributeError:
# no magic.from_file, we might be using the libmagic bindings
m = magic.open(magic.MIME)
m.load()
file_mime = m.file(f['path']).split(';')[0]
files['file%d' % (i + 1)] = (f['filename'], open(f['path'], 'rb'),
file_mime)
get_file_mime(f['path']))
requests.post(url, data=payload, files=files)