Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/sugar_network_server/resources/implementation.py
blob: 8b2f5adda7ca21413dee2bd991c3bfb6c13b4b69 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# Copyright (C) 2012, Aleksey Lim
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

# pylint: disable-msg=E1101,E0102,E0202

import os
import zipfile
from cStringIO import StringIO
from ConfigParser import ConfigParser
from os.path import exists, join
from gettext import gettext as _

import active_document as ad
import restful_document as rd
enforce = ad.util.enforce

from sugar_network_server import env
from sugar_network_server.licenses import GOOD_LICENSES
from sugar_network_server.resources.resource import Resource


_ASLO_DOWNLOAD_URL = 'http://download.sugarlabs.org/activities'
_ASLO_ACTIVITIES_PATH = '/upload/activities'


class Implementation(Resource):

    @ad.active_property(prefix='C',
            permissions=ad.ACCESS_CREATE | ad.ACCESS_READ)
    def context(self, value):
        return value

    @ad.active_property(prefix='L', typecast=[GOOD_LICENSES],
            permissions=ad.ACCESS_CREATE | ad.ACCESS_READ)
    def license(self, value):
        return value

    @ad.active_property(slot=2, prefix='V',
            permissions=ad.ACCESS_CREATE | ad.ACCESS_READ)
    def version(self, value):
        return value

    @ad.active_property(slot=3, prefix='D',
            permissions=ad.ACCESS_CREATE | ad.ACCESS_READ, typecast=int)
    def date(self, value):
        return value

    @ad.active_property(slot=4, prefix='S',
            permissions=ad.ACCESS_CREATE | ad.ACCESS_READ,
            typecast=env.STABILITIES)
    def stability(self, value):
        return value

    @ad.active_property(ad.StoredProperty, typecast=dict, default={})
    def feed(self, value):
        return value

    @feed.setter
    def feed(self, value):
        value.setdefault('implementations', {})
        value['implementations'].setdefault('*-*', {})
        return value

    @ad.active_property(full_text=True,
            permissions=ad.ACCESS_CREATE | ad.ACCESS_READ)
    def notes(self, value):
        return value

    @ad.active_property(ad.BlobProperty)
    def bundle(self, value):
        return value

    def recv_blob(self, prop, url):
        if not url.startswith(_ASLO_DOWNLOAD_URL):
            return Resource.recv_blob(self, prop, url)

        path = url[len(_ASLO_DOWNLOAD_URL):].strip('/').split('/')
        enforce(len(path) == 2 and path[-1].endswith('.xo'),
                _('Incorrect activities.sugarlabs.org path'))

        feed = self.feed
        feed['implementations']['*-*']['url'] = url

        path = join(_ASLO_ACTIVITIES_PATH, *path)
        if exists(path):
            try:
                zp = zipfile.ZipFile(path)
                extract = zp.namelist()[0].split(os.sep)[0]

                activity_info_data = StringIO(
                        zp.read(join(extract, 'activity', 'activity.info')))
                activity_info = ConfigParser()
                activity_info.readfp(activity_info_data)

                if activity_info.has_option('Activity', 'exec'):
                    exec_cmd = activity_info.get('Activity', 'exec')
                else:
                    exec_cmd = 'sugar-activity %s' % \
                            activity_info.get('Activity', 'class')
                feed['commands'] = {
                        'activity': exec_cmd,
                        }
            except Exception, error:
                ad.util.exception()
                raise RuntimeError(_('Cannot read bundle: %s') % error)

        self.feed = feed
        self.post()

    def send_blob(self, prop):
        url = self.feed['implementations']['*-*'].get('url')
        if url:
            raise rd.SeeOther(url)
        return Resource.send_blob(self, prop)