Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/sugar/activity/bundle.py
blob: feded650323dad055417f6caf8e205e6fb7255ea (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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
# Copyright (C) 2007, Red Hat, Inc.
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library 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
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.

"""Metadata description of a given application/activity"""

import logging
import locale
import os
import zipfile
from ConfigParser import ConfigParser
import StringIO
import tempfile

import dbus

from sugar import env
from sugar import activity

_PYTHON_FACTORY='sugar-activity-factory'

_DBUS_SHELL_SERVICE = "org.laptop.Shell"
_DBUS_SHELL_PATH = "/org/laptop/Shell"
_DBUS_ACTIVITY_REGISTRY_IFACE = "org.laptop.Shell.ActivityRegistry"

class AlreadyInstalledException(Exception): pass
class NotInstalledException(Exception): pass
class InvalidPathException(Exception): pass
class ZipExtractException(Exception): pass
class RegistrationException(Exception): pass

class Bundle:
    """Metadata description of a given application/activity
    
    The metadata is normally read from an activity.info file,
    which is an INI-style configuration file read using the 
    standard Python ConfigParser module.
    
    The format reference for the Bundle definition files is 
    available for further reference:
    
        http://wiki.laptop.org/go/Activity_bundles
    """
    def __init__(self, path):
        self._init_with_path(path)

    def _init_with_path(self, path):
        self._name = None
        self._icon = None
        self._service_name = None
        self._mime_types = None
        self._show_launcher = True
        self._valid = True
        self._path = path
        self._activity_version = 0

        info_file = self._get_info_file()
        if info_file:
            self._parse_info(info_file)
        else:
            self._valid = False

        linfo_file = self._get_linfo_file()
        if linfo_file:
            self._parse_linfo(linfo_file)

    def _get_info_file(self):
        info_file = None

        if os.path.isdir(self._path):
            info_path = os.path.join(self._path, 'activity', 'activity.info')
            if os.path.isfile(info_path):
                info_file = open(info_path)
        else:
            zip_file = zipfile.ZipFile(self._path)
            file_names = zip_file.namelist()
            root_dir = self._get_bundle_root_dir(file_names)
            info_path = os.path.join(root_dir, 'activity', 'activity.info')
            if info_path in file_names:
                info_data = zip_file.read(info_path)
                info_file = StringIO.StringIO(info_data)
            zip_file.close()

        return info_file

    def _parse_info(self, info_file):
        cp = ConfigParser()
        cp.readfp(info_file)

        section = 'Activity'

        if cp.has_option(section, 'service_name'):
            self._service_name = cp.get(section, 'service_name')
        else:
            self._valid = False
            logging.error('%s must specify a service name' % self._path)

        if cp.has_option(section, 'name'):
            self._name = cp.get(section, 'name')
        else:
            self._valid = False
            logging.error('%s must specify a name' % self._path)

        if cp.has_option(section, 'class'):
            self._class = cp.get(section, 'class')
            self._exec = '%s --bundle-path="%s"' % (
              env.get_bin_path(_PYTHON_FACTORY), self._path)
        elif cp.has_option(section, 'exec'):
            self._class = None
            cmdline = cp.get(section, 'exec')
            cmdline = os.path.join(self._path, cmdline)
            cmdline = cmdline.replace('$SUGAR_BUNDLE_PATH', self._path) 
            cmdline = os.path.expandvars(cmdline)
            self._exec = cmdline
        else:
            self._exec = None
            self._valid = False
            logging.error('%s must specify exec or class' % self._path)

        if cp.has_option(section, 'mime_types'):
            mime_list = cp.get(section, 'mime_types')
            self._mime_types = mime_list.strip(';')

        if cp.has_option(section, 'show_launcher'):
            if cp.get(section, 'show_launcher') == 'no':
                self._show_launcher = False

        if cp.has_option(section, 'icon'):
            self._icon = cp.get(section, 'icon')

        if cp.has_option(section, 'activity_version'):
            self._activity_version = int(cp.get(section, 'activity_version'))

    def _parse_linfo(self, linfo_file):
        cp = ConfigParser()
        cp.readfp(linfo_file)

        section = 'Activity'

        if cp.has_option(section, 'name'):
            self._name = cp.get(section, 'name')

    def _get_linfo_file(self):
        linfo_file = None

        lang = locale.getdefaultlocale()[0]
        if not lang:
            return None

        if os.path.isdir(self._path):
            linfo_path = os.path.join(self.get_locale_path(), lang, 'activity.linfo')
            if not os.path.isfile(linfo_path):
                linfo_path = os.path.join(self.get_locale_path(), lang[:2], 'activity.linfo')
            if os.path.isfile(linfo_path):
                linfo_file = open(linfo_path)
        else:
            zip_file = zipfile.ZipFile(self._path)
            file_names = zip_file.namelist()
            root_dir = self._get_bundle_root_dir(file_names)
            linfo_path = os.path.join(root_dir, 'locale', lang, 'activity.linfo')
            if not linfo_path in file_names:
                linfo_path = os.path.join(root_dir, 'locale', lang[:2], 'activity.linfo')
            if linfo_path in zip_file.namelist():                
                linfo_data = zip_file.read(linfo_path)
                linfo_file = StringIO.StringIO(linfo_data)

            zip_file.close()

        return linfo_file

    def is_valid(self):
        return self._valid

    def get_locale_path(self):
        """Get the locale path inside the activity bundle."""
        return os.path.join(self._path, 'locale')

    def get_icons_path(self):
        """Get the icons path inside the activity bundle."""
        return os.path.join(self._path, 'icons')

    def get_path(self):
        """Get the activity bundle path."""
        return self._path

    def get_name(self):
        """Get the activity user visible name."""
        return self._name

    def get_service_name(self):
        """Get the activity service name"""
        return self._service_name

    def get_icon(self):
        """Get the activity icon name"""
        if os.path.isdir(self._path):
            activity_path = os.path.join(self._path, 'activity')
            return os.path.join(activity_path, self._icon + '.svg')
        else:
            zip_file = zipfile.ZipFile(self._path)
            file_names = zip_file.namelist()
            root_dir = self._get_bundle_root_dir(file_names)
            icon_path = os.path.join(root_dir, 'activity', self._icon + '.svg')
            print icon_path
            print file_names
            if icon_path in file_names:
                icon_data = zip_file.read(icon_path)
                temp_file, temp_file_path = tempfile.mkstemp(self._icon)
                os.write(temp_file, icon_data)
                os.close(temp_file)
                return temp_file_path
            else:
                return None

    def get_activity_version(self):
        """Get the activity version"""
        return self._activity_version

    def get_exec(self):
        """Get the command to execute to launch the activity factory"""
        return self._exec

    def get_class(self):
        """Get the main Activity class"""
        return self._class

    def get_mime_types(self):
        """Get the MIME types supported by the activity"""
        return self._mime_types

    def get_show_launcher(self):
        """Get whether there should be a visible launcher for the activity"""
        return self._show_launcher

    def is_installed(self):
        if self._valid and activity.get_registry().get_activity(self._service_name):
            return True
        else:
            return False

    def _get_bundle_root_dir(self, file_names):
        """
        We check here that all the files in the .xo are inside one only dir
        (bundle_root_dir).
        """
        bundle_root_dir = None
        for file_name in file_names:
            if not bundle_root_dir:
                bundle_root_dir = file_name.split('/')[0]
                if not bundle_root_dir.endswith('.activity'):
                    raise 'Incorrect bundle.'
            else:
                if not file_name.startswith(bundle_root_dir):
                    raise 'Incorrect bundle.'

        return bundle_root_dir

    def install(self):
        if self.is_installed():
            raise AlreadyInstalledException

        ext = os.path.splitext(self._path)[1]
        if not os.path.isfile(self._path):
            raise InvalidPathException

        bundle_dir = env.get_user_activities_path()
        if not os.path.isdir(bundle_dir):
            os.mkdir(bundle_dir)

        zip_file = zipfile.ZipFile(self._path)
        file_names = zip_file.namelist()
        bundle_root_dir = self._get_bundle_root_dir(file_names)
        bundle_path = os.path.join(bundle_dir, bundle_root_dir)

        if os.spawnlp(os.P_WAIT, 'unzip', 'unzip', self._path, '-d', bundle_dir):
            raise ZipExtractException

        self._init_with_path(bundle_path)
        
        bus = dbus.SessionBus()
        proxy_obj = bus.get_object(_DBUS_SHELL_SERVICE, _DBUS_SHELL_PATH)
        dbus_service = dbus.Interface(proxy_obj, _DBUS_ACTIVITY_REGISTRY_IFACE)
        if not dbus_service.AddBundle(bundle_path):
            raise RegistrationException

    def deinstall(self):
        if not self.is_installed():
            raise NotInstalledException

        ext = os.path.splitext(self._path)[1]
        if not os.path.isfile(self._path) or ext != '.activity':
            raise InvalidPathException

        for root, dirs, files in os.walk(self._path, topdown=False):
            for name in files:
                os.remove(os.path.join(root, name))
            for name in dirs:
                os.rmdir(os.path.join(root, name))
        os.rmdir(self._path)

        self._init_with_path(None)

        # TODO: notify shell