diff options
author | Aleksey Lim <alsroot@sugarlabs.org> | 2013-06-02 13:40:12 (GMT) |
---|---|---|
committer | Aleksey Lim <alsroot@sugarlabs.org> | 2013-06-02 13:40:12 (GMT) |
commit | bb22cd557417556a622137982e3789a9c326b5fa (patch) | |
tree | 25288ef22a4f810aab12396508d3a0b70e4fc65f | |
parent | 1a81d18da1ca6d6bf097019859738e03c131eacd (diff) |
Process packages updates
-rw-r--r-- | sugar_network/node/commands.py | 28 | ||||
-rwxr-xr-x | tests/units/node/node.py | 40 |
2 files changed, 63 insertions, 5 deletions
diff --git a/sugar_network/node/commands.py b/sugar_network/node/commands.py index 0ef07a7..5cc39f4 100644 --- a/sugar_network/node/commands.py +++ b/sugar_network/node/commands.py @@ -66,13 +66,31 @@ class NodeCommands(db.VolumeCommands, Commands): @db.route('GET', '/packages') def route_packages(self, request, response): enforce(node.files_root.value, http.BadRequest, 'Disabled') - path = join(node.files_root.value, *request.path) - enforce(exists(path), http.NotFound, 'File was not found') - if isdir(path): + if request.path and request.path[-1] == 'updates': + root = join(node.files_root.value, *request.path[:-1]) + enforce(isdir(root), http.NotFound, 'Directory was not found') + result = [] + last_modified = 0 + for filename in os.listdir(root): + if '.' in filename: + continue + path = join(root, filename) + mtime = os.stat(path).st_mtime + if mtime > request.if_modified_since: + result.append(filename) + last_modified = max(last_modified, mtime) response.content_type = 'application/json' - return os.listdir(path) + if last_modified: + response.last_modified = last_modified + return result else: - return util.iter_file(path) + path = join(node.files_root.value, *request.path) + enforce(exists(path), http.NotFound, 'File was not found') + if isdir(path): + response.content_type = 'application/json' + return os.listdir(path) + else: + return util.iter_file(path) @db.volume_command(method='GET', cmd='stat', mime_type='application/json') diff --git a/tests/units/node/node.py b/tests/units/node/node.py index c8d84c0..e28f6f3 100755 --- a/tests/units/node/node.py +++ b/tests/units/node/node.py @@ -3,6 +3,7 @@ import time import json +from email.utils import formatdate, parsedate from os.path import exists from __init__ import tests @@ -346,6 +347,45 @@ class NodeTest(tests.Test): self.assertEqual(['package'], client.get(['packages', 'repo', 'arch'])) self.assertEqual('file', client.get(['packages', 'repo', 'arch', 'package'])) + def test_PackageUpdatesRoute(self): + node.files_root.value = '.' + self.touch( + ('packages/repo/1', '', 1), + ('packages/repo/1.1', '', 1), + ('packages/repo/2', '', 2), + ('packages/repo/2.2', '', 2), + ) + volume = self.start_master() + ipc = Client() + + self.assertEqual( + sorted(['1', '2']), + sorted(ipc.get(['packages', 'repo', 'updates']))) + + response = ipc.request('GET', ['packages', 'repo', 'updates'], headers={'if-modified-since': formatdate(0)}) + self.assertEqual( + sorted(['1', '2']), + sorted(json.loads(response.content))) + self.assertEqual(2, time.mktime(parsedate(response.headers['last-modified']))) + + response = ipc.request('GET', ['packages', 'repo', 'updates'], headers={'if-modified-since': formatdate(1)}) + self.assertEqual( + sorted(['2']), + sorted(json.loads(response.content))) + self.assertEqual(2, time.mktime(parsedate(response.headers['last-modified']))) + + response = ipc.request('GET', ['packages', 'repo', 'updates'], headers={'if-modified-since': formatdate(2)}) + self.assertEqual( + sorted([]), + sorted(json.loads(response.content))) + assert 'last-modified' not in response.headers + + response = ipc.request('GET', ['packages', 'repo', 'updates'], headers={'if-modified-since': formatdate(3)}) + self.assertEqual( + sorted([]), + sorted(json.loads(response.content))) + assert 'last-modified' not in response.headers + def test_Clone(self): volume = self.start_master() client = Client() |