Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/extensions/cpsection/updater/model.py
blob: 7a4a9088fec9f15fe05fb8292f4baad4e1bd1689 (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
#!/usr/bin/python
# Copyright (C) 2009, Sugar Labs
#
# 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 2 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, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
"""Sugar bundle updater: model.

This module implements the non-GUI portions of the bundle updater, including
list of installed bundls, whether updates are needed, and the URL at which to
find the bundle updated.

`UpdateList` inherits from `gtk.ListStore` in order to work closely with the
view pane. This module requires `gtk`.
"""

import os
import tempfile
import locale
import logging
import urllib
from gettext import gettext as _
import traceback

import gobject
import gtk

from sugar.bundle.activitybundle import ActivityBundle
from sugar.datastore import datastore

from jarabe.model import bundleregistry
from backends import aslo

"""List of columns in the `UpdateList`."""
BUNDLE_ID, \
    BUNDLE, \
    ICON, \
    NAME, \
    CURRENT_VERSION, \
    UPDATE_VERSION, \
    UPDATE_SIZE, \
    UPDATE_URL, \
    DESCRIPTION, \
    UPDATE_SELECTED, \
    UPDATE_AVAILABLE, \
    IS_HEADER = xrange(12)

class UpdateList(gtk.ListStore):
    """Model which provides backing storage for the BUNDLE list treeview."""

    __gproperties__ = {
        'is_valid': (gobject.TYPE_BOOLEAN, 'is valid',
                     'true iff the UpdateList has been properly refreshed',
                     False, gobject.PARAM_READABLE),
    }

    def __init__(self):
        logging.debug('STARTUP: Loading the bundle updater')

        gtk.ListStore.__init__(self,
                               str, object, gtk.gdk.Pixbuf, str,
                               long, long, long, str,
                               str, bool, bool, bool)

        self._cancel = False
        self._is_valid = True
        self._registry = bundleregistry.get_registry()
        self._steps_count = 0
        self._steps_total = 0
        self._progress_cb = None

    def refresh_list(self, progress_callback=lambda n, extra: None,
                           clear_cache=True):
        self._cancel = False
        self._progress_cb = progress_callback
        self._progress_cb(None, _('Looking for local actvities...'))

        self.clear()
        self._steps_total = len([i for i in self._registry])
        self._steps_count = 0

        row_map = {}

        for bundle in self._registry:
            self._make_progress(_('Checking %s...') % bundle.get_name())

            if self._cancel:
                break # Cancel bundle refresh

            row = [None, None, None, None,
                   0, 0, 0, None,
                   None, True, False, False]
            row[BUNDLE] = bundle
            row[BUNDLE_ID] = bundle.get_bundle_id()

            if self._refresh_row(row):
                row_map[row[BUNDLE_ID]] = self.get_path(self.append(row))

    def cancel(self):
        self._cancel = True

    def _refresh_row(self, row):
        logging.debug('Looking for %s' % row[BUNDLE].get_name())

        try:
            new_ver, new_url, new_size = aslo.fetch_update_info(row[BUNDLE])
        except Exception:
            logging.warning('Failure updating %s\n%s' % \
                    (row[BUNDLE].get_name(), traceback.format_exc()))
            return False

        row[CURRENT_VERSION] = row[BUNDLE].get_activity_version()
        row[UPDATE_VERSION] = long(new_ver)

        if row[CURRENT_VERSION] >= row[UPDATE_VERSION]:
            logging.debug('Skip %s update' % row[BUNDLE].get_name())
            return False

        row[ICON] = gtk.gdk.pixbuf_new_from_file_at_size(
                row[BUNDLE].get_icon(), 32, 32)
        row[NAME] = row[BUNDLE].get_name()
        row[UPDATE_URL] = new_url
        row[UPDATE_AVAILABLE] = True
        row[UPDATE_SIZE] = new_size
        row[DESCRIPTION] = \
                _('From version %(current)d to %(new)s (Size: %(size)s)') % \
                {'current': row[CURRENT_VERSION],
                 'new': row[UPDATE_VERSION],
                 'size': _humanize_size(row[UPDATE_SIZE])}
        row[UPDATE_SELECTED] = True

        return True

    def install_updates(self, progress_cb=(lambda n, row: None)):
        self._cancel = False
        self._progress_cb = progress_cb
        self._steps_total = len([0 for row in self if row[UPDATE_SELECTED]]) * 2
        self._steps_count = 0

        installed = 0

        for row in self:
            if self._cancel:
                return installed
            if row[IS_HEADER]:
                continue
            if not row[UPDATE_SELECTED]:
                continue

            logging.debug('Downloading %s from %s' % \
                    (row[NAME], row[UPDATE_URL]))
            self._make_progress(_('Downloading %s...') % row[NAME])

            fd, xofile = tempfile.mkstemp(suffix='.xo')
            os.close(fd)
            try:
                try:
                    urllib.urlretrieve(row[UPDATE_URL], xofile)
                except Exception, e:
                    logging.warning("Can't download %s: %s" % \
                            (row[UPDATE_URL], e))
                    continue

                if self._cancel:
                    return installed

                logging.debug('Installing %s' % row[NAME])
                self._make_progress(_('Installing %s...') % row[NAME])

                jobject = datastore.create()
                jobject.metadata['title'] = \
                        '%s-%s' % (row[NAME], row[UPDATE_VERSION])
                jobject.metadata['mime_type'] = ActivityBundle.MIME_TYPE
                jobject.file_path = xofile
                datastore.write(jobject, transfer_ownership=True)

                installed += 1
            finally:
                if os.path.exists(xofile):
                    os.unlink(xofile)

        return installed

###############################################################################

    def _make_progress(self, msg=None): #FIXME needs better name
        """Helper function to do progress update."""
        self._steps_count += 1
        self._progress_cb(float(self._steps_count)/self._steps_total, msg)

    def _sum_rows(self, row_func):
        """Sum the values returned by row_func called on all non-header
        rows."""
        return sum(row_func(r) for r in self if not r[IS_HEADER])

###############################################################################

    def updates_available(self):
        """Return the number of updates available.

        Updated by `refresh`."""
        return self._sum_rows(lambda r: 1 if r[UPDATE_AVAILABLE] else 0)

    def updates_selected(self):
        """Return the number of updates selected."""
        return self._sum_rows(lambda r:
                1 if r[UPDATE_AVAILABLE] and r[UPDATE_SELECTED] else 0)

    def updates_size(self):
        """Returns the size (in bytes) of the selected updates available.

        Updated by `refresh`."""
        return self._sum_rows(lambda r:
                r[UPDATE_SIZE] if r[UPDATE_AVAILABLE] and \
                        r[UPDATE_SELECTED] else 0)
    def is_valid(self):
        """The UpdateList is invalidated before it is refreshed, and when
        the group information is modified without refreshing."""
        return self._is_valid

###############################################################################
# Utility Funtions

def _humanize_size(bytes_):
    """
    Convert a given size in bytes to a nicer better readable unit
    """
    if bytes_ == 0:
        # TRANS: download size is 0
        return _('None')
    elif bytes_ < 1024:
        # TRANS: download size of very small updates
        return _('1 KB')
    elif bytes_ < 1024 * 1024:
        # TRANS: download size of small updates, e.g. '250 KB'
        return locale.format(_('%.0f KB'), bytes_ / 1024)
    else:
        # TRANS: download size of updates, e.g. '2.3 MB'
        return locale.format(_('%.1f MB'), bytes_ / 1024 / 1024)

def print_available(ul):#FIXME this should onlu return available updates
    def opt(x):
        if x is None or x == '':
            return ''
        return ': %s' % x
    for row in ul:
        if row[IS_HEADER]:
            print row[NAME] + opt(row[DESCRIPTION])
        else:
            print '*', row[NAME] + opt(row[DESCRIPTION])
    print
    #print _('%(number)d updates available.  Size: %(size)s') % \
    #      { 'number': ul.updates_available(),
    #        'size': _humanize_size(ul.updates_size()) }

###############################################################################
# Self-test code.
def _main():
    """Self-test."""
    update_list = UpdateList()
    update_list.refresh_list()
    print_available(update_list)
    update_list.install_updates()

if __name__ == '__main__':
    _main()