aboutsummaryrefslogtreecommitdiffstats
path: root/sb_builder/jobs.py
blob: a64bc581c6247dd0b3681f7b5e7a8b99494f0c4b (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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
# sb-builder - Jobs tab
# Copyright (C) 2026 Danilo M. <danix@danix.xyz>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
#
# 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, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

"""Jobs tab: job list, live log through job-follow, start and kill; the next-step dialog."""

import codecs
import os
import re

from PyQt6.QtCore import Qt, pyqtSignal
from PyQt6.QtGui import QColor, QFontDatabase, QTextCursor
from PyQt6.QtWidgets import (QCheckBox, QDialog, QHBoxLayout, QInputDialog, QLineEdit,
                             QListWidget, QListWidgetItem, QMenu, QMessageBox, QPlainTextEdit,
                             QLabel, QPushButton, QSplitter, QVBoxLayout, QWidget)

from .dashboard import STATUS
from .fmt import describe, failed_items, job_title
from .results import LogViewer
from .ui import SP, action, enable

ANSI = re.compile(rb"\x1b\[[0-9;?]*[A-Za-z]")
SHADOW_PLAN = re.compile(r"^(un)?shadow: ", re.M)
ACTIVE = ("starting", "running")
BAD = ("failed", "killed", "lost")


def is_preview(j):
    """A finished preview whose next step is a real run: slackrepo --preview, sbpull/sbshadow --no."""
    return j["status"] == "ok" and (
        (j["kind"] == "slackrepo" and "--preview" in j["args"])
        or (j["kind"] in ("sbpull", "sbshadow") and "--no" in j["args"]))


class NextStep(QDialog):
    """After a preview: its whole log, then Run it (or Apply shadowing) / Not now.

    run(job-start args) asks for the real run; the caller starts it. For a
    shadowing preview, loaded(has_plan) tells whether there is anything to apply.
    """

    run = pyqtSignal(list)
    loaded = pyqtSignal(bool)

    def __init__(self, remote, job, publish, parent=None):
        super().__init__(parent)
        self.job = job
        self.shadow = job["kind"] != "slackrepo"
        self.setWindowTitle("Preview: " + job_title(dict(job, args=[
            a for a in job["args"] if a not in ("--preview", "--no")])))
        self.resize(900, 600)
        self.decoder = codecs.getincrementaldecoder("utf-8")(errors="replace")
        self.log = QPlainTextEdit(readOnly=True)
        self.log.setFont(QFontDatabase.systemFont(QFontDatabase.SystemFont.FixedFont))
        self.publish = QCheckBox("publish", checked=publish, visible=not self.shadow)
        self.publish.setToolTip("Push to the mirror when the build ends; unticked, use Publish later")
        self.run_btn = QPushButton("Apply shadowing" if self.shadow else "Run it", default=True)
        self.run_btn.clicked.connect(self._run)
        later = QPushButton("Not now")
        later.clicked.connect(self.reject)
        bar = QHBoxLayout()
        bar.addWidget(self.publish)
        bar.addStretch()
        bar.addWidget(later)
        bar.addWidget(self.run_btn)
        layout = QVBoxLayout(self)
        layout.addWidget(self.log)
        layout.addLayout(bar)
        self.proc = p = remote.follow(job["id"], 0)  # the job has ended: the stream closes by itself
        p.readyReadStandardOutput.connect(self._chunk)
        p.finished.connect(self._ended)

    def _chunk(self):
        data = bytes(self.proc.readAllStandardOutput())
        self.log.appendPlainText(self.decoder.decode(ANSI.sub(b"", data)).rstrip("\n"))

    def _ended(self, *_):
        self._chunk()
        self.proc.deleteLater()
        if not self.has_plan():
            self.run_btn.setEnabled(False)
            self.run_btn.setToolTip("no shadow: or unshadow: lines, nothing to apply")
        self.loaded.emit(self.has_plan())

    def has_plan(self):
        return not self.shadow or bool(SHADOW_PLAN.search(self.log.toPlainText()))

    def confirm(self, question):
        return QMessageBox.question(self, "Confirm", question) == QMessageBox.StandardButton.Yes

    def _run(self):
        if self.shadow:
            if not self.confirm("Apply the shadowing plan to the repo (sbshadow --yes)?"):
                return
            self.run.emit(["sbshadow", "--yes"])
        else:
            args = [a for a in self.job["args"] if a != "--preview"]
            if args[0] in ("remove", "prune") and not self.confirm(
                    "Remove the packages listed in the preview from the repository?"):
                return
            if not self.publish.isChecked():
                args.insert(1, "--no-publish")
            self.run.emit(["slackrepo", *args])
        self.accept()


class BuildResultDialog(QDialog):
    """After a slackrepo run that failed items: each item's result, its build log, Retry failed."""

    retry = pyqtSignal(list)  # package names

    def __init__(self, remote, job, parent=None):
        super().__init__(parent)
        self.remote = remote
        self.viewers = []
        self.setWindowTitle("Build results: " + job_title(job))
        self.resize(600, 400)
        self.list = QListWidget()
        for item, result in sorted(job["results"].items(), key=lambda r: (r[1] == "ok", r[0])):
            row = QListWidgetItem(f"{item}   {result}")
            row.setData(Qt.ItemDataRole.UserRole, item)
            if result in STATUS:
                row.setForeground(QColor(STATUS[result]))
            self.list.addItem(row)
        self.list.itemDoubleClicked.connect(self.show_log)
        self.status = QLabel()
        # bad items (no such package) need a fix first: a retry would fail the same way
        self.again = sorted({os.path.basename(i) for i, r in job["results"].items()
                             if r in ("failed", "aborted")})
        log = QPushButton("Log")
        log.clicked.connect(lambda: self.show_log(self.list.currentItem()))
        self.retry_btn = QPushButton("Retry failed", enabled=bool(self.again))
        self.retry_btn.setToolTip(" ".join(self.again) or "nothing failed or aborted")
        self.retry_btn.clicked.connect(self._retry)
        close = QPushButton("Close")
        close.clicked.connect(self.accept)
        bar = QHBoxLayout()
        bar.addWidget(log)
        bar.addWidget(self.status, 1)
        bar.addWidget(self.retry_btn)
        bar.addWidget(close)
        layout = QVBoxLayout(self)
        layout.addWidget(self.list)
        layout.addLayout(bar)
        self.list.setCurrentRow(0)

    def show_log(self, row):
        if not row:
            return
        item = row.data(Qt.ItemDataRole.UserRole)

        def got(log, err):
            if err:
                self.status.setText(f"build log: {err}")
                return
            viewer = LogViewer(item, log, self)
            viewer.finished.connect(lambda *_: self.viewers.remove(viewer))
            self.viewers.append(viewer)
            viewer.show()
        self.remote.call(got, "build-log", item)

    def _retry(self):
        self.retry.emit(self.again)
        self.accept()


class JobsTab(QWidget):
    refresh_requested = pyqtSignal()
    started = pyqtSignal(str)         # id of a job this GUI started
    preview_requested = pyqtSignal(dict)  # a finished preview to take to its next step
    results_requested = pyqtSignal(dict)  # a slackrepo run that failed items, to show them

    def __init__(self, remote, parent=None):
        super().__init__(parent)
        self.remote = remote
        self.pending = ()
        self.jobs = {}
        self.current = None
        self.offset = 0
        self.proc = None
        self.decoder = codecs.getincrementaldecoder("utf-8")(errors="replace")

        self.pull_menu = QMenu(self)
        self.act_pull = action(self, "Pull overlay", "folder-download", SP.SP_ArrowDown)
        self.act_pull.setMenu(self.pull_menu)
        self.act_run = action(self, "Run it", "media-playback-start", SP.SP_MediaPlay,
                              lambda: self.preview_requested.emit(self.jobs[self.current]))
        self.act_results = action(self, "Build results", "dialog-information",
                                  SP.SP_FileDialogDetailedView,
                                  lambda: self.results_requested.emit(self.jobs[self.current]))
        self.act_kill = action(self, "Kill", "process-stop", SP.SP_BrowserStop, self.kill)
        self.actions = [self.act_pull,
                        action(self, "Check shadowing", "edit-find", SP.SP_FileDialogContentsView,
                               lambda: self.start("sbshadow", "--no")),
                        action(self, "Rebuild tree", "edit-clear-all", SP.SP_DialogResetButton,
                               self.rebuild_tree),
                        self.act_run, self.act_results, self.act_kill]
        self.view_actions = []

        self.list = QListWidget()
        self.list.currentItemChanged.connect(self._on_select)
        self.log = QPlainTextEdit(readOnly=True)
        self.log.setFont(QFontDatabase.systemFont(QFontDatabase.SystemFont.FixedFont))
        self.log.setMaximumBlockCount(50000)  # ponytail: older lines drop off; the full log stays on the VM
        split = QSplitter()
        split.addWidget(self.list)
        split.addWidget(self.log)
        split.setSizes([300, 700])
        layout = QVBoxLayout(self)
        layout.addWidget(split)
        self.set_overlays([])

    def set_overlays(self, overlays):
        self.pull_menu.clear()
        for o in overlays:
            self.pull_menu.addAction(f"{o} (preview)", lambda o=o: self.start("sbpull", o, "--no"))
        enable(self.act_pull, None if overlays else "no overlays known yet")

    def set_jobs(self, jobs):
        self.jobs = {j["id"]: j for j in jobs}
        self.list.blockSignals(True)
        self.list.clear()
        for j in jobs:
            item = QListWidgetItem(describe(j))
            item.setData(Qt.ItemDataRole.UserRole, j["id"])
            if j["status"] in BAD:
                item.setForeground(Qt.GlobalColor.red)
            self.list.addItem(item)
            if j["id"] == self.current:
                self.list.setCurrentItem(item)
        self.list.blockSignals(False)
        j = self.jobs.get(self.current)
        if j and j["status"] in ACTIVE and self.proc is None:
            self._follow()  # stream dropped while the job kept running: resume at offset
        self._update_buttons()

    def select(self, jid):
        for i in range(self.list.count()):
            if self.list.item(i).data(Qt.ItemDataRole.UserRole) == jid:
                self.list.setCurrentItem(self.list.item(i))
                return
        self._show(jid)  # not listed yet: just started

    def append_log(self, text):
        cur = self.log.textCursor()
        cur.movePosition(QTextCursor.MoveOperation.End)
        # ponytail: a \r split across two chunks is printed as-is
        for i, part in enumerate(text.replace("\r\n", "\n").split("\r")):
            if i:  # carriage return: the next text replaces the current line
                cur.movePosition(QTextCursor.MoveOperation.StartOfBlock,
                                 QTextCursor.MoveMode.KeepAnchor)
                cur.removeSelectedText()
            cur.insertText(part)
        self.log.setTextCursor(cur)

    def stop(self):
        if self.proc:
            p, self.proc = self.proc, None
            # disconnect first: finished arrives later, maybe after this tab is deleted
            p.readyReadStandardOutput.disconnect()
            p.finished.disconnect()
            p.kill()
            p.deleteLater()

    def start(self, *args):
        self.pending = args
        self.remote.call(self._started, "job-start", *args)

    def confirm(self, question):
        return QMessageBox.question(self, "Confirm", question) == QMessageBox.StandardButton.Yes

    def rebuild_tree(self):
        if self.confirm("Replace SBo-danix with a fresh clone of upstream and the overlays, "
                        "then reapply shadowing?"):
            self.start("rebuild-tree")

    def kill(self):
        if QMessageBox.question(self, "Kill job", f"Send SIGTERM to {self.current}?") \
                == QMessageBox.StandardButton.Yes:
            self.remote.call(lambda _d, err: err and QMessageBox.warning(self, "Kill failed", err),
                             "job-kill", self.current)

    def ask_passphrase(self, retry):
        text, ok = QInputDialog.getText(
            self, "Signing key", ("Wrong passphrase, try again." if retry else
                                  "Passphrase of the package signing key:"),
            QLineEdit.EchoMode.Password)
        return text if ok else None

    def start_signed(self, *args, retry=False):
        """Unlock the signing key first when it is locked, then start the job."""
        def status(data, err):
            if err:
                QMessageBox.warning(self, "Signing key", err)
            elif data["unlocked"] is False:
                self._unlock(args, retry)
            else:
                self.start(*args)
        self.remote.call(status, "gpg-status")

    def _unlock(self, args, retry):
        passphrase = self.ask_passphrase(retry)
        if passphrase is None:
            return

        def unlocked(_data, err):
            if err == "bad passphrase":
                self._unlock(args, True)
            elif err:
                QMessageBox.warning(self, "Signing key", err)
            else:
                self.start(*args)
        self.remote.call(unlocked, "gpg-unlock", stdin=passphrase.encode() + b"\n")

    def _started(self, data, err):
        if err == "signing key locked":
            self.start_signed(*self.pending)  # locked since the last check: ask again
            return
        if err:
            QMessageBox.warning(self, "Start failed", err)
            return
        if "busy" in data:
            box = QMessageBox(QMessageBox.Icon.Information, "Busy",
                              f"busy: {data['busy']}", parent=self)
            show = box.addButton("Show job", QMessageBox.ButtonRole.AcceptRole)
            box.addButton(QMessageBox.StandardButton.Close)
            box.exec()
            if box.clickedButton() is show and data["busy"]:
                self.select(data["busy"])
            return
        self.select(data["id"])
        self.started.emit(data["id"])
        self.refresh_requested.emit()

    def _on_select(self, item, _prev):
        jid = item.data(Qt.ItemDataRole.UserRole) if item else None
        if jid != self.current:
            self._show(jid)

    def _show(self, jid):
        self.stop()
        self.current, self.offset = jid, 0
        self.decoder.reset()
        self.log.clear()
        if jid:
            self._follow()
        self._update_buttons()

    def _follow(self):
        jid = self.current
        p = self.proc = self.remote.follow(jid, self.offset)
        p.readyReadStandardOutput.connect(lambda: self._chunk(p, jid))
        p.finished.connect(lambda *_: self._ended(p))

    def _chunk(self, p, jid):
        data = bytes(p.readAllStandardOutput())
        if jid != self.current:
            return
        self.offset += len(data)
        self.append_log(self.decoder.decode(ANSI.sub(b"", data)))

    def _ended(self, p):
        if p is self.proc:
            self.proc = None
        p.deleteLater()
        self._update_buttons()

    def _update_buttons(self):
        j = self.jobs.get(self.current)
        enable(self.act_kill, None if j and j["status"] == "running" else "select a running job")
        enable(self.act_run, None if j and is_preview(j) else "select a finished preview")
        enable(self.act_results, None if j and failed_items(j) else "select a build with failed packages")