diff options
author | Aleksey Lim <alsroot@sugarlabs.org> | 2014-01-05 09:47:44 (GMT) |
---|---|---|
committer | Aleksey Lim <alsroot@sugarlabs.org> | 2014-01-05 09:48:02 (GMT) |
commit | 47f1ea9d473075774f6825c546566b2ac48ad25b (patch) | |
tree | 48128c1fac03f90f67798eb2358e0595368c86c4 /sugar_network | |
parent | 28f17ea51f50fd75025bc598cbca1351aad4be3f (diff) |
Return blobs metadata in GET prop requests
Diffstat (limited to 'sugar_network')
-rw-r--r-- | sugar_network/client/cache.py | 2 | ||||
-rw-r--r-- | sugar_network/client/implementations.py | 26 | ||||
-rw-r--r-- | sugar_network/client/journal.py | 11 | ||||
-rw-r--r-- | sugar_network/db/directory.py | 2 | ||||
-rw-r--r-- | sugar_network/db/routes.py | 15 | ||||
-rw-r--r-- | sugar_network/toolkit/__init__.py | 9 |
6 files changed, 38 insertions, 27 deletions
diff --git a/sugar_network/client/cache.py b/sugar_network/client/cache.py index 554dde4..07daa16 100644 --- a/sugar_network/client/cache.py +++ b/sugar_network/client/cache.py @@ -138,7 +138,7 @@ class Cache(object): stat = os.statvfs(client.local_root.value) if stat.f_blocks == 0: - # TODO Sonds like a tmpfs or so + # TODO Sounds like a tmpfs or so return 0 limit = sys.maxint diff --git a/sugar_network/client/implementations.py b/sugar_network/client/implementations.py index f78df33..ca9e89c 100644 --- a/sugar_network/client/implementations.py +++ b/sugar_network/client/implementations.py @@ -24,6 +24,7 @@ import random import shutil import hashlib import logging +from copy import deepcopy from os.path import join, exists, basename, dirname, relpath from sugar_network import client, toolkit @@ -227,7 +228,7 @@ class Routes(object): def cache_impl(sel): guid = sel['guid'] data = sel['data'] - data_path = sel['path'] = impls.path(guid, 'data') + sel['path'] = impls.path(guid, 'data') size = data.get('unpack_size') or data['blob_size'] blob = None @@ -240,33 +241,36 @@ class Routes(object): if blob is None: blob = self._call(method='GET', path=['implementation', guid, 'data']) - try: - if not exists(dirname(data_path)): - os.makedirs(dirname(data_path)) + + blob_dir = dirname(sel['path']) + if not exists(blob_dir): + os.makedirs(blob_dir) + + with toolkit.mkdtemp(dir=blob_dir) as blob_dir: if 'activity' in context['type']: self._cache.ensure(size, data['blob_size']) with toolkit.TemporaryFile() as tmp_file: shutil.copyfileobj(blob, tmp_file) tmp_file.seek(0) with Bundle(tmp_file, 'application/zip') as bundle: - bundle.extractall(data_path, prefix=bundle.rootdir) + bundle.extractall(blob_dir, prefix=bundle.rootdir) for exec_dir in ('bin', 'activity'): - bin_path = join(data_path, exec_dir) + bin_path = join(blob_dir, exec_dir) if not exists(bin_path): continue for filename in os.listdir(bin_path): os.chmod(join(bin_path, filename), 0755) + blob = blob_dir else: self._cache.ensure(size) - with file(data_path, 'wb') as f: + with file(join(blob_dir, 'data'), 'wb') as f: shutil.copyfileobj(blob, f) - impl = sel.copy() + blob = f.name + impl = deepcopy(sel) impl['mtime'] = impl['ctime'] + impl['data']['blob'] = blob impls.create(impl) return cache_call(guid, size) - except Exception: - shutil.rmtree(data_path, ignore_errors=True) - raise result = [] for sel in request.session['solution']: diff --git a/sugar_network/client/journal.py b/sugar_network/client/journal.py index 646bced..ee2a2f3 100644 --- a/sugar_network/client/journal.py +++ b/sugar_network/client/journal.py @@ -90,7 +90,7 @@ class Routes(object): # Do not break SN like API guid = item['guid'] = item.pop('uid') if has_preview: - item['preview'] = _preview_url(guid) + item['preview'] = _preview(guid) return {'result': result, 'total': int(total)} @@ -100,7 +100,7 @@ class Routes(object): return {'guid': guid, 'title': get(guid, 'title'), 'description': get(guid, 'description'), - 'preview': _preview_url(guid), + 'preview': _preview(guid), } @route('GET', ['journal', None, 'preview']) @@ -193,6 +193,7 @@ def _prop_path(guid, prop): return _ds_path(guid, 'metadata', prop) -def _preview_url(guid): - return 'http://127.0.0.1:%s/journal/%s/preview' % \ - (client.ipc_port.value, guid) +def _preview(guid): + return {'url': 'http://127.0.0.1:%s/journal/%s/preview' % + (client.ipc_port.value, guid), + } diff --git a/sugar_network/db/directory.py b/sugar_network/db/directory.py index eb2f0e2..96a2923 100644 --- a/sugar_network/db/directory.py +++ b/sugar_network/db/directory.py @@ -313,7 +313,7 @@ class Directory(object): if op is not None: op(patch) for prop, meta in merge.items(): - is_blob = isinstance(self.metadata[prop], BlobProperty) + is_blob = isinstance(self.metadata.get(prop), BlobProperty) record.set(prop, cleanup_blob=is_blob, **meta) if record.consistent: diff --git a/sugar_network/db/routes.py b/sugar_network/db/routes.py index 77e2c4b..123e001 100644 --- a/sugar_network/db/routes.py +++ b/sugar_network/db/routes.py @@ -267,11 +267,16 @@ class Routes(object): if value is None: value = prop.default elif isinstance(value, Blob): - value = value.get('url') - if value is None: - value = '/'.join(['', metadata.name, doc.guid, name]) - if value.startswith('/'): - value = request.static_prefix + value + for key in ('mtime', 'seqno', 'blob'): + if key in value: + del value[key] + url = value.get('url') + if url is None: + value['url'] = '/'.join([ + request.static_prefix, metadata.name, doc.guid, name, + ]) + elif url.startswith('/'): + value['url'] = request.static_prefix + url result[name] = value return result diff --git a/sugar_network/toolkit/__init__.py b/sugar_network/toolkit/__init__.py index 5988e5d..a32d87f 100644 --- a/sugar_network/toolkit/__init__.py +++ b/sugar_network/toolkit/__init__.py @@ -485,7 +485,7 @@ def unique_filename(root, filename): class mkdtemp(str): def __new__(cls, **kwargs): - if cachedir.value: + if cachedir.value and 'dir' not in kwargs: if not exists(cachedir.value): os.makedirs(cachedir.value) kwargs['dir'] = cachedir.value @@ -496,7 +496,8 @@ class mkdtemp(str): return self def __exit__(self, exc_type, exc_value, traceback): - shutil.rmtree(self) + if exists(self): + shutil.rmtree(self) def svg_to_png(data, w, h): @@ -522,7 +523,7 @@ def svg_to_png(data, w, h): def TemporaryFile(*args, **kwargs): - if cachedir.value: + if cachedir.value and 'dir' not in kwargs: if not exists(cachedir.value): os.makedirs(cachedir.value) kwargs['dir'] = cachedir.value @@ -532,7 +533,7 @@ def TemporaryFile(*args, **kwargs): class NamedTemporaryFile(object): def __init__(self, *args, **kwargs): - if cachedir.value: + if cachedir.value and 'dir' not in kwargs: if not exists(cachedir.value): os.makedirs(cachedir.value) kwargs['dir'] = cachedir.value |