summaryrefslogtreecommitdiff
path: root/webapp.py
blob: 84e83fb77c70abcd6df55c6d4ab9b1b35661d9f1 (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
#!/usr/bin/python3

import datetime
import lzma

import apt_pkg
apt_pkg.init()
version_compare = apt_pkg.version_compare

import flask
import flask_sqlalchemy
import jinja2
import sqlalchemy
import werkzeug

app = flask.Flask("crossqa")
app.config["SQLALCHEMY_DATABASE_URI"] = 'sqlite:///db'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = flask_sqlalchemy.SQLAlchemy(app)

src_template = """<!DOCTYPE html>
<html>
 <head>
 </head>
 <body>
  <title>{{ sourcepackage|e }} - Debian cross build</title>
  <h1>{{ sourcepackage|e }}</h1>
  <h3>Cross satisfiability</h3>
  <table>
   <tr>
    <th>state</th>
    <th>architectures</th>
   </tr>
   {%- set okarchs = depresult.pop(None, None) -%}
   {%- for reason, archs in depresult.items()|sort -%}
    <tr>
     <td>{{ reason|e }}</td>
     <td>{{ archs|arch_format }}</td>
    </tr>
   {%- endfor -%}
   {%- if okarchs -%}
    <tr>
     <td>ok</td>
     <td>{{ okarchs|arch_format }}</td>
    </tr>
   {%- endif -%}
  </table>
  <h5>See also</h5>
  <ul>
   {%- if show_bootstrapdn -%}
    <li>
     <a href="https://bootstrap.debian.net/cross_all/{{ sourcepackage|e }}.html">bootstrap.debian.net</a>
    </li>
   {%- endif -%}
   <li>
    <a href="https://qa.debian.org/dose/debcheck/cross_unstable_main_amd64/">debcheck</a>
   </li>
  </ul>
  {%- if builds -%}
   <h3>Cross builds</h3>
   <table>
    <tr>
     <th>started</th>
     <th>version</th>
     <th>architecture</th>
     <th>result</th>
    </tr>
    {%- for build in builds|sort(attribute='starttime', reverse=true) -%}
     <tr>
      <td>
       <span title="{{ build.starttime|sqltimestamp }}">
        {{- build.starttime|sqltimestamp|formatts -}}
       </span>
      </td>
      <td>{{ build.version|e }}</td>
      <td>{{ build.architecture|e }}</td>
      <td>
       <a href="{{ url_for("show_log", filename=build.filename[:-3]) }}">
        {{- "ok" if build.success else "failed" -}}
       </a>
       <a href="{{ url_for("show_log", filename=build.filename) }}">xz</a>
      </td>
     </tr>
    {%- endfor -%}
   </table>
  {%- endif -%}
 </body>
</html>
"""

@app.template_filter("sqltimestamp")
def sqltimestamp_filter(s):
    try:
        return datetime.datetime.strptime(s, "%Y-%m-%d %H:%M:%S.%f")
    except ValueError:
        return datetime.datetime.strptime(s, "%Y-%m-%d %H:%M:%S")

@app.template_filter("formatts")
def formatts_filter(ts):
    assert isinstance(ts, datetime.datetime)
    dt = datetime.datetime.utcnow() - ts
    if dt < datetime.timedelta(seconds=1):
        return "now"
    if dt < datetime.timedelta(seconds=100):
        return "%d s" % dt.seconds
    if dt < datetime.timedelta(minutes=100):
        return "%d m" % (dt.seconds // 60)
    if dt < datetime.timedelta(days=1):
        return "%d h" % (dt.seconds // (60 * 60))
    return "%d d" % dt.days

@app.template_filter('arch_format')
@jinja2.contextfilter
def arch_format_filter(context, some_archs):
    if context["architectures"] == some_archs:
        return "any"
    return ", ".join(sorted(some_archs))

def collect_depstate(conn, source):
    version = None
    depstate = None
    query = sqlalchemy.text("""
        SELECT version, architecture, satisfiable, reason
            FROM depstate WHERE source = :source;""")
    for row in conn.execute(query, source=source):
        if version is None or version_compare(version, row.version) > 0:
            version = row.version
            depstate = {}
        depstate[row.architecture] = None if row.satisfiable else row.reason
    if version is None:
        raise werkzeug.exceptions.NotFound()
    depresult = {}
    for arch, reason in depstate.items():
        depresult.setdefault(reason, set()).add(arch)
    return version, depresult

@app.route("/src/<source>")
def show_source(source):
    context = dict(sourcepackage=source)
    with db.engine.connect() as conn:
        query = sqlalchemy.text("SELECT architecture FROM depcheck;")
        context["architectures"] = set(row[0] for row in conn.execute(query))
        context["version"], context["depresult"] = collect_depstate(conn,
                                                                    source)
        query = sqlalchemy.text("""
            SELECT version, architecture, success, starttime, filename
                FROM builds WHERE source = :source;""")
        context["builds"] = list(conn.execute(query, source=source))
        context["show_bootstrapdn"] = \
                any(reason and not reason.startswith("skew ")
                    for reason in context["depresult"].keys())
    return flask.render_template_string(src_template, **context)

@app.route("/build/<path:filename>")
def show_log(filename):
    if filename.endswith(".xz"):
        return flask.send_from_directory("logs", filename,
                                         mimetype="application/octet-stream")
    filename = flask.safe_join("logs", filename + ".xz")
    try:
        return flask.send_file(lzma.open(filename, "rb"),
                               mimetype="text/plain")
    except FileNotFoundError:
        raise werkzeug.exceptions.NotFound()