summaryrefslogtreecommitdiff
path: root/app.py
blob: a07760cc6f5b19fc46a69e7c7231af8be2de25dd (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
import datetime
import io
import json
import tarfile
import tempfile
from urllib.request import urlopen
import urllib.error

import flask
from flask import Flask, jsonify, redirect
from launchpadlib.launchpad import Launchpad

app = Flask(__name__)

tmpdir = tempfile.TemporaryDirectory()

launchpad = Launchpad.login_anonymously("just testing", "production", tmpdir.name, version="devel")

use_json = False

@app.route("/")
def index_root():
    return redirect("/ogayot", code=302)


@app.route("/<LPUSER>")
def index_lpuser(LPUSER):
    lpuser = launchpad.people[LPUSER]

    ppas = [ppa.name for ppa in lpuser.ppas]
    if use_json:
        return jsonify(ppas)
    return flask.render_template("list-ppas.html", ppas=ppas,
                                 base_url=flask.request.base_url)


@app.route("/<LPUSER>/<PPA>")
def index_ppa(LPUSER, PPA):
    lpuser = launchpad.people[LPUSER]

    ppa = next(filter(lambda p: p.name == PPA, lpuser.ppas))

    packages = list(set([pkg.source_package_name for pkg in ppa.getPublishedSources()]))
    if use_json:
        return jsonify(packages)
    return flask.render_template("list-packages.html", packages=packages,
                                 base_url=flask.request.base_url)


@app.route("/<LPUSER>/<PPA>/<PACKAGE>")
def index_package(LPUSER, PPA, PACKAGE):
    lpuser = launchpad.people[LPUSER]

    runs = []

    ppa = next(filter(lambda p: p.name == PPA, lpuser.ppas))
    series_set = set()
    for source_pkg in ppa.getPublishedSources():
        if source_pkg.source_package_name != PACKAGE:
            continue
        series_set.add(source_pkg.distro_series.name)

    series = list(series_set)
    if use_json:
        return jsonify(series)
    return flask.render_template("list-series.html", series=series,
                                 base_url=flask.request.base_url)


@app.route("/<LPUSER>/<PPA>/<PACKAGE>/<RELEASE>")
def index_release(LPUSER, PPA, PACKAGE, RELEASE):
    autopkgtest_url_base = f"https://autopkgtest.ubuntu.com/results/autopkgtest-{RELEASE}-{LPUSER}-{PPA}/"
    try:
        data = json.loads(urlopen(f"{autopkgtest_url_base}?format=json").read())
    except urllib.error.HTTPError:
        data = []

    arches_set = set()
    for section in data:
        tokens = section["name"].split("/")
        if tokens[3] != PACKAGE:
            continue
        arches_set.add(tokens[1])

    arches = list(arches_set)
    if use_json:
        return jsonify(arches)
    return flask.render_template("list-arches.html", arches=arches,
                                 base_url=flask.request.base_url)




def human_exitcode(code: int) -> str:
    if code in (0, 2):
        return 'pass'
    if code in (4, 6, 12):
        return 'fail'
    if code in (8,):
        return 'neutral'
    if code == 99:
        return 'denylisted'
    if code == 16:
        return 'tmpfail'
    if code == 20:
        return 'error'
    return 'exit code %i' % code


def human_sec(secs: int) -> str:
    return '%ih %02im %02is' % (secs // 3600, (secs % 3600) // 60, secs % 60)



@app.route("/<LPUSER>/<PPA>/<PACKAGE>/<RELEASE>/<ARCH>")
def index_results(LPUSER, PPA, PACKAGE, RELEASE, ARCH):

    autopkgtest_url_base = f"https://autopkgtest.ubuntu.com/results/autopkgtest-{RELEASE}-{LPUSER}-{PPA}/"
    data = json.loads(urlopen(f"{autopkgtest_url_base}?format=json").read())

    # Use the prefix as index
    runs = {}

    for info in data:
        index, suffix = info["name"].rsplit("/", maxsplit=1)
        if index.split("/")[3] != PACKAGE:
            continue
        if suffix != "result.tar":
            continue
        runs[index] = info

    headers = ("version", "triggers", "date", "duration", "requester", "result", "url_prefix", "show_retry")
    package_results = []
    for idx, info in reversed(runs.items()):
        date = " ".join(idx.split("/")[-1].split("_")[0:2])
        tar_stream = io.BytesIO(urlopen(f"{autopkgtest_url_base}/{info['name']}").read())
        file = tarfile.open(fileobj=tar_stream)
        exitcode = int(file.extractfile("exitcode").read().decode().splitlines()[0])
        duration = int(file.extractfile("duration").read().decode().splitlines()[0])
        version = file.extractfile("testpkg-version").read().decode().splitlines()[0].split(" ")[-1]
        requester = file.extractfile("requester").read().decode().splitlines()[0]
        testinfo = json.loads(file.extractfile("testinfo.json").read().decode())
        for entry in testinfo["custom_environment"]:
            key, value = entry.split("=", maxsplit=1)
            if key == "ADT_TEST_TRIGGERS":
                triggers = value
                break
        else:
            triggers = "?"

        result = {
            "version": version,
            "triggers": triggers,
            "date": datetime.datetime.strptime(date, "%Y%m%d %H%M%S").isoformat(),
            "duration": human_sec(duration),
            "requester": requester,
            "result": human_exitcode(exitcode),
            "url_prefix": f"{autopkgtest_url_base}/{idx}",
            "show_retry": human_exitcode(exitcode) != "pass",
        }
        package_results.append([result[h] for h in headers])


    return flask.render_template("browse-results.html",
                          package=PACKAGE,
                          release=RELEASE,
                          arch=ARCH,
                          package_results=package_results,
                          title_suffix=f"{LPUSER} {PPA} {PACKAGE}",
                          base_url="https://autopkgtest.ubuntu.com/",
                          static_url="/static",
                                 )

@app.route("/favicon.ico")
def index_favicon():
    return "", 404