swift is written in python 2.6

make swift client work in python 2.4
changes:
1. md5 interface changed
2. urlparse interface changed
3. Queue interface changed
This commit is contained in:
anthony 2011-10-13 12:07:09 -07:00
parent 01d38fbb3e
commit 6710f09ab4

View File

@ -15,7 +15,7 @@
# limitations under the License.
from errno import EEXIST, ENOENT
from hashlib import md5
import md5
from optparse import OptionParser
from os import environ, listdir, makedirs, utime
from os.path import basename, dirname, getmtime, getsize, isdir, join
@ -161,14 +161,16 @@ def http_connection(url, proxy=None):
:raises ClientException: Unable to handle protocol scheme
"""
parsed = urlparse(url)
proxy_parsed = urlparse(proxy) if proxy else None
if parsed.scheme == 'http':
conn = HTTPConnection((proxy_parsed if proxy else parsed).netloc)
elif parsed.scheme == 'https':
conn = HTTPSConnection((proxy_parsed if proxy else parsed).netloc)
proxy_parsed = parsed
if proxy:
proxy_parsed = urlparse(proxy)
if parsed[0] == 'http':
conn = HTTPConnection(proxy_parsed[1])
elif parsed[0] == 'https':
conn = HTTPSConnection(proxy_parsed[1])
else:
raise ClientException('Cannot handle protocol scheme %s for url %s' %
(parsed.scheme, repr(url)))
(parsed[0], repr(url)))
if proxy:
conn._set_tunnel(parsed.hostname, parsed.port)
return parsed, conn
@ -192,14 +194,14 @@ def get_auth(url, user, key, snet=False):
:raises ClientException: HTTP GET request to auth URL failed
"""
parsed, conn = http_connection(url)
conn.request('GET', parsed.path, '',
conn.request('GET', parsed[2], '',
{'X-Auth-User': user, 'X-Auth-Key': key})
resp = conn.getresponse()
resp.read()
if resp.status < 200 or resp.status >= 300:
raise ClientException('Auth GET failed', http_scheme=parsed.scheme,
raise ClientException('Auth GET failed', http_scheme=parsed[0],
http_host=conn.host, http_port=conn.port,
http_path=parsed.path, http_status=resp.status,
http_path=parsed[2], http_status=resp.status,
http_reason=resp.reason)
url = resp.getheader('x-storage-url')
if snet:
@ -249,7 +251,7 @@ def get_account(url, token, marker=None, limit=None, prefix=None,
qs += '&limit=%d' % limit
if prefix:
qs += '&prefix=%s' % quote(prefix)
conn.request('GET', '%s?%s' % (parsed.path, qs), '',
conn.request('GET', '%s?%s' % (parsed[2], qs), '',
{'X-Auth-Token': token})
resp = conn.getresponse()
resp_headers = {}
@ -257,9 +259,9 @@ def get_account(url, token, marker=None, limit=None, prefix=None,
resp_headers[header.lower()] = value
if resp.status < 200 or resp.status >= 300:
resp.read()
raise ClientException('Account GET failed', http_scheme=parsed.scheme,
raise ClientException('Account GET failed', http_scheme=parsed[0],
http_host=conn.host, http_port=conn.port,
http_path=parsed.path, http_query=qs, http_status=resp.status,
http_path=parsed[2], http_query=qs, http_status=resp.status,
http_reason=resp.reason)
if resp.status == 204:
resp.read()
@ -283,13 +285,13 @@ def head_account(url, token, http_conn=None):
parsed, conn = http_conn
else:
parsed, conn = http_connection(url)
conn.request('HEAD', parsed.path, '', {'X-Auth-Token': token})
conn.request('HEAD', parsed[2], '', {'X-Auth-Token': token})
resp = conn.getresponse()
resp.read()
if resp.status < 200 or resp.status >= 300:
raise ClientException('Account HEAD failed', http_scheme=parsed.scheme,
raise ClientException('Account HEAD failed', http_scheme=parsed[0],
http_host=conn.host, http_port=conn.port,
http_path=parsed.path, http_status=resp.status,
http_path=parsed[2], http_status=resp.status,
http_reason=resp.reason)
resp_headers = {}
for header, value in resp.getheaders():
@ -313,12 +315,12 @@ def post_account(url, token, headers, http_conn=None):
else:
parsed, conn = http_connection(url)
headers['X-Auth-Token'] = token
conn.request('POST', parsed.path, '', headers)
conn.request('POST', parsed[2], '', headers)
resp = conn.getresponse()
resp.read()
if resp.status < 200 or resp.status >= 300:
raise ClientException('Account POST failed',
http_scheme=parsed.scheme, http_host=conn.host,
http_scheme=parsed[0], http_host=conn.host,
http_port=conn.port, http_path=path, http_status=resp.status,
http_reason=resp.reason)
@ -361,7 +363,7 @@ def get_container(url, token, container, marker=None, limit=None,
rv[1].extend(listing)
return rv
parsed, conn = http_conn
path = '%s/%s' % (parsed.path, quote(container))
path = '%s/%s' % (parsed[2], quote(container))
qs = 'format=json'
if marker:
qs += '&marker=%s' % quote(marker)
@ -376,7 +378,7 @@ def get_container(url, token, container, marker=None, limit=None,
if resp.status < 200 or resp.status >= 300:
resp.read()
raise ClientException('Container GET failed',
http_scheme=parsed.scheme, http_host=conn.host,
http_scheme=parsed[0], http_host=conn.host,
http_port=conn.port, http_path=path, http_query=qs,
http_status=resp.status, http_reason=resp.reason)
resp_headers = {}
@ -405,13 +407,13 @@ def head_container(url, token, container, http_conn=None):
parsed, conn = http_conn
else:
parsed, conn = http_connection(url)
path = '%s/%s' % (parsed.path, quote(container))
path = '%s/%s' % (parsed[2], quote(container))
conn.request('HEAD', path, '', {'X-Auth-Token': token})
resp = conn.getresponse()
resp.read()
if resp.status < 200 or resp.status >= 300:
raise ClientException('Container HEAD failed',
http_scheme=parsed.scheme, http_host=conn.host,
http_scheme=parsed[0], http_host=conn.host,
http_port=conn.port, http_path=path, http_status=resp.status,
http_reason=resp.reason)
resp_headers = {}
@ -436,7 +438,7 @@ def put_container(url, token, container, headers=None, http_conn=None):
parsed, conn = http_conn
else:
parsed, conn = http_connection(url)
path = '%s/%s' % (parsed.path, quote(container))
path = '%s/%s' % (parsed[2], quote(container))
if not headers:
headers = {}
headers['X-Auth-Token'] = token
@ -445,7 +447,7 @@ def put_container(url, token, container, headers=None, http_conn=None):
resp.read()
if resp.status < 200 or resp.status >= 300:
raise ClientException('Container PUT failed',
http_scheme=parsed.scheme, http_host=conn.host,
http_scheme=parsed[0], http_host=conn.host,
http_port=conn.port, http_path=path, http_status=resp.status,
http_reason=resp.reason)
@ -466,14 +468,14 @@ def post_container(url, token, container, headers, http_conn=None):
parsed, conn = http_conn
else:
parsed, conn = http_connection(url)
path = '%s/%s' % (parsed.path, quote(container))
path = '%s/%s' % (parsed[2], quote(container))
headers['X-Auth-Token'] = token
conn.request('POST', path, '', headers)
resp = conn.getresponse()
resp.read()
if resp.status < 200 or resp.status >= 300:
raise ClientException('Container POST failed',
http_scheme=parsed.scheme, http_host=conn.host,
http_scheme=parsed[0], http_host=conn.host,
http_port=conn.port, http_path=path, http_status=resp.status,
http_reason=resp.reason)
@ -493,13 +495,13 @@ def delete_container(url, token, container, http_conn=None):
parsed, conn = http_conn
else:
parsed, conn = http_connection(url)
path = '%s/%s' % (parsed.path, quote(container))
path = '%s/%s' % (parsed[2], quote(container))
conn.request('DELETE', path, '', {'X-Auth-Token': token})
resp = conn.getresponse()
resp.read()
if resp.status < 200 or resp.status >= 300:
raise ClientException('Container DELETE failed',
http_scheme=parsed.scheme, http_host=conn.host,
http_scheme=parsed[0], http_host=conn.host,
http_port=conn.port, http_path=path, http_status=resp.status,
http_reason=resp.reason)
@ -527,12 +529,12 @@ def get_object(url, token, container, name, http_conn=None,
parsed, conn = http_conn
else:
parsed, conn = http_connection(url)
path = '%s/%s/%s' % (parsed.path, quote(container), quote(name))
path = '%s/%s/%s' % (parsed[2], quote(container), quote(name))
conn.request('GET', path, '', {'X-Auth-Token': token})
resp = conn.getresponse()
if resp.status < 200 or resp.status >= 300:
resp.read()
raise ClientException('Object GET failed', http_scheme=parsed.scheme,
raise ClientException('Object GET failed', http_scheme=parsed[0],
http_host=conn.host, http_port=conn.port, http_path=path,
http_status=resp.status, http_reason=resp.reason)
if resp_chunk_size:
@ -569,12 +571,12 @@ def head_object(url, token, container, name, http_conn=None):
parsed, conn = http_conn
else:
parsed, conn = http_connection(url)
path = '%s/%s/%s' % (parsed.path, quote(container), quote(name))
path = '%s/%s/%s' % (parsed[2], quote(container), quote(name))
conn.request('HEAD', path, '', {'X-Auth-Token': token})
resp = conn.getresponse()
resp.read()
if resp.status < 200 or resp.status >= 300:
raise ClientException('Object HEAD failed', http_scheme=parsed.scheme,
raise ClientException('Object HEAD failed', http_scheme=parsed[0],
http_host=conn.host, http_port=conn.port, http_path=path,
http_status=resp.status, http_reason=resp.reason)
resp_headers = {}
@ -618,7 +620,7 @@ def put_object(url, token=None, container=None, name=None, contents=None,
parsed, conn = http_conn
else:
parsed, conn = http_connection(url, proxy=proxy)
path = parsed.path
path = parsed[2]
if container:
path = '%s/%s' % (path.rstrip('/'), quote(container))
if name:
@ -668,7 +670,7 @@ def put_object(url, token=None, container=None, name=None, contents=None,
resp = conn.getresponse()
resp.read()
if resp.status < 200 or resp.status >= 300:
raise ClientException('Object PUT failed', http_scheme=parsed.scheme,
raise ClientException('Object PUT failed', http_scheme=parsed[0],
http_host=conn.host, http_port=conn.port, http_path=path,
http_status=resp.status, http_reason=resp.reason)
return resp.getheader('etag', '').strip('"')
@ -691,13 +693,13 @@ def post_object(url, token, container, name, headers, http_conn=None):
parsed, conn = http_conn
else:
parsed, conn = http_connection(url)
path = '%s/%s/%s' % (parsed.path, quote(container), quote(name))
path = '%s/%s/%s' % (parsed[2], quote(container), quote(name))
headers['X-Auth-Token'] = token
conn.request('POST', path, '', headers)
resp = conn.getresponse()
resp.read()
if resp.status < 200 or resp.status >= 300:
raise ClientException('Object POST failed', http_scheme=parsed.scheme,
raise ClientException('Object POST failed', http_scheme=parsed[0],
http_host=conn.host, http_port=conn.port, http_path=path,
http_status=resp.status, http_reason=resp.reason)
@ -724,7 +726,7 @@ def delete_object(url, token=None, container=None, name=None, http_conn=None,
parsed, conn = http_conn
else:
parsed, conn = http_connection(url, proxy=proxy)
path = parsed.path
path = parsed[2]
if container:
path = '%s/%s' % (path.rstrip('/'), quote(container))
if name:
@ -740,7 +742,7 @@ def delete_object(url, token=None, container=None, name=None, http_conn=None,
resp.read()
if resp.status < 200 or resp.status >= 300:
raise ClientException('Object DELETE failed',
http_scheme=parsed.scheme, http_host=conn.host,
http_scheme=parsed[0], http_host=conn.host,
http_port=conn.port, http_path=path, http_status=resp.status,
http_reason=resp.reason)
@ -947,7 +949,7 @@ class QueueFunctionThread(Thread):
item = self.queue.get_nowait()
if not self.abort:
self.func(item, *self.args, **self.kwargs)
self.queue.task_done()
#self.queue.task_done()
except Empty:
if self.abort:
break
@ -1196,7 +1198,7 @@ def st_download(options, args, print_queue, error_queue):
fp = open(path, 'wb')
read_length = 0
if 'x-object-manifest' not in headers:
md5sum = md5()
md5sum = md5.new()
for chunk in body:
fp.write(chunk)
read_length += len(chunk)