Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/sugar_network/client/solver.py
blob: 584d0c570a85c08134aea77377db031e5ea14db5 (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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
# 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/>.

# pylint: disable-msg=W0611,F0401,W0201,E1101,W0232

import sys
import logging
from os.path import isabs, join, dirname

from sugar_network.client import packagekit, SUGAR_API_COMPATIBILITY
from sugar_network.toolkit.spec import parse_version
from sugar_network.toolkit import http, lsb_release, pipe, exception

sys.path.insert(0, join(dirname(__file__), '..', 'lib', 'zeroinstall'))

from zeroinstall.injector import reader, model, arch as _arch
from zeroinstall.injector.config import Config
from zeroinstall.injector.driver import Driver
from zeroinstall.injector.requirements import Requirements
from zeroinstall.injector.arch import machine_ranks
from zeroinstall.injector.distro import try_cleanup_distro_version


model.Interface.__init__ = lambda *args: _interface_init(*args)
reader.check_readable = lambda *args, **kwargs: True
reader.update_from_cache = lambda *args, **kwargs: None
reader.load_feed_from_cache = lambda url, **kwargs: _load_feed(url)

_logger = logging.getLogger('zeroinstall')
_stability = None
_conn = None


def canonicalize_machine(arch):
    result = _arch.canonicalize_machine(arch)
    return None if arch in ('noarch', 'all') else result


def select_architecture(arches):
    """Select most appropriate, for the host system, machine architecture

    :param arches:
        list of architecture names to select
    :returns:
        one of passed architecture names, or, `None` if not any

    """
    result_rank = 9999
    result_arch = None
    for arch in arches:
        rank = machine_ranks.get(canonicalize_machine(arch))
        if rank is not None and rank < result_rank:
            result_rank = rank
            result_arch = arch
    return result_arch


def solve(conn, context, stability):
    global _conn, _stability

    _conn = conn
    _stability = stability

    req = Requirements(context)
    # TODO
    req.command = 'activity'
    config = Config()
    driver = Driver(config, req)
    solver = driver.solver
    solver.record_details = True
    status = None
    ready = False

    while True:
        solver.solve(context, driver.target_arch, command_name=req.command)
        if ready and solver.ready:
            break
        ready = solver.ready

        resolved = None
        for url in solver.feeds_used:
            feed = config.iface_cache.get_feed(url)
            if feed is None:
                continue
            while feed.to_resolve:
                try:
                    resolved = packagekit.resolve(feed.to_resolve.pop(0))
                except Exception, error:
                    if feed.to_resolve:
                        continue
                    if status is None:
                        status = conn.get(cmd='status')
                    if status['route'] == 'offline':
                        raise http.ServiceUnavailable(str(error))
                    else:
                        raise
                feed.resolve(resolved.values())
                feed.to_resolve = None
        if not resolved:
            break

    selections = solver.selections.selections
    missed = []

    top_summary = []
    dep_summary = []
    for iface, impls in solver.details.items():
        summary = (top_summary if iface.uri == context else dep_summary)
        summary.append(iface.uri)
        if impls:
            sel = selections.get(iface.uri)
            for impl, reason in impls:
                if not reason and sel is None:
                    reason = 'wrong version'
                    missed.append(iface.uri)
                if reason:
                    reason = '(%s)' % reason
                summary.append('%s v%s %s' % (
                    '*' if sel is not None and sel.impl is impl else ' ',
                    impl.get_version(),
                    reason or '',
                    ))
        else:
            summary.append('  (no versions)')
            missed.append(iface.uri)
    pipe.trace('\n  '.join(['Solving results:'] + top_summary + dep_summary))

    if not ready:
        # pylint: disable-msg=W0212
        reason_exception = solver.get_failure_reason()
        if reason_exception is not None:
            reason = reason_exception.message
        else:
            reason = 'Cannot find implementations for %s' % ', '.join(missed)
        raise RuntimeError(reason)

    solution = []
    solution.append(_impl_new(config, context, selections[context]))
    for iface, sel in selections.items():
        if sel is not None and iface != context:
            solution.append(_impl_new(config, iface, sel))

    return solution


def _interface_init(self, url):
    self.uri = url
    self.reset()


def _impl_new(config, iface, sel):
    feed = config.iface_cache.get_feed(iface)
    impl = {'id': sel.id,
            'context': iface,
            'version': sel.version,
            'name': feed.title,
            'stability': sel.impl.upstream_stability.name,
            }
    if sel.impl.hints:
        for key in ('mime_type', 'blob_size', 'unpack_size'):
            value = sel.impl.hints.get(key)
            if value is not None:
                impl[key] = value

    if isabs(sel.id):
        impl['spec'] = join(sel.id, 'activity', 'activity.info')
    if sel.local_path:
        impl['path'] = sel.local_path
    if sel.impl.to_install:
        impl['install'] = sel.impl.to_install
    if sel.impl.download_sources:
        prefix = sel.impl.download_sources[0].extract
        if prefix:
            impl['prefix'] = prefix
    commands = sel.get_commands()
    if commands:
        impl['command'] = commands.values()[0].path.split()
    return impl


def _load_feed(context):
    feed = _Feed(context)

    if context == 'sugar':
        try:
            # pylint: disable-msg=F0401
            from jarabe import config
            host_versin = '.'.join(config.version.split('.', 2)[:2])
            for version in SUGAR_API_COMPATIBILITY.get(host_versin) or []:
                feed.implement_sugar(version)
            feed.name = feed.title = context
            return feed
        except ImportError:
            pass

    feed_content = None
    try:
        feed_content = _conn.get(['context', context], cmd='feed',
                stability=_stability, distro=lsb_release.distributor_id())
        pipe.trace('Found %s feed: %r', context, feed_content)
    except http.ServiceUnavailable:
        pipe.trace('Failed to fetch %s feed', context)
        raise
    except Exception:
        exception(_logger, 'Failed to fetch %r feed', context)
        pipe.trace('No feeds for %s', context)
        return None

    # XXX 0install fails on non-ascii `name` values
    feed.name = context
    feed.title = feed_content['name']
    feed.to_resolve = feed_content.get('packages')
    if not feed.to_resolve:
        pipe.trace('No compatible packages for %s', context)
    for impl in feed_content['implementations']:
        feed.implement(impl)
    if not feed.to_resolve and not feed.implementations:
        pipe.trace('No implementations for %s', context)

    return feed


class _Feed(model.ZeroInstallFeed):
    # pylint: disable-msg=E0202

    def __init__(self, context):
        self.context = context
        self.local_path = None
        self.implementations = {}
        self.last_modified = None
        self.feeds = []
        self.metadata = []
        self.last_checked = None
        self.to_resolve = None
        self._package_implementations = []
        self.title = None

    @property
    def url(self):
        return self.context

    @property
    def feed_for(self):
        return set([self.context])

    def resolve(self, packages):
        top_package = packages[0]

        impl = _Implementation(self, self.context, None)
        impl.version = parse_version(top_package['version'])
        impl.released = 0
        impl.arch = '*-%s' % top_package['arch']
        impl.upstream_stability = model.stability_levels['packaged']
        impl.to_install = [i for i in packages if not i['installed']]
        impl.add_download_source(self.context, 0, None)

        self.implementations[self.context] = impl

    def implement(self, release):
        impl_id = release['guid']

        impl = _Implementation(self, impl_id, None)
        impl.version = parse_version(release['version'])
        impl.released = 0
        impl.arch = release['arch']
        impl.upstream_stability = model.stability_levels['stable']
        impl.requires.extend(_read_requires(release.get('requires')))
        impl.hints = release

        if isabs(impl_id):
            impl.local_path = impl_id
        else:
            impl.add_download_source(impl_id,
                    release.get('size') or 0, release.get('extract'))

        for name, command in release['commands'].items():
            impl.commands[name] = _Command(name, command)

        for name, insert, mode in release.get('bindings') or []:
            binding = model.EnvironmentBinding(name, insert, mode=mode)
            impl.bindings.append(binding)

        self.implementations[impl_id] = impl

    def implement_sugar(self, sugar_version):
        impl_id = 'sugar-%s' % sugar_version
        impl = _Implementation(self, impl_id, None)
        impl.version = parse_version(sugar_version)
        impl.released = 0
        impl.arch = '*-*'
        impl.upstream_stability = model.stability_levels['packaged']
        impl.local_path = '/'
        self.implementations[impl_id] = impl


class _Implementation(model.ZeroInstallImplementation):

    to_install = None
    hints = None


class _Dependency(model.InterfaceDependency):

    def __init__(self, guid, data):
        self._importance = data.get('importance', model.Dependency.Essential)
        self._metadata = {}
        self.qdom = None
        self.interface = guid
        self.restrictions = []
        self.bindings = []

        for not_before, before in data.get('restrictions') or []:
            restriction = model.VersionRangeRestriction(
                    not_before=parse_version(not_before),
                    before=parse_version(before))
            self.restrictions.append(restriction)

    @property
    def context(self):
        return self.interface

    @property
    def metadata(self):
        return self._metadata

    @property
    def importance(self):
        return self._importance

    def get_required_commands(self):
        return []

    @property
    def command(self):
        pass


class _Command(model.Command):

    def __init__(self, name, data):
        self.qdom = None
        self.name = name
        self._path = data['exec']
        self._requires = _read_requires(data.get('requires'))

    @property
    def path(self):
        return self._path

    @property
    def requires(self):
        return self._requires

    def get_runner(self):
        pass

    def __str__(self):
        return ''

    @property
    def bindings(self):
        return []


def _read_requires(data):
    result = []
    for guid, dep_data in (data or {}).items():
        result.append(_Dependency(guid, dep_data))
    return result


if __name__ == '__main__':
    from pprint import pprint
    logging.basicConfig(level=logging.DEBUG)
    pipe.trace = logging.info
    pprint(solve(*sys.argv[1:]))