-
Notifications
You must be signed in to change notification settings - Fork 3
/
jobs.cgi
executable file
·213 lines (181 loc) · 7.63 KB
/
jobs.cgi
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
#!/usr/bin/env python3
#
# Copyright (c) 2019 Jon Turney
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
import cgi
import cgitb
import datetime
import sqlite3
import textwrap
from urllib.parse import urlencode
import carpetbag
dbfn = carpetbag.dbfile
rows_per_page = 25
conn = sqlite3.connect('file:%s?mode=ro' % dbfn, uri=True)
conn.row_factory = sqlite3.Row
def results(parse):
page = int(parse.get('page', 1))
highlight = int(parse.get('id', 0))
result = textwrap.dedent('''\
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="refresh" content="300">
<link rel="stylesheet" type="text/css" href="/style.css" />
<title>Cygwin package builds</title>
</head>
<body>
<table class="grid">''')
result += textwrap.dedent('''<tr><th>id</th>
<th>source package</th>
<th>status</th>
<th>by</th>
<th>commit</th>
<th>ref</th>
<th>logs</th>
<th>arch</th>
<th>when</th>
<th>duration</th></tr>''')
def options_list(column):
selected = parse.get(column, '')
sql = 'SELECT DISTINCT %s FROM jobs ORDER BY %s' % (column, column)
c = conn.execute(sql)
opts = [''] + [r[0] for r in c]
return ('<select name="%s" form="filter">' % (column) +
''.join(['<option%s>%s</option>' % (' selected' if o == selected else '', o) for o in opts]) +
'</select>')
result += textwrap.dedent('''<tr><td><form id="filter" method="get"><button>Filter</button></form></td>
<td>%s</td>
<td>%s</td>
<td>%s</td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td></tr>''' % (options_list('srcpkg'),
options_list('status'),
options_list('user')))
where_list = []
where_params = ()
for w in ['user', 'status', 'srcpkg']:
if w in parse:
where_list.append("%s = ?" % (w))
where_params = where_params + (parse[w],)
where_clause = ''
if where_list:
where_clause = 'WHERE ' + 'AND '.join(where_list)
sql = 'SELECT COUNT(*) FROM jobs %s' % (where_clause)
c = conn.execute(sql, where_params)
(rows,) = c.fetchone()
maxpages = int((rows + (rows_per_page - 1)) / rows_per_page)
if page < 1:
page = 1
if page > maxpages:
page = maxpages
sql = 'SELECT * FROM jobs %s' % where_clause + 'ORDER BY id DESC LIMIT ?,?'
c = conn.execute(sql, where_params + ((page - 1) * rows_per_page, rows_per_page))
for row in c:
jobid = row['id']
srcpkg = row['srcpkg']
commit = row['hash']
username = row['user']
status = row['status']
logurl = row['logurl']
timestamp = row['timestamp']
duration = row['duration']
arches = row['arches']
# artifacts = row['artifacts']
ref = row['ref']
commiturl = 'https://cygwin.com/cgit/cygwin-packages/%s/commit/?id=%s' % (srcpkg, commit)
shorthash = commit[0:8]
if jobid == highlight:
result += '<tr class="highlight">'
else:
result += '<tr>'
if srcpkg != 'playground':
srcpkglink = '<a href="https://cygwin.com/packages/summary/%s-src.html">%s</a>' % (srcpkg, srcpkg)
else:
srcpkglink = '%s' % (srcpkg)
def status_to_class(s):
if s.endswith('succeeded') or s == 'deployed':
return 'succeeded' # green
elif s.endswith('failed'):
return 'failed' # red
else:
return 'normal'
result += textwrap.dedent('''<td>%d</td>
<td>%s</td>
<td class="%s">%s</td>
<td>%s</td>
<td><a href="%s">%s</a></td>''') % (jobid, srcpkglink, status_to_class(status), status, username, commiturl, shorthash)
if ref:
ref = ref.replace('refs/heads/', '')
ref = ref.replace('refs/tags/', '')
result += '<td>%s</td>' % (ref)
else:
result += '<td></td>'
if logurl:
result += '<td><a href="%s">[log]</a></td>' % (logurl)
else:
result += '<td></td>'
if arches:
result += '<td>%s</td>' % (' '.join(a for a in arches.split() if a != 'source'))
else:
result += '<td></td>'
if timestamp:
result += '<td>%s</td>' % (datetime.datetime.fromtimestamp(timestamp).strftime('%Y-%m-%d %H:%M:%S'))
else:
result += '<td></td>'
if duration:
result += '<td>%s</td>' % (str(datetime.timedelta(seconds=int(duration))))
else:
result += '<td></td>'
result += '</tr>'
result += '</table>'
def query_string_modify_page(page):
parse['page'] = page
return urlencode(parse)
result += '<div class="gridfooter container">'
result += '<span class="floatleft">'
result += 'powered by <a href="https://cygwin.com/cgit/cygwin-apps/scallywag/">scallywag</a>, duct-tape and optimism'
result += '</span>'
result += '<span class="center">'
if page > 1:
result += '<a href="?%s">previous</a>' % query_string_modify_page(page - 1)
result += ' page %d of %d ' % (page, maxpages)
if page < maxpages:
result += '<a href="?%s">next</a>' % query_string_modify_page(page + 1)
result += '</span>'
result += '<span class="right"></span>'
result += '</div>'
result += textwrap.dedent('''</body>
</html>''')
return result
if __name__ == "__main__":
cgitb.enable()
print('Content-Type: text/html')
print()
parse = cgi.parse()
# if any query variable appears more than once, use the value of the last
# occurence.
parse = {k: v[-1] for k, v in parse.items()}
print(results(parse))