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
|
# sb-builder - Packages 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.
"""Packages tab: search the inventory, read a package's hints, start slackrepo previews."""
import html
from PyQt6.QtCore import QAbstractTableModel, QModelIndex, QSortFilterProxyModel, Qt, pyqtSignal
from PyQt6.QtGui import QColor
from PyQt6.QtWidgets import (QHBoxLayout, QLabel, QLineEdit, QMessageBox, QPushButton,
QScrollArea, QSplitter, QTableView, QVBoxLayout, QWidget)
from . import hints as hintlib
from .dashboard import STATUS
from .fmt import NA, fmt_time
OVERLAYS = ("personal", "pentesting")
STATUSES = ("ok", "failed", "never", "outdated")
COLUMNS = ("", "name", "category", "version", "built", "status", "maintainer")
STATUS_COLOR = {"ok": STATUS["ok"], "failed": STATUS["failed"], "outdated": STATUS["aborted"]}
FIELDS = {"cat": "category", "maint": "maintainer", "req": "requires", "status": "status"}
def status(row):
# ponytail: outdated compares versions only; slackrepo also rebuilds on
# SlackBuild or dependency changes, which its preview shows
if not row["built_version"] and not row["result"]:
return "never"
if row["result"] in ("failed", "bad", "aborted"):
return "failed"
if row["built_version"] != row["version"]:
return "outdated"
return "ok"
def overlay(row):
cat = row["path"].split("/", 1)[0]
return cat if cat in OVERLAYS else "upstream"
def parse_query(text):
"""[(field or None, term)], lower-cased; unknown prefixes count as plain text."""
terms = []
for word in text.lower().split():
field, sep, term = word.partition(":")
if sep and field in FIELDS and term:
terms.append((FIELDS[field], term))
else:
terms.append((None, word))
return terms
def matches(row, terms, overlays=(), statuses=()):
"""All terms match, and the row is in a toggled chip of each non-empty group."""
if overlays and overlay(row) not in overlays:
return False
if statuses and row["status"] not in statuses:
return False
for field, term in terms:
if field is None:
hay = f"{row['name']} {row['summary'] or ''}"
elif field == "requires":
hay = " ".join(row["requires"])
else:
hay = row[field] or ""
if term not in hay.lower():
return False
return True
def load_rows(inventory):
rows = []
for values in inventory["rows"]:
row = dict(zip(inventory["columns"], values))
row["category"], row["name"] = row["path"].split("/", 1)
row["status"] = status(row)
rows.append(row)
return rows
class Model(QAbstractTableModel):
def __init__(self):
super().__init__()
self.rows = []
self.checked = set() # package names
def set_rows(self, rows):
self.beginResetModel()
self.rows = rows
names = {r["name"] for r in rows}
self.checked &= names
self.endResetModel()
def rowCount(self, parent=QModelIndex()):
return 0 if parent.isValid() else len(self.rows)
def columnCount(self, parent=QModelIndex()):
return len(COLUMNS)
def headerData(self, section, orientation, role=Qt.ItemDataRole.DisplayRole):
if orientation == Qt.Orientation.Horizontal and role == Qt.ItemDataRole.DisplayRole:
return COLUMNS[section]
return None
def flags(self, index):
f = Qt.ItemFlag.ItemIsEnabled | Qt.ItemFlag.ItemIsSelectable
return f | Qt.ItemFlag.ItemIsUserCheckable if index.column() == 0 else f
def data(self, index, role=Qt.ItemDataRole.DisplayRole):
row, col = self.rows[index.row()], index.column()
if role == Qt.ItemDataRole.CheckStateRole and col == 0:
return Qt.CheckState.Checked if row["name"] in self.checked else Qt.CheckState.Unchecked
if role == Qt.ItemDataRole.DisplayRole:
return (None, row["name"], row["category"], row["version"],
row["built_version"] or "", row["status"], row["maintainer"] or "")[col]
if role == Qt.ItemDataRole.ForegroundRole and col == 5 and row["status"] in STATUS_COLOR:
return QColor(STATUS_COLOR[row["status"]])
if role == Qt.ItemDataRole.UserRole:
return row
return None
def setData(self, index, value, role=Qt.ItemDataRole.EditRole):
if role != Qt.ItemDataRole.CheckStateRole or index.column() != 0:
return False
name = self.rows[index.row()]["name"]
if Qt.CheckState(value) == Qt.CheckState.Checked:
self.checked.add(name)
else:
self.checked.discard(name)
self.dataChanged.emit(index, index)
return True
class Filter(QSortFilterProxyModel):
def __init__(self):
super().__init__()
self.terms, self.overlays, self.statuses = [], set(), set()
def update(self, text, overlays, statuses):
self.terms, self.overlays, self.statuses = parse_query(text), overlays, statuses
self.invalidateFilter()
def filterAcceptsRow(self, source_row, _parent):
row = self.sourceModel().rows[source_row]
return matches(row, self.terms, self.overlays, self.statuses)
def chip(text):
b = QPushButton(text, checkable=True)
b.setStyleSheet("QPushButton { border: 1px solid palette(mid); border-radius: 10px;"
" padding: 2px 10px; }"
"QPushButton:checked { background: palette(highlight);"
" color: palette(highlighted-text); }")
return b
def detail_html(row):
"""The detail pane for one package: facts, dependencies, then its hints."""
view = hintlib.interpret(row["hints"], row["name"], row["info_version"], row["requires"])
e = html.escape
out = [f"<h3>{e(row['name'])}</h3><p>{e(row['path'])}"
f"{' · ' + e(row['summary']) if row['summary'] else ''}</p>"]
if view["skip"]:
out.append(f'<p style="color:{STATUS["aborted"]}"><b>⚠ Skipped by slackrepo:</b> '
f"{e(view['skip'])}</p>")
facts = [(".info version", row["info_version"]), ("hint version", row["hint_version"]),
("built version", row["built_version"]),
("built", fmt_time(row["built_time"]) if row["built_time"] else None),
("last result", row["result"]), ("maintainer", row["maintainer"])]
out.append(table([(k, v or NA) for k, v in facts]))
marks = {"": "", "del": "<s>{}</s> <small>(DELREQUIRES)</small>",
"add": "+{} <small>(ADDREQUIRES)</small>", "build": "+{} <small>(build time only)</small>",
"pragma": "+{} <small>(pragma)</small>"}
deps = [(marks[m] or "{}").format(e(d)) for d, m in view["deps"]]
out.append(f"<h4>Dependencies</h4><p>{' '.join(deps) or 'none'}</p>")
if view["conflicts"]:
out.append(f"<p>Conflicts: {e(' '.join(view['conflicts']))}</p>")
for title, rows in view["sections"]:
out.append(f"<h4>{e(title)}</h4>" + table(rows))
if view["shell"]:
out.append(f"<p><small>Expanded by the shell at build time, shown as written: "
f"{e(', '.join(view['shell']))}</small></p>")
if view["ignored"]:
out.append(f'<p style="color:{STATUS["aborted"]}">Not read by slackrepo: '
f"{e(', '.join(view['ignored']))}</p>")
if row["hint_path"]:
out.append(f"<p><small>hint: {e(row['hint_path'])}</small></p>")
return "".join(out)
def table(rows):
cells = "".join(f"<tr><td style='padding-right:12px'><i>{html.escape(str(k))}</i></td>"
f"<td>{html.escape(str(v))}</td></tr>" for k, v in rows)
return f"<table>{cells}</table>"
class PackagesTab(QWidget):
start_requested = pyqtSignal(list) # job-start arguments
def __init__(self, remote, parent=None):
super().__init__(parent)
self.remote = remote
self.model = Model()
self.proxy = Filter()
self.proxy.setSourceModel(self.model)
self.query = QLineEdit(placeholderText="search (cat: maint: req: status:)")
self.query.textChanged.connect(self.refilter)
self.chips = {}
chips = QHBoxLayout()
for group in (("personal", "pentesting", "upstream"), STATUSES):
for name in group:
self.chips[name] = chip(name)
self.chips[name].toggled.connect(self.refilter)
chips.addWidget(self.chips[name])
chips.addSpacing(16)
chips.addStretch()
self.refresh_btn = QPushButton("Refresh")
self.refresh_btn.clicked.connect(self.reload)
chips.addWidget(self.refresh_btn)
self.table = QTableView(sortingEnabled=True)
self.table.setModel(self.proxy)
self.table.setSelectionBehavior(QTableView.SelectionBehavior.SelectRows)
self.table.setSelectionMode(QTableView.SelectionMode.SingleSelection)
self.table.verticalHeader().hide()
self.table.horizontalHeader().setStretchLastSection(True)
for col, width in enumerate((28, 220, 110, 120, 120, 80)):
self.table.setColumnWidth(col, width)
self.table.selectionModel().currentRowChanged.connect(self.show_detail)
self.detail = QLabel(textFormat=Qt.TextFormat.RichText, wordWrap=True,
alignment=Qt.AlignmentFlag.AlignTop)
self.detail.setTextInteractionFlags(Qt.TextInteractionFlag.TextSelectableByMouse)
self.detail.setContentsMargins(12, 8, 12, 8)
scroll = QScrollArea(widgetResizable=True)
scroll.setWidget(self.detail)
split = QSplitter()
split.addWidget(self.table)
split.addWidget(scroll)
split.setSizes([700, 400])
self.count = QLabel()
bar = QHBoxLayout()
bar.addWidget(self.count)
for label, slot in (("Select visible", self.select_visible), ("Clear", self.clear)):
b = QPushButton(label)
b.clicked.connect(slot)
bar.addWidget(b)
bar.addStretch()
for action in ("build", "update", "rebuild", "remove"):
b = QPushButton(action.capitalize())
b.clicked.connect(lambda _c, a=action: self.act(a))
bar.addWidget(b)
for label, slot in (("Update all", self.update_all), ("Publish", self.publish)):
b = QPushButton(label)
b.clicked.connect(slot)
bar.addWidget(b)
layout = QVBoxLayout(self)
layout.addWidget(self.query)
layout.addLayout(chips)
layout.addWidget(split)
layout.addLayout(bar)
self.model.dataChanged.connect(lambda *_: self.update_count())
self.update_count()
def reload(self):
self.count.setText("loading…")
self.remote.call(self._loaded, "inventory")
def _loaded(self, data, err):
if err:
self.count.setText(f"inventory failed: {err}")
return
self.model.set_rows(load_rows(data))
self.update_count()
def refilter(self):
on = {n for n, b in self.chips.items() if b.isChecked()}
self.proxy.update(self.query.text(), on - set(STATUSES), on & set(STATUSES))
self.update_count()
def update_count(self):
self.count.setText(f"{self.proxy.rowCount():,} shown · {len(self.model.checked)} selected")
def select_visible(self):
for i in range(self.proxy.rowCount()):
self.model.checked.add(self.proxy.index(i, 0).data(Qt.ItemDataRole.UserRole)["name"])
self.model.layoutChanged.emit()
self.update_count()
def clear(self):
self.model.checked.clear()
self.model.layoutChanged.emit()
self.update_count()
def targets(self):
"""Checked packages, else the current row; sorted names."""
if self.model.checked:
return sorted(self.model.checked)
current = self.table.currentIndex()
return [current.data(Qt.ItemDataRole.UserRole)["name"]] if current.isValid() else []
def act(self, action):
names = self.targets()
if names:
self.start_requested.emit(["slackrepo", action, "--preview", *names])
def update_all(self):
self.start_requested.emit(["slackrepo", "update", "--preview"])
def publish(self):
if QMessageBox.question(self, "Publish", "Push the package repository to the mirror now?") \
== QMessageBox.StandardButton.Yes:
self.start_requested.emit(["publish"])
def show_detail(self, current, _previous):
row = current.data(Qt.ItemDataRole.UserRole) if current.isValid() else None
self.detail.setText(detail_html(row) if row else "")
|