diff options
author | Aleksey Lim <alsroot@sugarlabs.org> | 2013-06-09 08:11:37 (GMT) |
---|---|---|
committer | Aleksey Lim <alsroot@sugarlabs.org> | 2013-06-09 08:11:37 (GMT) |
commit | 0c35215578314c428efe54222c2047bc2a5a3fa7 (patch) | |
tree | 57c25f8bbf0bc7ec3c65944fd1baa5fa0a9dd2f5 | |
parent | 52e446781d312702def4fb18fff98051409d49b9 (diff) |
Calculate uncompressed size for activity implementations
-rw-r--r-- | sugar_network/client/clones.py | 2 | ||||
-rw-r--r-- | sugar_network/db/metadata.py | 6 | ||||
-rw-r--r-- | sugar_network/node/commands.py | 1 | ||||
-rw-r--r-- | sugar_network/node/volume.py | 3 | ||||
-rw-r--r-- | sugar_network/resources/implementation.py | 6 | ||||
-rw-r--r-- | sugar_network/toolkit/bundle.py | 21 | ||||
-rw-r--r-- | tests/units/client/__main__.py | 1 | ||||
-rwxr-xr-x | tests/units/db/document.py | 3 | ||||
-rwxr-xr-x | tests/units/db/volume.py | 1 | ||||
-rwxr-xr-x | tests/units/node/node.py | 9 | ||||
-rwxr-xr-x | tests/units/node/volume.py | 1 | ||||
-rwxr-xr-x | tests/units/resources/implementation.py | 15 | ||||
-rw-r--r-- | tests/units/toolkit/__main__.py | 1 | ||||
-rwxr-xr-x | tests/units/toolkit/spec.py (renamed from tests/units/client/spec.py) | 0 |
14 files changed, 58 insertions, 12 deletions
diff --git a/sugar_network/client/clones.py b/sugar_network/client/clones.py index 9c240d3..1a977bd 100644 --- a/sugar_network/client/clones.py +++ b/sugar_network/client/clones.py @@ -168,7 +168,7 @@ class _Inotify(Inotify): icon_path = join(spec.root, spec['icon']) if exists(icon_path): - with file(icon_path, 'b') as f: + with file(icon_path, 'rb') as f: self._contexts.update(context, {'artifact_icon': {'blob': f}}) with util.NamedTemporaryFile() as f: diff --git a/sugar_network/db/metadata.py b/sugar_network/db/metadata.py index a5ba8c3..ea797a3 100644 --- a/sugar_network/db/metadata.py +++ b/sugar_network/db/metadata.py @@ -123,8 +123,10 @@ class PropertyMetadata(dict): if path_: with file(path_) as f: meta.update(json.load(f)) - if exists(path_ + PropertyMetadata.BLOB_SUFFIX): - meta['blob'] = path_ + PropertyMetadata.BLOB_SUFFIX + blob_path = path_ + PropertyMetadata.BLOB_SUFFIX + if exists(blob_path): + meta['blob'] = blob_path + meta['blob_size'] = os.stat(blob_path).st_size meta['mtime'] = int(os.stat(path_).st_mtime) dict.__init__(self, meta) diff --git a/sugar_network/node/commands.py b/sugar_network/node/commands.py index 0f443cb..a71fad6 100644 --- a/sugar_network/node/commands.py +++ b/sugar_network/node/commands.py @@ -317,7 +317,6 @@ class NodeCommands(db.VolumeCommands, Commands): db.VolumeCommands.on_update(self, request, props, event) if 'deleted' in props.get('layer', []): event['event'] = 'delete' - print '1>>>', request, props, event @db.directory_command_pre(method='GET') def _NodeCommands_find_pre(self, request): diff --git a/sugar_network/node/volume.py b/sugar_network/node/volume.py index 09a51ed..5a6d0f6 100644 --- a/sugar_network/node/volume.py +++ b/sugar_network/node/volume.py @@ -13,7 +13,6 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. -import os import logging from sugar_network.toolkit import BUFFER_SIZE, http, util, coroutine, enforce @@ -51,7 +50,7 @@ def diff(volume, in_seq, out_seq=None, exclude_seq=None, layer=None, blob_path = meta.pop('blob') yield {'guid': guid, 'diff': {prop: meta}, - 'blob_size': os.stat(blob_path).st_size, + 'blob_size': meta['blob_size'], 'blob': util.iter_file(blob_path), } elif fetch_blobs and 'url' in meta: diff --git a/sugar_network/resources/implementation.py b/sugar_network/resources/implementation.py index c29a41a..1d3a861 100644 --- a/sugar_network/resources/implementation.py +++ b/sugar_network/resources/implementation.py @@ -20,6 +20,7 @@ import xapian from sugar_network import db, resources from sugar_network.resources.volume import Resource from sugar_network.toolkit.licenses import GOOD_LICENSES +from sugar_network.toolkit.bundle import Bundle from sugar_network.toolkit import http, util, enforce @@ -95,5 +96,10 @@ class Implementation(Resource): def data(self, value): context = self.volume['context'].get(self['context']) if 'activity' in context['type']: + uncompressed_size = 0 + with Bundle(value['blob'], mime_type='application/zip') as bundle: + for arcname in bundle.get_names(): + uncompressed_size += bundle.getmember(arcname).size + value['uncompressed_size'] = uncompressed_size value['mime_type'] = 'application/vnd.olpc-sugar' return value diff --git a/sugar_network/toolkit/bundle.py b/sugar_network/toolkit/bundle.py index d7b12f1..eb3a18b 100644 --- a/sugar_network/toolkit/bundle.py +++ b/sugar_network/toolkit/bundle.py @@ -37,12 +37,16 @@ class Bundle(object): self._do_get_names = self._bundle.namelist self._do_extractfile = self._bundle.open self._do_extract = self._bundle.extract + self._do_getmember = self._bundle.getinfo + self._cast_info = _ZipInfo elif mime_type.split('/')[-1].endswith('-tar'): import tarfile self._bundle = tarfile.open(bundle) self._do_get_names = self._bundle.getnames self._do_extractfile = self._bundle.extractfile self._do_extract = self._bundle.extract + self._do_getmember = self._bundle.getmember + self._cast_info = lambda x: x else: raise BundleError('Unsupported bundle type for "%s" file, ' 'it can be either tar or zip.' % bundle) @@ -66,6 +70,9 @@ class Bundle(object): def extractall(self, path, members=None): self._bundle.extractall(path=path, members=members) + def getmember(self, name): + return self._cast_info(self._do_getmember(name)) + @property def rootdir(self): if self._rootdir is not False: @@ -122,3 +129,17 @@ def _detect_mime_type(filename): return 'application/x-xz-compressed-tar' if filename.endswith('.tar'): return 'application/x-tar' + + +class _ZipInfo(object): + + def __init__(self, info): + self._info = info + + @property + def name(self): + return self._info.filename + + @property + def size(self): + return self._info.file_size diff --git a/tests/units/client/__main__.py b/tests/units/client/__main__.py index ff0e495..5034221 100644 --- a/tests/units/client/__main__.py +++ b/tests/units/client/__main__.py @@ -9,7 +9,6 @@ from journal import * from offline_commands import * from online_commands import * from server_commands import * -from spec import * from solver import * if __name__ == '__main__': diff --git a/tests/units/db/document.py b/tests/units/db/document.py index f3150fe..e25ee29 100755 --- a/tests/units/db/document.py +++ b/tests/units/db/document.py @@ -371,6 +371,7 @@ class DocumentTest(tests.Test): 'blob': { 'mtime': 1, 'blob': tests.tmpdir + '/1/1/blob.blob', + 'blob_size': 1, }, }}, {'guid': '2', 'diff': { @@ -381,6 +382,7 @@ class DocumentTest(tests.Test): 'blob': { 'mtime': 2, 'blob': tests.tmpdir + '/2/2/blob.blob', + 'blob_size': 1, }, }}, {'guid': '3', 'diff': { @@ -403,6 +405,7 @@ class DocumentTest(tests.Test): 'blob': { 'mtime': 2, 'blob': tests.tmpdir + '/2/2/blob.blob', + 'blob_size': 1, }, }}, ], diff --git a/tests/units/db/volume.py b/tests/units/db/volume.py index 5c250b9..9d65c6d 100755 --- a/tests/units/db/volume.py +++ b/tests/units/db/volume.py @@ -266,6 +266,7 @@ class VolumeTest(tests.Test): blob_meta = { 'seqno': 2, 'blob': blob_path + '.blob', + 'blob_size': 4, 'digest': hashlib.sha1('blob').hexdigest(), 'mime_type': 'application/octet-stream', 'mtime': int(os.stat(blob_path).st_mtime), diff --git a/tests/units/node/node.py b/tests/units/node/node.py index 9e7f0c9..93ae517 100755 --- a/tests/units/node/node.py +++ b/tests/units/node/node.py @@ -4,6 +4,7 @@ import os import time import json +import zipfile from email.utils import formatdate, parsedate from os.path import exists @@ -434,9 +435,13 @@ class NodeTest(tests.Test): 'notes': '', 'requires': ['foo', 'bar'], }) - client.request('PUT', ['implementation', impl, 'data'], 'bundle') + bundle = zipfile.ZipFile('blob', 'w') + bundle.writestr('topdir/probe', 'probe') + bundle.close() + blob = file('blob', 'rb').read() + client.request('PUT', ['implementation', impl, 'data'], blob) - self.assertEqual('bundle', client.get(['context', context], cmd='clone', version='1', stability='stable', requires=['foo', 'bar'])) + self.assertEqual(blob, client.get(['context', context], cmd='clone', version='1', stability='stable', requires=['foo', 'bar'])) def call(cp, principal=None, content=None, **kwargs): diff --git a/tests/units/node/volume.py b/tests/units/node/volume.py index db1cdd4..85ec1e2 100755 --- a/tests/units/node/volume.py +++ b/tests/units/node/volume.py @@ -418,6 +418,7 @@ class VolumeTest(tests.Test): {'guid': guid, 'blob_size': len('payload'), 'diff': { 'prop': { 'digest': hashlib.sha1('payload').hexdigest(), + 'blob_size': len('payload'), 'mime_type': 'application/octet-stream', 'mtime': 0, }, diff --git a/tests/units/resources/implementation.py b/tests/units/resources/implementation.py index 3b711f6..bfee1fb 100755 --- a/tests/units/resources/implementation.py +++ b/tests/units/resources/implementation.py @@ -1,6 +1,8 @@ #!/usr/bin/env python # sugar-lint: disable +import zipfile + import xapian from __init__ import tests @@ -62,7 +64,7 @@ class ImplementationTest(tests.Test): xapian.sortable_serialise(eval('1''0000''0000''6''001')), _encode_version('1-post1.2-3')) - def test_SetMimeTypeForActivities(self): + def test_Activities(self): self.start_online_client() client = IPCClient() @@ -83,8 +85,15 @@ class ImplementationTest(tests.Test): self.assertEqual('image/png', self.node_volume['implementation'].get(impl).meta('data')['mime_type']) client.put(['context', context, 'type'], 'activity') - client.request('PUT', ['implementation', impl, 'data'], 'blob', {'Content-Type': 'image/png'}) - self.assertEqual('application/vnd.olpc-sugar', self.node_volume['implementation'].get(impl).meta('data')['mime_type']) + bundle = zipfile.ZipFile('blob', 'w') + bundle.writestr('topdir/probe', 'probe') + bundle.close() + client.request('PUT', ['implementation', impl, 'data'], file('blob', 'rb').read()) + + data = self.node_volume['implementation'].get(impl).meta('data') + self.assertEqual('application/vnd.olpc-sugar', data['mime_type']) + self.assertNotEqual(5, data['blob_size']) + self.assertEqual(5, data.get('uncompressed_size')) def test_WrongAuthor(self): self.start_online_client() diff --git a/tests/units/toolkit/__main__.py b/tests/units/toolkit/__main__.py index f3ca1ad..9871e6b 100644 --- a/tests/units/toolkit/__main__.py +++ b/tests/units/toolkit/__main__.py @@ -8,6 +8,7 @@ from mountpoints import * from rrd import * from util import * from options import * +from spec import * if __name__ == '__main__': tests.main() diff --git a/tests/units/client/spec.py b/tests/units/toolkit/spec.py index 891dbe8..891dbe8 100755 --- a/tests/units/client/spec.py +++ b/tests/units/toolkit/spec.py |