This repository has been archived on 2020-03-24. You can view files and clone it, but cannot push or open issues or pull requests.
quay/storage/fakestorage.py

88 lines
2.5 KiB
Python
Raw Normal View History

import cStringIO as StringIO
import hashlib
from collections import defaultdict
from uuid import uuid4
from storage.basestorage import BaseStorageV2
_FAKE_STORAGE_MAP = defaultdict(StringIO.StringIO)
class FakeStorage(BaseStorageV2):
def __init__(self, context):
2016-01-15 16:15:40 +00:00
super(FakeStorage, self).__init__()
def _init_path(self, path=None, create=False):
return path
def get_direct_download_url(self, path, request_ip=None, expires_in=60, requires_cors=False, head=False):
2016-01-19 20:52:34 +00:00
try:
if self.get_content('supports_direct_download') == 'true':
return 'http://somefakeurl?goes=here'
2016-01-19 20:52:34 +00:00
except:
pass
return None
def get_content(self, path):
2015-05-29 22:08:17 +00:00
if not path in _FAKE_STORAGE_MAP:
raise IOError('Fake file %s not found. Exist: %s' % (path, _FAKE_STORAGE_MAP.keys()))
2015-05-29 22:08:17 +00:00
_FAKE_STORAGE_MAP.get(path).seek(0)
return _FAKE_STORAGE_MAP.get(path).read()
def put_content(self, path, content):
_FAKE_STORAGE_MAP.pop(path, None)
_FAKE_STORAGE_MAP[path].write(content)
def stream_read(self, path):
io_obj = _FAKE_STORAGE_MAP[path]
io_obj.seek(0)
while True:
buf = io_obj.read(self.buffer_size)
if not buf:
break
yield buf
def stream_read_file(self, path):
return StringIO.StringIO(self.get_content(path))
def stream_write(self, path, fp, content_type=None, content_encoding=None):
out_fp = _FAKE_STORAGE_MAP[path]
out_fp.seek(0)
self.stream_write_to_fp(fp, out_fp)
def remove(self, path):
2015-05-29 22:08:17 +00:00
_FAKE_STORAGE_MAP.pop(path, None)
def exists(self, path):
2016-05-05 17:55:24 +00:00
if _FAKE_STORAGE_MAP.get('all_files_exist', None):
return True
2015-05-29 22:08:17 +00:00
return path in _FAKE_STORAGE_MAP
def get_checksum(self, path):
return hashlib.sha256(_FAKE_STORAGE_MAP[path].read()).hexdigest()[:7]
def initiate_chunked_upload(self):
new_uuid = str(uuid4())
_FAKE_STORAGE_MAP[new_uuid].seek(0)
return new_uuid, {}
2015-11-30 20:45:45 +00:00
def stream_upload_chunk(self, uuid, offset, length, in_fp, _, content_type=None):
upload_storage = _FAKE_STORAGE_MAP[uuid]
2015-12-10 04:16:33 +00:00
try:
return self.stream_write_to_fp(in_fp, upload_storage, length), {}, None
except IOError as ex:
return 0, {}, ex
def complete_chunked_upload(self, uuid, final_path, _):
_FAKE_STORAGE_MAP[final_path] = _FAKE_STORAGE_MAP[uuid]
_FAKE_STORAGE_MAP.pop(uuid, None)
def cancel_chunked_upload(self, uuid, _):
_FAKE_STORAGE_MAP.pop(uuid, None)
def copy_to(self, destination, path):
content = self.get_content(path)
destination.put_content(path, content)