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

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

from zeroinstall.injector import model
from zeroinstall.injector.requirements import Requirements

from sugar_network.zerosugar import pipe, packagekit, Spec
from sugar_network.zerosugar.solution import solve
from sugar_network import local
from sugar_network.toolkit import sugar
from active_toolkit import util, enforce


_logger = logging.getLogger('zerosugar.injector')


def launch(mountpoint, context, command='activity', args=None):
    return pipe.fork(_launch, mountpoint, context, command, args)


def checkin(mountpoint, context, command='activity'):
    return pipe.fork(_checkin, mountpoint, context, command)


def _launch(mountpoint, context, command, args):
    if args is None:
        args = []

    solution = _make(context, command)
    cmd = solution.commands[0]
    args = cmd.path.split() + args

    _logger.info('Executing %s: %s', solution.interface, args)
    pipe.progress('exec')

    if command == 'activity':
        _activity_env(solution.top, os.environ)
    os.execvpe(args[0], args, os.environ)


def _checkin(mountpoint, context, command):
    solution = _make(context, command)

    checkedin = []
    try:
        for sel, __, __ in solution.walk():
            dst_path = util.unique_filename(
                    local.activity_dirs.value[0], basename(sel.local_path))
            checkedin.append(dst_path)
            _logger.info('Checkin implementation to %r', dst_path)
            util.cptree(sel.local_path, dst_path)
    except Exception:
        while checkedin:
            shutil.rmtree(checkedin.pop(), ignore_errors=True)
        raise


def _make(context, command):
    requirement = Requirements(context)
    requirement.command = command

    pipe.progress('analyze')
    solution = solve(requirement)

    to_install = []
    for sel, __, __ in solution.walk():
        to_install.extend(sel.to_install or [])
    if to_install:
        packagekit.install(to_install)

    for sel, __, __ in solution.walk():
        if sel.is_available():
            continue

        enforce(sel.download_sources,
                'No sources to download implementation for %r context',
                sel.interface)

        # TODO Per download progress
        pipe.progress('download')

        impl = sel.client.get(['implementation', sel.id, 'data'],
                cmd='get_blob')
        enforce(impl and 'path' in impl, 'Cannot download implementation')
        impl_path = impl['path']

        dl = sel.download_sources[0]
        if dl.extract is not None:
            impl_path = join(impl_path, dl.extract)
        sel.local_path = impl_path

    pipe.progress('ready', session={'implementation': solution.top.id})

    return solution


def _activity_env(selection, environ):
    root = sugar.profile_path('data', selection.interface)

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

    # TODO Any way to avoid loading spec file?
    spec = Spec(root=selection.local_path)

    environ['SUGAR_BUNDLE_PATH'] = selection.local_path
    environ['SUGAR_BUNDLE_ID'] = selection.feed.context
    environ['SUGAR_BUNDLE_NAME'] = spec['Activity', 'name']
    environ['SUGAR_BUNDLE_VERSION'] = model.format_version(selection.version)
    environ['SUGAR_ACTIVITY_ROOT'] = root
    environ['PYTHONPATH'] = '%s:%s' % \
            (selection.local_path, environ['PYTHONPATH'])
    environ['SUGAR_LOCALEDIR'] = join(selection.local_path, 'locale')

    bin_path = join(selection.local_path, 'bin')
    if exists(bin_path):
        environ['PATH'] = bin_path + ':' + environ['PATH']
        # TODO Do it only once on unzip
        # Activities might call bin/* files but python zipfile module
        # doesn't set exec permissions while extracting
        for filename in os.listdir(bin_path):
            os.chmod(join(bin_path, filename), 0755)

    os.chdir(selection.local_path)