Merge "fix some format"
This commit is contained in:
commit
58a924aebf
@ -27,12 +27,15 @@ import simplejson as json
|
|||||||
from nose import SkipTest
|
from nose import SkipTest
|
||||||
from xml.dom import minidom
|
from xml.dom import minidom
|
||||||
|
|
||||||
|
|
||||||
class AuthenticationFailed(Exception):
|
class AuthenticationFailed(Exception):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
class RequestError(Exception):
|
class RequestError(Exception):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
class ResponseError(Exception):
|
class ResponseError(Exception):
|
||||||
def __init__(self, response):
|
def __init__(self, response):
|
||||||
self.status = response.status
|
self.status = response.status
|
||||||
@ -45,15 +48,17 @@ class ResponseError(Exception):
|
|||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return '%d: %s' % (self.status, self.reason)
|
return '%d: %s' % (self.status, self.reason)
|
||||||
|
|
||||||
|
|
||||||
def listing_empty(method):
|
def listing_empty(method):
|
||||||
for i in xrange(0, 6):
|
for i in xrange(0, 6):
|
||||||
if len(method()) == 0:
|
if len(method()) == 0:
|
||||||
return True
|
return True
|
||||||
|
|
||||||
time.sleep(2**i)
|
time.sleep(2 ** i)
|
||||||
|
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
def listing_items(method):
|
def listing_items(method):
|
||||||
marker = None
|
marker = None
|
||||||
once = True
|
once = True
|
||||||
@ -64,17 +69,17 @@ def listing_items(method):
|
|||||||
yield i
|
yield i
|
||||||
|
|
||||||
if once or marker:
|
if once or marker:
|
||||||
if marker:
|
if marker:
|
||||||
items = method(parms={'marker':marker})
|
items = method(parms={'marker': marker})
|
||||||
else:
|
else:
|
||||||
items = method()
|
items = method()
|
||||||
|
|
||||||
if len(items) == 10000:
|
if len(items) == 10000:
|
||||||
marker = items[-1]
|
marker = items[-1]
|
||||||
else:
|
else:
|
||||||
marker = None
|
marker = None
|
||||||
|
|
||||||
once = False
|
once = False
|
||||||
else:
|
else:
|
||||||
items = []
|
items = []
|
||||||
|
|
||||||
@ -82,7 +87,7 @@ def listing_items(method):
|
|||||||
class Connection(object):
|
class Connection(object):
|
||||||
def __init__(self, config):
|
def __init__(self, config):
|
||||||
for key in 'auth_host auth_port auth_ssl username password'.split():
|
for key in 'auth_host auth_port auth_ssl username password'.split():
|
||||||
if not config.has_key(key):
|
if not key in config:
|
||||||
raise SkipTest
|
raise SkipTest
|
||||||
|
|
||||||
self.auth_host = config['auth_host']
|
self.auth_host = config['auth_host']
|
||||||
@ -123,10 +128,10 @@ class Connection(object):
|
|||||||
path = '%sv1.0' % (self.auth_prefix)
|
path = '%sv1.0' % (self.auth_prefix)
|
||||||
if self.auth_ssl:
|
if self.auth_ssl:
|
||||||
connection = httplib.HTTPSConnection(self.auth_host,
|
connection = httplib.HTTPSConnection(self.auth_host,
|
||||||
port=self.auth_port)
|
port=self.auth_port)
|
||||||
else:
|
else:
|
||||||
connection = httplib.HTTPConnection(self.auth_host,
|
connection = httplib.HTTPConnection(self.auth_host,
|
||||||
port=self.auth_port)
|
port=self.auth_port)
|
||||||
#connection.set_debuglevel(3)
|
#connection.set_debuglevel(3)
|
||||||
connection.request('GET', path, '', headers)
|
connection.request('GET', path, '', headers)
|
||||||
response = connection.getresponse()
|
response = connection.getresponse()
|
||||||
@ -156,7 +161,7 @@ class Connection(object):
|
|||||||
self.conn_class = httplib.HTTPSConnection
|
self.conn_class = httplib.HTTPSConnection
|
||||||
self.storage_port = 443
|
self.storage_port = 443
|
||||||
else:
|
else:
|
||||||
raise ValueError, 'unexpected protocol %s' % (x[0])
|
raise ValueError('unexpected protocol %s' % (x[0]))
|
||||||
|
|
||||||
self.storage_host = x[2].split(':')[0]
|
self.storage_host = x[2].split(':')[0]
|
||||||
if ':' in x[2]:
|
if ':' in x[2]:
|
||||||
@ -170,9 +175,9 @@ class Connection(object):
|
|||||||
|
|
||||||
def http_connect(self):
|
def http_connect(self):
|
||||||
self.connection = self.conn_class(self.storage_host,
|
self.connection = self.conn_class(self.storage_host,
|
||||||
port=self.storage_port)
|
port=self.storage_port)
|
||||||
#self.connection.set_debuglevel(3)
|
#self.connection.set_debuglevel(3)
|
||||||
|
|
||||||
def make_path(self, path=[], cfg={}):
|
def make_path(self, path=[], cfg={}):
|
||||||
if cfg.get('version_only_path'):
|
if cfg.get('version_only_path'):
|
||||||
return '/' + self.storage_url.split('/')[1]
|
return '/' + self.storage_url.split('/')[1]
|
||||||
@ -181,8 +186,8 @@ class Connection(object):
|
|||||||
quote = urllib.quote
|
quote = urllib.quote
|
||||||
if cfg.get('no_quote') or cfg.get('no_path_quote'):
|
if cfg.get('no_quote') or cfg.get('no_path_quote'):
|
||||||
quote = lambda x: x
|
quote = lambda x: x
|
||||||
return '%s/%s' % (self.storage_url, '/'.join([quote(i) for i
|
return '%s/%s' % (self.storage_url,
|
||||||
in path]))
|
'/'.join([quote(i) for i in path]))
|
||||||
else:
|
else:
|
||||||
return self.storage_url
|
return self.storage_url
|
||||||
|
|
||||||
@ -197,16 +202,15 @@ class Connection(object):
|
|||||||
return headers
|
return headers
|
||||||
|
|
||||||
def make_request(self, method, path=[], data='', hdrs={}, parms={},
|
def make_request(self, method, path=[], data='', hdrs={}, parms={},
|
||||||
cfg={}):
|
cfg={}):
|
||||||
|
|
||||||
path = self.make_path(path, cfg=cfg)
|
path = self.make_path(path, cfg=cfg)
|
||||||
headers = self.make_headers(hdrs, cfg=cfg)
|
headers = self.make_headers(hdrs, cfg=cfg)
|
||||||
if isinstance(parms, dict) and parms:
|
if isinstance(parms, dict) and parms:
|
||||||
quote = urllib.quote
|
quote = urllib.quote
|
||||||
if cfg.get('no_quote') or cfg.get('no_parms_quote'):
|
if cfg.get('no_quote') or cfg.get('no_parms_quote'):
|
||||||
quote = lambda x: x
|
quote = lambda x: x
|
||||||
query_args = ['%s=%s' % (quote(x), quote(str(y))) for (x,y) in
|
query_args = ['%s=%s' % (quote(x), quote(str(y)))
|
||||||
parms.items()]
|
for (x, y) in parms.items()]
|
||||||
path = '%s?%s' % (path, '&'.join(query_args))
|
path = '%s?%s' % (path, '&'.join(query_args))
|
||||||
if not cfg.get('no_content_length'):
|
if not cfg.get('no_content_length'):
|
||||||
if cfg.get('set_content_length'):
|
if cfg.get('set_content_length'):
|
||||||
@ -258,16 +262,16 @@ class Connection(object):
|
|||||||
quote = urllib.quote
|
quote = urllib.quote
|
||||||
if cfg.get('no_quote') or cfg.get('no_parms_quote'):
|
if cfg.get('no_quote') or cfg.get('no_parms_quote'):
|
||||||
quote = lambda x: x
|
quote = lambda x: x
|
||||||
query_args = ['%s=%s' % (quote(x), quote(str(y))) for (x,y) in
|
query_args = ['%s=%s' % (quote(x), quote(str(y)))
|
||||||
parms.items()]
|
for (x, y) in parms.items()]
|
||||||
path = '%s?%s' % (path, '&'.join(query_args))
|
path = '%s?%s' % (path, '&'.join(query_args))
|
||||||
|
|
||||||
query_args = ['%s=%s' % (urllib.quote(x),
|
query_args = ['%s=%s' % (urllib.quote(x),
|
||||||
urllib.quote(str(y))) for (x,y) in parms.items()]
|
urllib.quote(str(y))) for (x, y) in parms.items()]
|
||||||
path = '%s?%s' % (path, '&'.join(query_args))
|
path = '%s?%s' % (path, '&'.join(query_args))
|
||||||
|
|
||||||
self.connection = self.conn_class(self.storage_host,
|
self.connection = self.conn_class(self.storage_host,
|
||||||
port=self.storage_port)
|
port=self.storage_port)
|
||||||
#self.connection.set_debuglevel(3)
|
#self.connection.set_debuglevel(3)
|
||||||
self.connection.putrequest('PUT', path)
|
self.connection.putrequest('PUT', path)
|
||||||
for key, value in headers.iteritems():
|
for key, value in headers.iteritems():
|
||||||
@ -288,6 +292,7 @@ class Connection(object):
|
|||||||
self.connection.close()
|
self.connection.close()
|
||||||
return self.response.status
|
return self.response.status
|
||||||
|
|
||||||
|
|
||||||
class Base:
|
class Base:
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return self.name
|
return self.name
|
||||||
@ -296,9 +301,9 @@ class Base:
|
|||||||
headers = dict(self.conn.response.getheaders())
|
headers = dict(self.conn.response.getheaders())
|
||||||
ret = {}
|
ret = {}
|
||||||
for field in fields:
|
for field in fields:
|
||||||
if not headers.has_key(field[1]):
|
if not field[1] in headers:
|
||||||
raise ValueError("%s was not found in response header" %
|
raise ValueError("%s was not found in response header" %
|
||||||
(field[1]))
|
(field[1]))
|
||||||
|
|
||||||
try:
|
try:
|
||||||
ret[field[0]] = int(headers[field[1]])
|
ret[field[0]] = int(headers[field[1]])
|
||||||
@ -306,6 +311,7 @@ class Base:
|
|||||||
ret[field[0]] = headers[field[1]]
|
ret[field[0]] = headers[field[1]]
|
||||||
return ret
|
return ret
|
||||||
|
|
||||||
|
|
||||||
class Account(Base):
|
class Account(Base):
|
||||||
def __init__(self, conn, name):
|
def __init__(self, conn, name):
|
||||||
self.conn = conn
|
self.conn = conn
|
||||||
@ -318,11 +324,11 @@ class Account(Base):
|
|||||||
format = parms.get('format', None)
|
format = parms.get('format', None)
|
||||||
if format not in [None, 'json', 'xml']:
|
if format not in [None, 'json', 'xml']:
|
||||||
raise RequestError('Invalid format: %s' % format)
|
raise RequestError('Invalid format: %s' % format)
|
||||||
if format is None and parms.has_key('format'):
|
if format is None and 'format' in parms:
|
||||||
del parms['format']
|
del parms['format']
|
||||||
|
|
||||||
status = self.conn.make_request('GET', self.path, hdrs=hdrs,
|
status = self.conn.make_request('GET', self.path, hdrs=hdrs,
|
||||||
parms=parms, cfg=cfg)
|
parms=parms, cfg=cfg)
|
||||||
if status == 200:
|
if status == 200:
|
||||||
if format == 'json':
|
if format == 'json':
|
||||||
conts = json.loads(self.conn.response.read())
|
conts = json.loads(self.conn.response.read())
|
||||||
@ -361,13 +367,13 @@ class Account(Base):
|
|||||||
|
|
||||||
def info(self, hdrs={}, parms={}, cfg={}):
|
def info(self, hdrs={}, parms={}, cfg={}):
|
||||||
if self.conn.make_request('HEAD', self.path, hdrs=hdrs,
|
if self.conn.make_request('HEAD', self.path, hdrs=hdrs,
|
||||||
parms=parms, cfg=cfg) != 204:
|
parms=parms, cfg=cfg) != 204:
|
||||||
|
|
||||||
raise ResponseError(self.conn.response)
|
raise ResponseError(self.conn.response)
|
||||||
|
|
||||||
fields = [['object_count', 'x-account-object-count'],
|
fields = [['object_count', 'x-account-object-count'],
|
||||||
['container_count', 'x-account-container-count'],
|
['container_count', 'x-account-container-count'],
|
||||||
['bytes_used', 'x-account-bytes-used']]
|
['bytes_used', 'x-account-bytes-used']]
|
||||||
|
|
||||||
return self.header_fields(fields)
|
return self.header_fields(fields)
|
||||||
|
|
||||||
@ -375,6 +381,7 @@ class Account(Base):
|
|||||||
def path(self):
|
def path(self):
|
||||||
return []
|
return []
|
||||||
|
|
||||||
|
|
||||||
class Container(Base):
|
class Container(Base):
|
||||||
def __init__(self, conn, account, name):
|
def __init__(self, conn, account, name):
|
||||||
self.conn = conn
|
self.conn = conn
|
||||||
@ -383,11 +390,11 @@ class Container(Base):
|
|||||||
|
|
||||||
def create(self, hdrs={}, parms={}, cfg={}):
|
def create(self, hdrs={}, parms={}, cfg={}):
|
||||||
return self.conn.make_request('PUT', self.path, hdrs=hdrs,
|
return self.conn.make_request('PUT', self.path, hdrs=hdrs,
|
||||||
parms=parms, cfg=cfg) in (201, 202)
|
parms=parms, cfg=cfg) in (201, 202)
|
||||||
|
|
||||||
def delete(self, hdrs={}, parms={}):
|
def delete(self, hdrs={}, parms={}):
|
||||||
return self.conn.make_request('DELETE', self.path, hdrs=hdrs,
|
return self.conn.make_request('DELETE', self.path, hdrs=hdrs,
|
||||||
parms=parms) == 204
|
parms=parms) == 204
|
||||||
|
|
||||||
def delete_files(self):
|
def delete_files(self):
|
||||||
for f in listing_items(self.files):
|
for f in listing_items(self.files):
|
||||||
@ -407,11 +414,11 @@ class Container(Base):
|
|||||||
format = parms.get('format', None)
|
format = parms.get('format', None)
|
||||||
if format not in [None, 'json', 'xml']:
|
if format not in [None, 'json', 'xml']:
|
||||||
raise RequestError('Invalid format: %s' % format)
|
raise RequestError('Invalid format: %s' % format)
|
||||||
if format is None and parms.has_key('format'):
|
if format is None and 'format' in parms:
|
||||||
del parms['format']
|
del parms['format']
|
||||||
|
|
||||||
status = self.conn.make_request('GET', self.path, hdrs=hdrs,
|
status = self.conn.make_request('GET', self.path, hdrs=hdrs,
|
||||||
parms=parms, cfg=cfg)
|
parms=parms, cfg=cfg)
|
||||||
if status == 200:
|
if status == 200:
|
||||||
if format == 'json':
|
if format == 'json':
|
||||||
files = json.loads(self.conn.response.read())
|
files = json.loads(self.conn.response.read())
|
||||||
@ -426,7 +433,7 @@ class Container(Base):
|
|||||||
for x in tree.getElementsByTagName('object'):
|
for x in tree.getElementsByTagName('object'):
|
||||||
file = {}
|
file = {}
|
||||||
for key in ['name', 'hash', 'bytes', 'content_type',
|
for key in ['name', 'hash', 'bytes', 'content_type',
|
||||||
'last_modified']:
|
'last_modified']:
|
||||||
|
|
||||||
file[key] = x.getElementsByTagName(key)[0].\
|
file[key] = x.getElementsByTagName(key)[0].\
|
||||||
childNodes[0].nodeValue
|
childNodes[0].nodeValue
|
||||||
@ -452,11 +459,11 @@ class Container(Base):
|
|||||||
|
|
||||||
def info(self, hdrs={}, parms={}, cfg={}):
|
def info(self, hdrs={}, parms={}, cfg={}):
|
||||||
status = self.conn.make_request('HEAD', self.path, hdrs=hdrs,
|
status = self.conn.make_request('HEAD', self.path, hdrs=hdrs,
|
||||||
parms=parms, cfg=cfg)
|
parms=parms, cfg=cfg)
|
||||||
|
|
||||||
if self.conn.response.status == 204:
|
if self.conn.response.status == 204:
|
||||||
fields = [['bytes_used', 'x-container-bytes-used'],
|
fields = [['bytes_used', 'x-container-bytes-used'],
|
||||||
['object_count', 'x-container-object-count']]
|
['object_count', 'x-container-object-count']]
|
||||||
|
|
||||||
return self.header_fields(fields)
|
return self.header_fields(fields)
|
||||||
|
|
||||||
@ -466,6 +473,7 @@ class Container(Base):
|
|||||||
def path(self):
|
def path(self):
|
||||||
return [self.name]
|
return [self.name]
|
||||||
|
|
||||||
|
|
||||||
class File(Base):
|
class File(Base):
|
||||||
def __init__(self, conn, account, container, name):
|
def __init__(self, conn, account, container, name):
|
||||||
self.conn = conn
|
self.conn = conn
|
||||||
@ -496,7 +504,7 @@ class File(Base):
|
|||||||
headers['Content-Type'] = 'application/octet-stream'
|
headers['Content-Type'] = 'application/octet-stream'
|
||||||
|
|
||||||
for key in self.metadata:
|
for key in self.metadata:
|
||||||
headers['X-Object-Meta-'+key] = self.metadata[key]
|
headers['X-Object-Meta-' + key] = self.metadata[key]
|
||||||
|
|
||||||
return headers
|
return headers
|
||||||
|
|
||||||
@ -516,7 +524,7 @@ class File(Base):
|
|||||||
return checksum.hexdigest()
|
return checksum.hexdigest()
|
||||||
|
|
||||||
def copy(self, dest_cont, dest_file, hdrs={}, parms={}, cfg={}):
|
def copy(self, dest_cont, dest_file, hdrs={}, parms={}, cfg={}):
|
||||||
if cfg.has_key('destination'):
|
if 'destination' in cfg:
|
||||||
headers = {'Destination': cfg['destination']}
|
headers = {'Destination': cfg['destination']}
|
||||||
elif cfg.get('no_destination'):
|
elif cfg.get('no_destination'):
|
||||||
headers = {}
|
headers = {}
|
||||||
@ -524,15 +532,15 @@ class File(Base):
|
|||||||
headers = {'Destination': '%s/%s' % (dest_cont, dest_file)}
|
headers = {'Destination': '%s/%s' % (dest_cont, dest_file)}
|
||||||
headers.update(hdrs)
|
headers.update(hdrs)
|
||||||
|
|
||||||
if headers.has_key('Destination'):
|
if 'Destination' in headers:
|
||||||
headers['Destination'] = urllib.quote(headers['Destination'])
|
headers['Destination'] = urllib.quote(headers['Destination'])
|
||||||
|
|
||||||
return self.conn.make_request('COPY', self.path, hdrs=headers,
|
return self.conn.make_request('COPY', self.path, hdrs=headers,
|
||||||
parms=parms) == 201
|
parms=parms) == 201
|
||||||
|
|
||||||
def delete(self, hdrs={}, parms={}):
|
def delete(self, hdrs={}, parms={}):
|
||||||
if self.conn.make_request('DELETE', self.path, hdrs=hdrs,
|
if self.conn.make_request('DELETE', self.path, hdrs=hdrs,
|
||||||
parms=parms) != 204:
|
parms=parms) != 204:
|
||||||
|
|
||||||
raise ResponseError(self.conn.response)
|
raise ResponseError(self.conn.response)
|
||||||
|
|
||||||
@ -540,13 +548,14 @@ class File(Base):
|
|||||||
|
|
||||||
def info(self, hdrs={}, parms={}, cfg={}):
|
def info(self, hdrs={}, parms={}, cfg={}):
|
||||||
if self.conn.make_request('HEAD', self.path, hdrs=hdrs,
|
if self.conn.make_request('HEAD', self.path, hdrs=hdrs,
|
||||||
parms=parms, cfg=cfg) != 200:
|
parms=parms, cfg=cfg) != 200:
|
||||||
|
|
||||||
raise ResponseError(self.conn.response)
|
raise ResponseError(self.conn.response)
|
||||||
|
|
||||||
fields = [['content_length', 'content-length'], ['content_type',
|
fields = [['content_length', 'content-length'],
|
||||||
'content-type'], ['last_modified', 'last-modified'], ['etag',
|
['content_type', 'content-type'],
|
||||||
'etag']]
|
['last_modified', 'last-modified'],
|
||||||
|
['etag', 'etag']]
|
||||||
|
|
||||||
header_fields = self.header_fields(fields)
|
header_fields = self.header_fields(fields)
|
||||||
header_fields['etag'] = header_fields['etag'].strip('"')
|
header_fields['etag'] = header_fields['etag'].strip('"')
|
||||||
@ -557,7 +566,7 @@ class File(Base):
|
|||||||
return False
|
return False
|
||||||
|
|
||||||
status = self.conn.make_request('HEAD', self.path, hdrs=hdrs,
|
status = self.conn.make_request('HEAD', self.path, hdrs=hdrs,
|
||||||
parms=parms)
|
parms=parms)
|
||||||
if status == 404:
|
if status == 404:
|
||||||
return False
|
return False
|
||||||
elif (status < 200) or (status > 299):
|
elif (status < 200) or (status > 299):
|
||||||
@ -596,7 +605,7 @@ class File(Base):
|
|||||||
return data
|
return data
|
||||||
|
|
||||||
def read(self, size=-1, offset=0, hdrs=None, buffer=None,
|
def read(self, size=-1, offset=0, hdrs=None, buffer=None,
|
||||||
callback=None, cfg={}):
|
callback=None, cfg={}):
|
||||||
|
|
||||||
if size > 0:
|
if size > 0:
|
||||||
range = 'bytes=%d-%d' % (offset, (offset + size) - 1)
|
range = 'bytes=%d-%d' % (offset, (offset + size) - 1)
|
||||||
@ -606,7 +615,7 @@ class File(Base):
|
|||||||
hdrs = {'Range': range}
|
hdrs = {'Range': range}
|
||||||
|
|
||||||
status = self.conn.make_request('GET', self.path, hdrs=hdrs,
|
status = self.conn.make_request('GET', self.path, hdrs=hdrs,
|
||||||
cfg=cfg)
|
cfg=cfg)
|
||||||
|
|
||||||
if(status < 200) or (status > 299):
|
if(status < 200) or (status > 299):
|
||||||
raise ResponseError(self.conn.response)
|
raise ResponseError(self.conn.response)
|
||||||
@ -680,7 +689,7 @@ class File(Base):
|
|||||||
headers.update(hdrs)
|
headers.update(hdrs)
|
||||||
|
|
||||||
self.conn.put_start(self.path, hdrs=headers, parms=parms,
|
self.conn.put_start(self.path, hdrs=headers, parms=parms,
|
||||||
cfg=cfg, chunked=True)
|
cfg=cfg, chunked=True)
|
||||||
|
|
||||||
self.conn.put_data(data, True)
|
self.conn.put_data(data, True)
|
||||||
elif self.chunked_write_in_progress:
|
elif self.chunked_write_in_progress:
|
||||||
@ -690,7 +699,7 @@ class File(Base):
|
|||||||
raise RuntimeError
|
raise RuntimeError
|
||||||
|
|
||||||
def write(self, data='', hdrs={}, parms={}, callback=None, cfg={}):
|
def write(self, data='', hdrs={}, parms={}, callback=None, cfg={}):
|
||||||
block_size = 2**20
|
block_size = 2 ** 20
|
||||||
|
|
||||||
if isinstance(data, file):
|
if isinstance(data, file):
|
||||||
try:
|
try:
|
||||||
@ -723,8 +732,7 @@ class File(Base):
|
|||||||
raise err
|
raise err
|
||||||
|
|
||||||
if (self.conn.response.status < 200) or \
|
if (self.conn.response.status < 200) or \
|
||||||
(self.conn.response.status > 299):
|
(self.conn.response.status > 299):
|
||||||
|
|
||||||
raise ResponseError(self.conn.response)
|
raise ResponseError(self.conn.response)
|
||||||
|
|
||||||
self.md5 = self.compute_md5sum(data)
|
self.md5 = self.compute_md5sum(data)
|
||||||
|
Loading…
Reference in New Issue
Block a user