Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/utils.py
blob: 60a5647208bd1989f12618c88054ac387ba41c83 (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
#!/usr/bin/env python
# -*- coding: utf-8 -*-

# utils.py by:
#    Agustin Zubiaga <aguz@sugarlabs.org>
#    Ignacio Rodríguez <ignacio@sugarlabs.org>
#    Alan Aguiar <alanjas@gmail.com>

# 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, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

import os
import gtk
import urllib
import logging

from sugar.activity import activity
from sugar.bundle.activitybundle import ActivityBundle

# Paths
LIST_DOWNLOAD = "http://people.sugarlabs.org/ignacio/store.lst"
LIST_DOWNLOAD_MIRROR1 = "http://people.sugarlabs.org/aguz/store.lst"
LIST_DOWNLOAD_MIRROR2 = "http://www.fing.edu.uy/~aaguiar/files/store.lst"
LIST_PATH = os.path.join(activity.get_bundle_path(), 'store.lst')
ICONS_DIR = os.path.join(activity.get_activity_root(), 'data')
TMP_DIR = os.path.join(activity.get_activity_root(), "tmp")

downloading = False

# Logging
_logger = logging.getLogger('install-activity')
_logger.setLevel(logging.DEBUG)
logging.basicConfig()


def get_logger():
    return _logger


def _know():
    _file = None
    try:
        _file = urllib.urlopen(LIST_DOWNLOAD)
    except:
        try:
            _file = urllib.urlopen(LIST_DOWNLOAD_MIRROR1)
        except:
            try:
                _file = urllib.urlopen(LIST_DOWNLOAD_MIRROR2)
            except:
                pass
    return _file


def update_list():
    """Download the latest list version"""
    global downloading
    try:
        downloading = True
        remote_file = _know()
        if remote_file is not None:
            _file = open(LIST_PATH, 'w')
            _file.write(remote_file.read())
            _file.close()
            remote_file.close()
    except:
        pass

    downloading = False


def get_store_list():
    """Returns the store list"""
    store_list = []
    try:
        f = open(LIST_PATH, 'r')
        e = True
        while e:
            line = f.readline()
            if not(line):
                e = False
            else:
                line = line.replace('\n', '')
                l = line.split('|')
                store_list.append(l)
    except:
        pass

    return store_list


def get_icon(activity_id):
    """Returns the icon of an specified activity"""
    store_list = get_store_list()
    activity_obj = store_list[activity_id]
    number = activity_obj[0]

    file_image = os.path.join(ICONS_DIR, "icon%s" % number)
    if not os.path.exists(file_image):
        url =\
      'http://activities.sugarlabs.org/en-US/sugar/images/addon_icon/' + number
        f, headers = urllib.urlretrieve(url, file_image)

    pixbuf = gtk.gdk.pixbuf_new_from_file_at_size(file_image, 45, 45)

    return pixbuf


def download_activity(_id, progress_function):
    """Download (and install) an activity"""
    store_list = get_store_list()
    activity_obj = store_list[_id]

    name = activity_obj[1]
    name = name.lower()
    name = name.replace(' ', '_')
    n = activity_obj[0]
    web = 'http://activities.sugarlabs.org/es-ES/sugar/downloads/latest/'
    web = web + n + '/addon-' + n + '-latest.xo'
    version = activity_obj[3]

    def progress_changed(block, block_size, total_size):
        downloaded = block * block_size
        progress = downloaded * 100 / total_size

        progress_function(progress)

    xo = name + '-' + version + '.xo'
    file_path = os.path.join(activity.get_activity_root(), "data", xo)

    _logger.info(_("Downloading activity (%s)") % name)
    urllib.urlretrieve(web,
                       file_path,
                       reporthook=progress_changed)

    _logger.info(_("Installing activity (%s)") % name)
    install_activity(file_path, progress_function)


def install_activity(xofile, progress_function):
    """Install an downloaded activity"""
    # Show "Installing..." message
    progress_function(150)

    # Install the .xo
    bundle = ActivityBundle(xofile)
    bundle.install()

    # Remove the .xo
    os.remove(xofile)

    # Show "Installed..." message
    progress_function(200)

    _logger.info(_("Activity installed!"))