Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/sugar_network/node/obs.py
blob: 796ea7c06ae5966af516a0f57e26e4e0083e669c (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
# Copyright (C) 2012-2014 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 logging
from xml.etree import cElementTree as ElementTree
from os.path import join, exists, basename

from sugar_network import toolkit
from sugar_network.toolkit import Option, http, enforce


obs_url = Option(
        'OBS API url; the entire OBS related functionality makes sense only '
        'for master server',
        default='https://obs.sugarlabs.org')

obs_project = Option(
        'OBS project to use unattended building',
        default='base')

obs_presolve_project = Option(
        'OBS project to use with packagekit-backend-presolve',
        default='presolve')

_logger = logging.getLogger('node.obs')
_conn = None
_repos = {}


def get_repos():
    return _get_repos(obs_project.value)


def resolve(repo, arch, packages):
    response = _request('GET', ['resolve'], params={
        'project': obs_project.value,
        'repository': repo,
        'arch': arch,
        'package': packages,
        })
    return dict(response.find('binary').items())


def presolve(repo_name, packages, dst_path):
    for repo in _get_repos(obs_presolve_project.value):
        dst_dir = join(dst_path, 'packages',
                obs_presolve_project.value, repo['name'])
        result = {}
        to_download = []

        for package in packages:
            files = result.setdefault(package, {})
            try:
                for repo_arch in repo['arches']:
                    response = _request('GET', ['resolve'], params={
                        'project': obs_presolve_project.value,
                        'repository': '%(lsb_id)s-%(lsb_release)s' % repo,
                        'arch': repo_arch,
                        'package': package,
                        'withdeps': '1',
                        'exclude': 'sweets-sugar',
                        })
                    for binary in response.findall('binary'):
                        binary = dict(binary.items())
                        arch = binary.pop('arch')
                        url = binary.pop('url')
                        filename = binary['path'] = basename(url)
                        path = join(dst_dir, filename)
                        if not exists(path):
                            to_download.append((url, path))
                        files.setdefault(arch, []).append(binary)
            except Exception:
                toolkit.exception(_logger, 'Failed to presolve %r on %s',
                        packages, repo['name'])
                continue

        _logger.debug('Presolve %r on %s', packages, repo['name'])

        if not exists(dst_dir):
            os.makedirs(dst_dir)
        for url, path in to_download:
            _conn.download(url, path)
        for package, info in result.items():
            with toolkit.new_file(join(dst_dir, package)) as f:
                json.dump(info, f)

        return {'repo': repo['name'], 'packages': result}


def _request(*args, **kwargs):
    global _conn

    if _conn is None:
        _conn = http.Connection(obs_url.value)

    response = _conn.request(*args, allowed=(400, 404), **kwargs)
    enforce(response.headers.get('Content-Type') == 'text/xml',
            'Irregular OBS response')
    reply = ElementTree.fromstring(response.content)

    if response.status_code != 200:
        summary = reply.find('summary')
        enforce(summary is not None, 'Unknown OBS error')
        raise RuntimeError(summary.text)

    return reply


def _get_repos(project):
    if project in _repos:
        return _repos[project]

    if not obs_url.value:
        return []

    repos = _repos[project] = []
    for repo in _request('GET', ['build', project]).findall('entry'):
        repo = repo.get('name')
        arches = _request('GET', ['build', project, repo])
        lsb_id, lsb_release = repo.split('-', 1)
        repos.append({
            'lsb_id': lsb_id,
            'lsb_release': lsb_release,
            'name': repo,
            'arches': [i.get('name') for i in arches.findall('entry')],
            })

    return repos