Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAleksey Lim <alsroot@sugarlabs.org>2013-06-09 08:49:37 (GMT)
committer Aleksey Lim <alsroot@sugarlabs.org>2013-06-09 08:49:37 (GMT)
commite036cec6f898d4150d385ce0a6a2c33fed3b6662 (patch)
tree170d2aef764a56eeb3a93e4ff31aaf1b84d82a67
parent0c35215578314c428efe54222c2047bc2a5a3fa7 (diff)
Caluculate implementation sizes even for urls
-rw-r--r--sugar_network/resources/implementation.py20
-rw-r--r--sugar_network/toolkit/http.py5
-rwxr-xr-xtests/units/resources/implementation.py49
3 files changed, 69 insertions, 5 deletions
diff --git a/sugar_network/resources/implementation.py b/sugar_network/resources/implementation.py
index 1d3a861..d9774e7 100644
--- a/sugar_network/resources/implementation.py
+++ b/sugar_network/resources/implementation.py
@@ -15,6 +15,8 @@
# pylint: disable-msg=E1101,E0102,E0202
+import os
+
import xapian
from sugar_network import db, resources
@@ -95,11 +97,23 @@ class Implementation(Resource):
@data.setter
def data(self, value):
context = self.volume['context'].get(self['context'])
- if 'activity' in context['type']:
+ if 'activity' not in context['type']:
+ return value
+
+ def calc_uncompressed_size(path):
uncompressed_size = 0
- with Bundle(value['blob'], mime_type='application/zip') as bundle:
+ with Bundle(path, 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'
+
+ if 'blob' in value:
+ calc_uncompressed_size(value['blob'])
+ elif 'url' in value:
+ with util.NamedTemporaryFile() as f:
+ http.download(value['url'], f.name)
+ value['blob_size'] = os.stat(f.name).st_size
+ calc_uncompressed_size(f.name)
+
+ value['mime_type'] = 'application/vnd.olpc-sugar'
return value
diff --git a/sugar_network/toolkit/http.py b/sugar_network/toolkit/http.py
index 1f4ece1..691d13a 100644
--- a/sugar_network/toolkit/http.py
+++ b/sugar_network/toolkit/http.py
@@ -89,6 +89,11 @@ class ServiceUnavailable(Status):
status_code = 503
+def download(url, dst_path):
+ # TODO (?) Reuse HTTP session
+ return Client().download(url, dst_path)
+
+
class Client(object):
def __init__(self, api_url='', creds=None, trust_env=True, max_retries=0):
diff --git a/tests/units/resources/implementation.py b/tests/units/resources/implementation.py
index bfee1fb..967b588 100755
--- a/tests/units/resources/implementation.py
+++ b/tests/units/resources/implementation.py
@@ -1,17 +1,20 @@
#!/usr/bin/env python
# sugar-lint: disable
+import os
import zipfile
import xapian
from __init__ import tests
+from sugar_network import db
+from sugar_network.db.router import Router, route
from sugar_network.resources.volume import Volume
from sugar_network.resources.implementation import _encode_version, Implementation
from sugar_network.node.commands import NodeCommands
from sugar_network.client import IPCClient
-from sugar_network.toolkit import http
+from sugar_network.toolkit import http, coroutine
class ImplementationTest(tests.Test):
@@ -64,7 +67,7 @@ class ImplementationTest(tests.Test):
xapian.sortable_serialise(eval('1''0000''0000''6''001')),
_encode_version('1-post1.2-3'))
- def test_Activities(self):
+ def test_ActivitityFiles(self):
self.start_online_client()
client = IPCClient()
@@ -95,6 +98,48 @@ class ImplementationTest(tests.Test):
self.assertNotEqual(5, data['blob_size'])
self.assertEqual(5, data.get('uncompressed_size'))
+ def test_ActivityUrls(self):
+ bundle = zipfile.ZipFile('blob', 'w')
+ bundle.writestr('topdir/probe', 'probe')
+ bundle.close()
+ bundle = file('blob', 'rb').read()
+ bundle_size = os.stat('blob').st_size
+ uncompressed_size = 5
+
+ class Files(db.CommandsProcessor):
+
+ @route('GET', '/bundle')
+ def bundle(self, request, response):
+ return bundle
+
+ self.start_online_client()
+ client = IPCClient()
+ files_server = coroutine.WSGIServer(('127.0.0.1', 9999), Router(Files()))
+ coroutine.spawn(files_server.serve_forever)
+ coroutine.dispatch()
+
+ context = client.post(['context'], {
+ 'type': 'activity',
+ 'title': 'title',
+ 'summary': 'summary',
+ 'description': 'description',
+ })
+ impl = client.post(['implementation'], {
+ 'context': context,
+ 'license': 'GPLv3+',
+ 'version': '1',
+ 'stability': 'stable',
+ 'notes': '',
+ })
+ client.put(['implementation', impl, 'data'], {'url': 'http://127.0.0.1:9999/bundle'})
+
+ data = self.node_volume['implementation'].get(impl).meta('data')
+ self.assertEqual('application/vnd.olpc-sugar', data['mime_type'])
+ self.assertEqual(bundle_size, data['blob_size'])
+ self.assertEqual(uncompressed_size, data.get('uncompressed_size'))
+ self.assertEqual('http://127.0.0.1:9999/bundle', data['url'])
+ assert 'blob' not in data
+
def test_WrongAuthor(self):
self.start_online_client()
client = IPCClient()