diff options
-rwxr-xr-x | linaro-cp.py | 22 |
1 files changed, 18 insertions, 4 deletions
diff --git a/linaro-cp.py b/linaro-cp.py index 9dfa919..43f9aaa 100755 --- a/linaro-cp.py +++ b/linaro-cp.py @@ -158,7 +158,7 @@ class API_v3(API_v1): return super(API_v3, self)._upload_data( url, data, headers, retry_count) - def _put_s3(self, url, filename, mtype): + def _put_s3(self, url, filename, mtype, retry_count=3): response = cStringIO.StringIO() size = os.path.getsize(filename) headers = ['Content-Type: ' + mtype] @@ -168,10 +168,24 @@ class API_v3(API_v1): c.setopt(pycurl.INFILESIZE, size) c.setopt(pycurl.PUT, 1) c.setopt(pycurl.WRITEFUNCTION, response.write) - with open(filename, 'rb') as f: - c.setopt(pycurl.INFILE, f) - c.perform() + try: + with open(filename, 'rb') as f: + c.setopt(pycurl.INFILE, f) + c.perform() + except Exception as e: + if retry_count > 0: + # server could be reloading or something. give it a second and + # try again + print('Upload failed for %s, retrying in 2 seconds' % url) + time.sleep(2) + return self._put_s3(url, filename, mtype, retry_count - 1) + else: + return repr(e) code = c.getinfo(pycurl.RESPONSE_CODE) + if code == 503 and retry_count > 0: + print('503 failure for %s, retrying in 2 seconds' % url) + time.sleep(2) + return self._put_s3(url, filename, mtype, retry_count - 1) if code not in (200, 201): return response.getvalue() |