Readded changes to bin/swift after merge from trunk

This commit is contained in:
gholt 2011-06-14 23:14:28 +00:00
parent ba4f74d4fe
commit 599aedae95

View File

@ -150,7 +150,7 @@ class ClientException(Exception):
return b and '%s: %s' % (a, b) or a return b and '%s: %s' % (a, b) or a
def http_connection(url): def http_connection(url, proxy=None):
""" """
Make an HTTPConnection or HTTPSConnection Make an HTTPConnection or HTTPSConnection
@ -158,14 +158,16 @@ def http_connection(url):
:returns: tuple of (parsed url, connection object) :returns: tuple of (parsed url, connection object)
:raises ClientException: Unable to handle protocol scheme :raises ClientException: Unable to handle protocol scheme
""" """
parsed = urlparse(url) parsed = urlparse(proxy or url)
if parsed.scheme == 'http': if parsed.scheme == 'http':
conn = HTTPConnection(parsed.netloc) conn = HTTPConnection(parsed.netloc)
elif parsed.scheme == 'https': elif parsed.scheme == 'https':
conn = HTTPSConnection(parsed.netloc) conn = HTTPSConnection(parsed.netloc)
else: else:
raise ClientException('Cannot handle protocol scheme %s for url %s' % raise ClientException('Cannot handle protocol scheme %s for url %s' %
(parsed.scheme, repr(url))) (parsed.scheme, repr(proxy or url)))
if proxy:
parsed = urlparse(url)
return parsed, conn return parsed, conn
@ -578,9 +580,9 @@ def head_object(url, token, container, name, http_conn=None):
return resp_headers return resp_headers
def put_object(url, token, container, name, contents, content_length=None, def put_object(url, token=None, container=None, name=None, contents=None,
etag=None, chunk_size=65536, content_type=None, headers=None, content_length=None, etag=None, chunk_size=65536,
http_conn=None): content_type=None, headers=None, http_conn=None, proxy=None):
""" """
Put an object Put an object
@ -603,15 +605,28 @@ def put_object(url, token, container, name, contents, content_length=None,
if http_conn: if http_conn:
parsed, conn = http_conn parsed, conn = http_conn
else: else:
parsed, conn = http_connection(url) parsed, conn = http_connection(url, proxy=proxy)
path = '%s/%s/%s' % (parsed.path, quote(container), quote(name)) path = parsed.path
if not headers: if container:
path = '%s/%s' % (path.rstrip('/'), quote(container))
if name:
path = '%s/%s' % (path.rstrip('/'), quote(name))
if proxy:
path = parsed.scheme + '://' + parsed.netloc + path
if headers:
headers = dict(headers)
else:
headers = {} headers = {}
headers['X-Auth-Token'] = token if token:
headers['X-Auth-Token'] = token
if etag: if etag:
headers['ETag'] = etag.strip('"') headers['ETag'] = etag.strip('"')
if content_length is not None: if content_length is not None:
headers['Content-Length'] = str(content_length) headers['Content-Length'] = str(content_length)
else:
for n, v in headers.iteritems():
if n.lower() == 'content-length':
content_length = int(v)
if content_type is not None: if content_type is not None:
headers['Content-Type'] = content_type headers['Content-Type'] = content_type
if not contents: if not contents:
@ -646,7 +661,7 @@ def put_object(url, token, container, name, contents, content_length=None,
raise ClientException('Object PUT failed', http_scheme=parsed.scheme, raise ClientException('Object PUT failed', http_scheme=parsed.scheme,
http_host=conn.host, http_port=conn.port, http_path=path, http_host=conn.host, http_port=conn.port, http_path=path,
http_status=resp.status, http_reason=resp.reason) http_status=resp.status, http_reason=resp.reason)
return resp.getheader('etag').strip('"') return resp.getheader('etag', '').strip('"')
def post_object(url, token, container, name, headers, http_conn=None): def post_object(url, token, container, name, headers, http_conn=None):
@ -677,7 +692,8 @@ def post_object(url, token, container, name, headers, http_conn=None):
http_status=resp.status, http_reason=resp.reason) http_status=resp.status, http_reason=resp.reason)
def delete_object(url, token, container, name, http_conn=None): def delete_object(url, token=None, container=None, name=None, http_conn=None,
headers=None, proxy=None):
""" """
Delete object Delete object
@ -692,9 +708,21 @@ def delete_object(url, token, container, name, http_conn=None):
if http_conn: if http_conn:
parsed, conn = http_conn parsed, conn = http_conn
else: else:
parsed, conn = http_connection(url) parsed, conn = http_connection(url, proxy=proxy)
path = '%s/%s/%s' % (parsed.path, quote(container), quote(name)) path = parsed.path
conn.request('DELETE', path, '', {'X-Auth-Token': token}) if container:
path = '%s/%s' % (path.rstrip('/'), quote(container))
if name:
path = '%s/%s' % (path.rstrip('/'), quote(name))
if headers:
headers = dict(headers)
else:
headers = {}
if token:
headers['X-Auth-Token'] = token
if proxy:
path = parsed.scheme + '://' + parsed.netloc + path
conn.request('DELETE', path, '', headers)
resp = conn.getresponse() resp = conn.getresponse()
resp.read() resp.read()
if resp.status < 200 or resp.status >= 300: if resp.status < 200 or resp.status >= 300:
@ -1363,10 +1391,14 @@ Container: %s
Objects: %d Objects: %d
Bytes: %d Bytes: %d
Read ACL: %s Read ACL: %s
Write ACL: %s'''.strip('\n') % (conn.url.rsplit('/', 1)[-1], args[0], Write ACL: %s
Sync To: %s
Sync Key: %s'''.strip('\n') % (conn.url.rsplit('/', 1)[-1], args[0],
object_count, bytes_used, object_count, bytes_used,
headers.get('x-container-read', ''), headers.get('x-container-read', ''),
headers.get('x-container-write', ''))) headers.get('x-container-write', ''),
headers.get('x-container-sync-to', ''),
headers.get('x-container-sync-key', '')))
for key, value in headers.items(): for key, value in headers.items():
if key.startswith('x-container-meta-'): if key.startswith('x-container-meta-'):
print_queue.put('%9s: %s' % ('Meta %s' % print_queue.put('%9s: %s' % ('Meta %s' %
@ -1375,7 +1407,8 @@ Write ACL: %s'''.strip('\n') % (conn.url.rsplit('/', 1)[-1], args[0],
if not key.startswith('x-container-meta-') and key not in ( if not key.startswith('x-container-meta-') and key not in (
'content-length', 'date', 'x-container-object-count', 'content-length', 'date', 'x-container-object-count',
'x-container-bytes-used', 'x-container-read', 'x-container-bytes-used', 'x-container-read',
'x-container-write'): 'x-container-write', 'x-container-sync-to',
'x-container-sync-key'):
print_queue.put( print_queue.put(
'%9s: %s' % (key.title(), value)) '%9s: %s' % (key.title(), value))
except ClientException, err: except ClientException, err:
@ -1440,13 +1473,18 @@ def st_post(options, args, print_queue, error_queue):
parser.add_option('-w', '--write-acl', dest='write_acl', help='Sets the ' parser.add_option('-w', '--write-acl', dest='write_acl', help='Sets the '
'Write ACL for containers. Quick summary of ACL syntax: account1, ' 'Write ACL for containers. Quick summary of ACL syntax: account1, '
'account2:user2') 'account2:user2')
parser.add_option('-t', '--sync-to', dest='sync_to', help='Sets the '
'Sync To for containers, for multi-cluster replication.')
parser.add_option('-k', '--sync-key', dest='sync_key', help='Sets the '
'Sync Key for containers, for multi-cluster replication.')
parser.add_option('-m', '--meta', action='append', dest='meta', default=[], parser.add_option('-m', '--meta', action='append', dest='meta', default=[],
help='Sets a meta data item with the syntax name:value. This option ' help='Sets a meta data item with the syntax name:value. This option '
'may be repeated. Example: -m Color:Blue -m Size:Large') 'may be repeated. Example: -m Color:Blue -m Size:Large')
(options, args) = parse_args(parser, args) (options, args) = parse_args(parser, args)
args = args[1:] args = args[1:]
if (options.read_acl or options.write_acl) and not args: if (options.read_acl or options.write_acl or options.sync_to or
exit('-r and -w options only allowed for containers') options.sync_key) and not args:
exit('-r, -w, -t, and -k options only allowed for containers')
conn = Connection(options.auth, options.user, options.key) conn = Connection(options.auth, options.user, options.key)
if not args: if not args:
headers = {} headers = {}
@ -1474,6 +1512,10 @@ def st_post(options, args, print_queue, error_queue):
headers['X-Container-Read'] = options.read_acl headers['X-Container-Read'] = options.read_acl
if options.write_acl is not None: if options.write_acl is not None:
headers['X-Container-Write'] = options.write_acl headers['X-Container-Write'] = options.write_acl
if options.sync_to is not None:
headers['X-Container-Sync-To'] = options.sync_to
if options.sync_key is not None:
headers['X-Container-Sync-Key'] = options.sync_key
try: try:
conn.post_container(args[0], headers=headers) conn.post_container(args[0], headers=headers)
except ClientException, err: except ClientException, err: