# sb-builder - Jobs tab # Copyright (C) 2026 Danilo M. # # 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 re from PyQt6.QtCore import Qt, pyqtSignal from PyQt6.QtGui import QFontDatabase, QTextCursor from PyQt6.QtWidgets import (QCheckBox, QDialog, QHBoxLayout, QInputDialog, QLineEdit, QListWidget, QListWidgetItem, QMenu, QMessageBox, QPlainTextEdit, QPushButton, QSplitter, QVBoxLayout, QWidget) from .fmt import describe, job_title 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 not self.publish.isChecked(): args.insert(1, "--no-publish") self.run.emit(["slackrepo", *args]) 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 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_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")), self.act_run, 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 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")