cherry pick
This commit is contained in:
parent
bb4927470a
commit
eb047d1050
3 changed files with 243 additions and 47 deletions
|
@ -6,11 +6,13 @@ from uuid import uuid4
|
|||
|
||||
from storage.basestorage import BaseStorageV2
|
||||
|
||||
_FAKE_STORAGE_MAP = defaultdict(StringIO.StringIO)
|
||||
_GLOBAL_FAKE_STORAGE_MAP = defaultdict(StringIO.StringIO)
|
||||
|
||||
class FakeStorage(BaseStorageV2):
|
||||
def __init__(self, context):
|
||||
super(FakeStorage, self).__init__()
|
||||
self._fake_storage_map = (defaultdict(StringIO.StringIO)
|
||||
if context == 'local' else _GLOBAL_FAKE_STORAGE_MAP)
|
||||
|
||||
def _init_path(self, path=None, create=False):
|
||||
return path
|
||||
|
@ -25,18 +27,18 @@ class FakeStorage(BaseStorageV2):
|
|||
return None
|
||||
|
||||
def get_content(self, path):
|
||||
if not path in _FAKE_STORAGE_MAP:
|
||||
raise IOError('Fake file %s not found' % path)
|
||||
if not path in self._fake_storage_map:
|
||||
raise IOError('Fake file %s not found. Exist: %s' % (path, self._fake_storage_map.keys()))
|
||||
|
||||
_FAKE_STORAGE_MAP.get(path).seek(0)
|
||||
return _FAKE_STORAGE_MAP.get(path).read()
|
||||
self._fake_storage_map.get(path).seek(0)
|
||||
return self._fake_storage_map.get(path).read()
|
||||
|
||||
def put_content(self, path, content):
|
||||
_FAKE_STORAGE_MAP.pop(path, None)
|
||||
_FAKE_STORAGE_MAP[path].write(content)
|
||||
self._fake_storage_map.pop(path, None)
|
||||
self._fake_storage_map[path].write(content)
|
||||
|
||||
def stream_read(self, path):
|
||||
io_obj = _FAKE_STORAGE_MAP[path]
|
||||
io_obj = self._fake_storage_map[path]
|
||||
io_obj.seek(0)
|
||||
while True:
|
||||
buf = io_obj.read(self.buffer_size)
|
||||
|
@ -48,41 +50,51 @@ class FakeStorage(BaseStorageV2):
|
|||
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 = self._fake_storage_map[path]
|
||||
out_fp.seek(0)
|
||||
self.stream_write_to_fp(fp, out_fp)
|
||||
|
||||
def remove(self, path):
|
||||
_FAKE_STORAGE_MAP.pop(path, None)
|
||||
self._fake_storage_map.pop(path, None)
|
||||
|
||||
def exists(self, path):
|
||||
if _FAKE_STORAGE_MAP.get('all_files_exist', None):
|
||||
if self._fake_storage_map.get('all_files_exist', None):
|
||||
return True
|
||||
return path in _FAKE_STORAGE_MAP
|
||||
return path in self._fake_storage_map
|
||||
|
||||
def get_checksum(self, path):
|
||||
return hashlib.sha256(_FAKE_STORAGE_MAP[path].read()).hexdigest()[:7]
|
||||
return hashlib.sha256(self._fake_storage_map[path].read()).hexdigest()[:7]
|
||||
|
||||
def initiate_chunked_upload(self):
|
||||
new_uuid = str(uuid4())
|
||||
_FAKE_STORAGE_MAP[new_uuid].seek(0)
|
||||
self._fake_storage_map[new_uuid].seek(0)
|
||||
return new_uuid, {}
|
||||
|
||||
def stream_upload_chunk(self, uuid, offset, length, in_fp, _, content_type=None):
|
||||
upload_storage = _FAKE_STORAGE_MAP[uuid]
|
||||
upload_storage = self._fake_storage_map[uuid]
|
||||
upload_storage.seek(offset)
|
||||
|
||||
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)
|
||||
self._fake_storage_map[final_path] = self._fake_storage_map[uuid]
|
||||
self._fake_storage_map.pop(uuid, None)
|
||||
|
||||
def cancel_chunked_upload(self, uuid, _):
|
||||
_FAKE_STORAGE_MAP.pop(uuid, None)
|
||||
self._fake_storage_map.pop(uuid, None)
|
||||
|
||||
def copy_to(self, destination, path):
|
||||
if self.exists('break_copying'):
|
||||
raise IOError('Broken!')
|
||||
|
||||
if self.exists('fake_copying'):
|
||||
return
|
||||
|
||||
if self.exists('except_copying'):
|
||||
raise Exception("I'm an exception!")
|
||||
|
||||
content = self.get_content(path)
|
||||
destination.put_content(path, content)
|
||||
|
|
Reference in a new issue