/usr/lib/python2.7/dist-packages/simplestreams/objectstores/swift.py is in python-simplestreams 0.1.0~bzr341-0ubuntu2.3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 | # Copyright (C) 2013 Canonical Ltd.
#
# Author: Scott Moser <scott.moser@canonical.com>
#
# Simplestreams is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at your
# option) any later version.
#
# Simplestreams is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
# License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with Simplestreams. If not, see <http://www.gnu.org/licenses/>.
import simplestreams.objectstores as objectstores
import simplestreams.contentsource as cs
import simplestreams.openstack as openstack
import errno
import hashlib
from swiftclient import Connection, ClientException
def get_swiftclient(**kwargs):
# nmap has entries that need name changes from a 'get_service_conn_info'
# to a swift Connection name.
# pt has names that pass straight through
nmap = {'endpoint': 'preauthurl', 'token': 'preauthtoken'}
pt = ('insecure', 'cacert')
connargs = {v: kwargs.get(k) for k, v in nmap.items() if k in kwargs}
connargs.update({k: kwargs.get(k) for k in pt if k in kwargs})
return Connection(**connargs)
class SwiftContentSource(cs.IteratorContentSource):
def is_enoent(self, exc):
return is_enoent(exc)
class SwiftObjectStore(objectstores.ObjectStore):
def __init__(self, prefix, region=None):
# expect 'swift://bucket/path_prefix'
self.prefix = prefix
if prefix.startswith("swift://"):
path = prefix[8:]
else:
path = prefix
(self.container, self.path_prefix) = path.split("/", 1)
super(SwiftObjectStore, self).__init__()
self.keystone_creds = openstack.load_keystone_creds()
if region is not None:
self.keystone_creds['region_name'] = region
conn_info = openstack.get_service_conn_info('object-store',
**self.keystone_creds)
self.swiftclient = get_swiftclient(**conn_info)
# http://docs.openstack.org/developer/swift/misc.html#acls
self.swiftclient.put_container(self.container,
headers={'X-Container-Read':
'.r:*,.rlistings'})
def insert(self, path, reader, checksums=None, mutable=True, size=None):
#store content from reader.read() into path, expecting result checksum
self._insert(path=path, contents=reader, checksums=checksums,
mutable=mutable)
def insert_content(self, path, content, checksums=None, mutable=True):
self._insert(path=path, contents=content, checksums=checksums,
mutable=mutable)
def remove(self, path):
self.swiftclient.delete_object(container=self.container,
obj=self.path_prefix + path)
def source(self, path):
def itgen():
(_headers, iterator) = self.swiftclient.get_object(
container=self.container, obj=self.path_prefix + path,
resp_chunk_size=self.read_size)
return iterator
return SwiftContentSource(itgen=itgen, url=self.prefix + path)
def exists_with_checksum(self, path, checksums=None):
return headers_match_checksums(self._head_path(path), checksums)
def _head_path(self, path):
try:
headers = self.swiftclient.head_object(container=self.container,
obj=self.path_prefix + path)
except Exception as exc:
if is_enoent(exc):
return {}
raise
return headers
def _insert(self, path, contents, checksums=None, mutable=True, size=None):
# content is a ContentSource or a string
headers = self._head_path(path)
if headers:
if not mutable:
return
if headers_match_checksums(headers, checksums):
return
insargs = {'container': self.container, 'obj': self.path_prefix + path,
'contents': contents}
if size is not None and isinstance(contents, str):
size = len(contents)
if size is not None:
insargs['content_length'] = size
if checksums and checksums.get('md5'):
insargs['etag'] = checksums.get('md5')
elif isinstance(contents, str):
insargs['etag'] = hashlib.md5(contents).hexdigest()
self.swiftclient.put_object(**insargs)
def headers_match_checksums(headers, checksums):
if not (headers and checksums):
return False
if ('md5' in checksums and headers.get('etag') == checksums.get('md5')):
return True
return False
def is_enoent(exc):
return ((isinstance(exc, IOError) and exc.errno == errno.ENOENT) or
(isinstance(exc, ClientException) and exc.http_status == 404))
# vi: ts=4 expandtab
|