summaryrefslogtreecommitdiffstats
path: root/bin/reproducible_html_packages.py
blob: c7d081dd3d351e8b9ca4c6d044bf608bfdc34d73 (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
#!/usr/bin/python3
# -*- coding: utf-8 -*-
#
# Copyright © 2015 Mattia Rizzolo <mattia@mapreri.org>
# Licensed under GPL-2+
#
# Depends: python3 python3-psycopg2
#
# Build rb-pkg pages (the pages that describe the package status)

import psycopg2

from reproducible_common import *

html_package_page = Template((tab*2).join(("""
<table class="head">
    <tr>
        <td>
            <span style="font-size:1.2em;">$package</span> $version
            <a href="/index_$status.html" target="_parent" title="$status">
                <img src="/static/$icon" alt="$status" />
            </a>
            <span style="font-size:0.9em;">at $build_time:</span>
$links
            <a href="https://packages.qa.debian.org/$package" target="main">PTS</a>
            <a href="https://bugs.debian.org/src:$package" target="main">BTS</a>
            <a href="https://sources.debian.net/src/$package/" target="main">sources</a>
            <a href="https://sources.debian.net/src/$package/$version/debian/rules" target="main">debian/rules</a>
        </td>
        <td>
${bugs_links}
        </td>
        <td style="text-align:right; font-size:0.9em;">
            <a href="%s" target="_parent">
                reproducible builds
            </a>
        </td>
    </tr>
</table>
<iframe id="main" name="main" src="${default_view}">
    <p>
        Your browser does not support iframes.
        Use a different one or follow the links above.
    </p>
</iframe>""" % REPRODUCIBLE_URL ).splitlines(True)))


def sizeof_fmt(num):
    for unit in ['B','KB','MB','GB']:
        if abs(num) < 1024.0:
            if unit == 'GB':
                log.error('The size of this file is bigger than 1 GB!')
                log.error('Please check')
            return str(int(round(float("%3f" % num), 0))) + "%s" % (unit)
        num /= 1024.0
    return str(int(round(float("%f" % num), 0))) + "%s" % ('Yi')

def start_udd_connection():
    username = "public-udd-mirror"
    password = "public-udd-mirror"
    host = "public-udd-mirror.xvm.mit.edu"
    port = 5432
    db = "udd"
    try:
        log.debug("Starting connection to the UDD database")
        conn = psycopg2.connect("dbname=" + db +
                               " user=" + username +
                               " host=" + host +
                               " password=" + password)
    except:
        log.error("Erorr connecting to the UDD database replica")
        raise
    conn.set_client_encoding('utf8')
    return conn

def query_udd(query):
    cursor = conn.cursor()
    cursor.execute(query)
    return cursor.fetchall()

def is_virtual_package(package):
    rows = query_udd("""SELECT source FROM sources WHERE source='%s'""" % package)
    if len(rows) > 0:
            return False
    return True

def bug_has_patch(bug):
    query = """SELECT id FROM bugs_tags WHERE id=%s AND tag='patch'""" % bug
    if len(query_udd(query)) > 0:
        return True
    return False

def get_bugs():
    query = """
        SELECT bugs.id, bugs.source, bugs.done
        FROM bugs JOIN bugs_tags on bugs.id = bugs_tags.id
                  JOIN bugs_usertags on bugs_tags.id = bugs_usertags.id
        WHERE bugs_usertags.email = 'reproducible-builds@lists.alioth.debian.org'
        AND bugs.id NOT IN (
            SELECT id
            FROM bugs_usertags
            WHERE email = 'reproducible-builds@lists.alioth.debian.org'
            AND (
                bugs_usertags.tag = 'toolchain'
                OR bugs_usertags.tag = 'infrastructure')
            )
    """
    # returns a list of tuples [(id, source, done)]
    return query_udd(query)

def check_package_status(package):
    """
    This returns a tuple containing status, version and build_date of the last
    version of the package built by jenkins CI
    """
    try:
        query = 'SELECT status,version,build_date ' + \
                'FROM source_packages ' + \
                'WHERE name="%s"' % package
        result = query_db(query)[0]
    except IndexError:
        log.critical('The query produces no results. The query: ' + query)
        raise
    status = str(result[0])
    version = str(result[1])
    build_date = str(result[2])
    return (status, version, build_date)

def gen_extra_links(package, version):
    notes = NOTES_PATH + '/' + package + '_note.html'
    rbuild = RBUILD_PATH + '/' + package + '_' + strip_epoch(version) + '.rbuild.log'
    buildinfo = BUILDINFO_PATH + '/' + package + '_' + strip_epoch(version) + '_amd64.buildinfo'
    dbd = DBD_PATH + '/' + package + '_' + strip_epoch(version) + '.debbindiff.html'

    links = ''
    default_view = False
    # check whether there are notes available for this package
    if os.access(notes, os.R_OK):
        url = NOTES_URI + '/' + package + '_note.html'
        links += '<a href="' + url + '" target="main">notes</a>\n'
        default_view = url
    else:
        log.debug('notes not detected at ' + notes)
    if os.access(dbd, os.R_OK):
        url = DBD_URI + '/' + package + '_' + strip_epoch(version) + '.debbindiff.html'
        links += '<a href="' + url + '" target="main">debbindiff</a>\n'
        if not default_view:
            default_view = url
    else:
        log.debug('debbindiff not detetected at ' + dbd)
    if pkg_has_buildinfo(package, version):
        url = BUILDINFO_URI + '/' + package + '_' + strip_epoch(version) + '_amd64.buildinfo'
        links += '<a href="' + url + '" target="main">buildinfo</a>\n'
        if not default_view:
            default_view = url
    else:
        log.debug('buildinfo not detected at ' + buildinfo)
    if os.access(rbuild, os.R_OK):
        url = RBUILD_URI + '/' + package + '_' + strip_epoch(version) + '.rbuild.log'
        log_size = os.stat(rbuild).st_size
        links +='<a href="' + url + '" target="main">rbuild (' + \
                sizeof_fmt(log_size) + ')</a>\n'
        if not default_view:
            default_view = url
    else:
        log.warning('The package ' + package +
                    ' did not produce any buildlog! Check ' + rbuild)
    return (links, default_view)

def parse_bugs(bugs):
    """
    This function returns a dict:
    { "package_name": {
        bug1: {patch: True, done: False},
        bug2: {patch: False, done: False},
       }
    }

    The `bugs` argument is the list of tuples returned by the get_bugs() above
    """
    log.info("finding out which usertagged bugs have been closed or at least have patches")
    packages = {}

    for bug in bugs:
        if bug[1] not in packages:
            packages[bug[1]] = {}
        # bug[0] = bug_id, bug[1] = source_name, bug[2] = who_when_done
        if is_virtual_package(bug[1]):
            continue
        packages[bug[1]][bug[0]] = {'done': False, 'patch': False}
        if bug[2]: # if the bug is done
            packages[bug[1]][bug[0]]['done'] = True
        try:
            if bug_has_patch(bug[0]):
                packages[bug[1]][bug[0]]['patch'] = True
        except KeyError:
            log.error('item: ' + str(bug))
    return packages



def gen_bugs_links(package, bugs):
    html = ''
    if package in bugs:
        for bug in bugs[package]:
            html += '<a href="https://bugs.debian.org/' + str(bug) + \
                    '" target="main" class="'
            if bugs[package][bug]['done']:
                html += 'bug-done '
            if bugs[package][bug]['patch']:
                html += ' bug-patch'
            html += '">#' + str(bug) + '</a> '
            print(package + html)
        return html


def process_packages(packages, no_clean=False):
    """
    generate the /rb-pkg/package.html page
    packages should be a list
    """
    bugs = parse_bugs(get_bugs())
    total = len(packages)
    log.info('Generating the pages of ' + str(total) + ' package(s)')
    for pkg in sorted(packages):
        pkg = str(pkg)
        status, version, build_date = check_package_status(pkg)
        log.info('Generating the page of ' + pkg + ' ' + version +
                 ' builded at ' + build_date)

        links, default_view = gen_extra_links(pkg, version)
        bugs_links = gen_bugs_links(pkg, bugs)
        status, icon = join_status_icon(status, pkg, version)

        html = html_package_page.substitute(package=pkg,
                                            status=status,
                                            version=version,
                                            build_time=build_date,
                                            icon=icon,
                                            links=links,
                                            bugs_links=bugs_links,
                                            default_view=default_view)
        destfile = RB_PKG_PATH + '/' + pkg + '.html'
        desturl = REPRODUCIBLE_URL + RB_PKG_URI + '/' + pkg + '.html'
        title = pkg + ' - reproducible build results'
        write_html_page(title=title, body=html, destfile=destfile,
                        noheader=True, noendpage=True)
        log.info("Package page generated at " + desturl)
    if not no_clean:
        purge_old_pages() # housekeep is always good


def purge_old_pages():
    presents = sorted(os.listdir(RB_PKG_PATH))
    for page in presents:
        pkg = page.rsplit('.', 1)[0]
        log.debug('Checking if ' + page + ' (from ' + pkg + ') is still needed')
        query = 'SELECT name FROM source_packages WHERE name="%s"' % pkg
        result = query_db(query)
        if not result: # actually, the query produces no results
            log.info('There is no package named ' + pkg + ' in the database.' +
                     ' Removing old page.')
            os.remove(RB_PKG_PATH + '/' + page)


try:
    conn = start_udd_connection()
except:
    raise