Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/sugar_network/client/injector.py
blob: cdcdc5e175cb84ea62a36404c4e15ff658c16d3f (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
# Copyright (C) 2010-2013 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/>.

import os
import json
import shutil
import logging
from os.path import join, exists, basename, dirname

from sugar_network import client, toolkit
from sugar_network.client import journal, cache
from sugar_network.toolkit import pipe, lsb_release


_PMS_PATHS = {
        'Debian': '/var/lib/dpkg/status',
        'Fedora': '/var/lib/rpm/Packages',
        'Ubuntu': '/var/lib/dpkg/status',
        }

_logger = logging.getLogger('client.injector')
_pms_path = _PMS_PATHS.get(lsb_release.distributor_id())
_mtime = None


def make(guid):
    return pipe.fork(_make, log_path=client.profile_path('logs', guid),
            context=guid, session={'context': guid})


def launch(guid, args=None, activity_id=None, object_id=None, uri=None,
        color=None):
    if object_id:
        if not activity_id:
            activity_id = journal.get(object_id, 'activity_id')
        if not color:
            color = journal.get(object_id, 'icon-color')

    if not activity_id:
        activity_id = journal.create_activity_id()

    if args is None:
        args = []
    args.extend([
        '-b', guid,
        '-a', activity_id,
        ])
    if object_id:
        args.extend(['-o', object_id])
    if uri:
        args.extend(['-u', uri])

    return pipe.fork(_launch, log_path=client.profile_path('logs', guid),
            context=guid, args=args, session={
                'context': guid,
                'activity_id': activity_id,
                'color': color,
                })


def clone(guid):
    return pipe.fork(_clone, log_path=client.profile_path('logs', guid),
            context=guid, session={'context': guid})


def clone_impl(context, **params):
    return pipe.fork(_clone_impl,
            log_path=client.profile_path('logs', context),
            context_guid=context, params=params, session={'context': context})


def invalidate_solutions(mtime):
    global _mtime
    _mtime = mtime


def _make(context):
    pipe.feedback('analyze')
    solution = _solve(context)
    pipe.feedback('solved', environ={'solution': solution})

    to_install = []
    for impl in solution:
        if 'install' in impl:
            to_install.extend(impl['install'])
    if to_install:
        pipe.trace('Install %s package(s)',
                ', '.join([i['name'] for i in to_install]))
        from sugar_network.client import packagekit
        packagekit.install(to_install)

    for impl in solution:
        if 'path' in impl or impl['stability'] == 'packaged':
            continue
        impl_path = cache.get(impl['id'], impl)
        if 'prefix' in impl:
            impl_path = join(impl_path, impl['prefix'])
        impl['path'] = impl_path

    pipe.feedback('ready')
    return solution


def _launch(context, args):
    solution = _make(context)

    args = solution[0]['command'] + (args or [])
    _logger.info('Executing %r feed: %s', context, args)
    pipe.feedback('exec')

    _activity_env(solution[0], os.environ)
    os.execvpe(args[0], args, os.environ)


def _clone(context):
    solution = _make(context)

    cloned = []
    try:
        for impl in solution:
            path = impl.get('path')
            if not path or \
                    path == '/':  # Fake path set by "sugar" dependency
                continue
            dst_path = toolkit.unique_filename(
                    client.activity_dirs.value[0], basename(path))
            cloned.append(dst_path)
            _logger.info('Clone implementation to %r', dst_path)
            toolkit.cptree(path, dst_path)
            impl['path'] = dst_path
    except Exception:
        while cloned:
            shutil.rmtree(cloned.pop(), ignore_errors=True)
        raise

    _set_cached_solution(context, solution)


def _clone_impl(context_guid, params):
    conn = client.IPCConnection()

    context = conn.get(['context', context_guid], reply=['title'])
    impl = conn.meta(['context', context_guid], cmd='clone', **params)

    src_path = cache.get(impl['guid'], impl)
    if 'extract' in impl:
        src_path = join(src_path, impl['extract'])
    dst_path = toolkit.unique_filename(
            client.activity_dirs.value[0], basename(src_path))

    _logger.info('Clone implementation to %r', dst_path)
    toolkit.cptree(src_path, dst_path)

    _set_cached_solution(context_guid, [{
        'id': dst_path,
        'context': context_guid,
        'version': impl['version'],
        'name': context['title'],
        'stability': impl['stability'],
        'spec': join(dst_path, 'activity', 'activity.info'),
        'path': dst_path,
        'command': impl['commands']['activity']['exec'].split(),
        }])


def _solve(context):
    pipe.trace('Start solving %s feed', context)

    solution, stale = _get_cached_solution(context)
    if stale is False:
        pipe.trace('Reuse cached solution')
        return solution

    conn = client.IPCConnection()
    if solution is not None and conn.get(cmd='status')['route'] == 'offline':
        pipe.trace('Reuse stale cached solution in offline mode')
        return solution

    from sugar_network.client import solver

    solution = solver.solve(conn, context)
    _set_cached_solution(context, solution)

    return solution


def _activity_env(impl, environ):
    root = client.profile_path('data', impl['context'])
    impl_path = impl['path']

    for path in ['instance', 'data', 'tmp']:
        path = join(root, path)
        if not exists(path):
            os.makedirs(path)

    environ['PATH'] = ':'.join([
        join(impl_path, 'activity'),
        join(impl_path, 'bin'),
        environ['PATH'],
        ])
    environ['SUGAR_BUNDLE_PATH'] = impl_path
    environ['SUGAR_BUNDLE_ID'] = impl['context']
    environ['SUGAR_BUNDLE_NAME'] = impl['name'].encode('utf8')
    environ['SUGAR_BUNDLE_VERSION'] = impl['version']
    environ['SUGAR_ACTIVITY_ROOT'] = root
    environ['PYTHONPATH'] = impl_path + ':' + environ.get('PYTHONPATH', '')
    environ['SUGAR_LOCALEDIR'] = join(impl_path, 'locale')

    os.chdir(impl_path)


def _cached_solution_path(guid):
    return client.path('cache', 'solutions', guid[:2], guid)


def _get_cached_solution(guid):
    path = _cached_solution_path(guid)
    solution = None
    if exists(path):
        try:
            with file(path) as f:
                api_url, solution = json.load(f)
        except Exception, error:
            _logger.debug('Cannot open %r solution: %s', path, error)
    if solution is None:
        return None, None

    stale = (api_url != client.api_url.value)
    if not stale and _mtime is not None:
        stale = (_mtime > os.stat(path).st_mtime)
    if not stale and _pms_path is not None:
        stale = (os.stat(_pms_path).st_mtime > os.stat(path).st_mtime)

    for impl in solution:
        impl_path = impl.get('path')
        if impl_path and not exists(impl_path):
            os.unlink(path)
            return None, None
        if not stale:
            spec = impl.get('spec')
            if spec and exists(spec):
                stale = (os.stat(spec).st_mtime > os.stat(path).st_mtime)

    return solution, stale


def _set_cached_solution(guid, solution):
    path = _cached_solution_path(guid)
    if not exists(dirname(path)):
        os.makedirs(dirname(path))
    with file(path, 'w') as f:
        json.dump([client.api_url.value, solution], f)