xstack 2.0.2__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- xstack/__init__.py +167 -0
- xstack/address.py +137 -0
- xstack/app/__init__.py +36 -0
- xstack/app/__main__.py +5 -0
- xstack/app/application.py +692 -0
- xstack/app/config.py +139 -0
- xstack/app/delegate.py +292 -0
- xstack/app/demo.py +42 -0
- xstack/app/dialogs.py +492 -0
- xstack/app/log_view.py +632 -0
- xstack/app/menu.py +362 -0
- xstack/app/options.py +664 -0
- xstack/app/resources.py +20 -0
- xstack/app/runner.py +35 -0
- xstack/app/settings.py +89 -0
- xstack/app/tree.py +582 -0
- xstack/attributes.py +212 -0
- xstack/compat.py +96 -0
- xstack/component.py +1175 -0
- xstack/constants.py +16 -0
- xstack/stack.py +1078 -0
- xstack/test/__init__.py +0 -0
- xstack/test/components.py +360 -0
- xstack/test/test_stack.py +1314 -0
- xstack-2.0.2.dist-info/METADATA +162 -0
- xstack-2.0.2.dist-info/RECORD +29 -0
- xstack-2.0.2.dist-info/WHEEL +5 -0
- xstack-2.0.2.dist-info/licenses/LICENSE +21 -0
- xstack-2.0.2.dist-info/top_level.txt +1 -0
xstack/app/log_view.py
ADDED
|
@@ -0,0 +1,632 @@
|
|
|
1
|
+
"""
|
|
2
|
+
The Log tab of the app. It shows log records and, while a build runs, everything
|
|
3
|
+
printed to stdout and stderr, with highlighting that makes the build report
|
|
4
|
+
easier to read. Printed output still reaches its normal destination (such as a
|
|
5
|
+
DCC's script editor) as well.
|
|
6
|
+
|
|
7
|
+
What is captured:
|
|
8
|
+
|
|
9
|
+
* Log records from the loggers named in AppConfig.log_logger_names (and
|
|
10
|
+
their children), at any time.
|
|
11
|
+
* During a build of the watched stack: every log record, and everything
|
|
12
|
+
printed. Component modules are loaded under generated module names, so
|
|
13
|
+
their loggers can only be told apart by when they log.
|
|
14
|
+
"""
|
|
15
|
+
import re
|
|
16
|
+
import sys
|
|
17
|
+
import time
|
|
18
|
+
import logging
|
|
19
|
+
import functools
|
|
20
|
+
import threading
|
|
21
|
+
|
|
22
|
+
from Qt import QtCore, QtGui, QtWidgets
|
|
23
|
+
|
|
24
|
+
# -- The kinds of text the log shows. Log records use their logging level.
|
|
25
|
+
STDOUT = 0
|
|
26
|
+
STDERR = 1
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
# --------------------------------------------------------------------------------------
|
|
30
|
+
class _StreamTee:
|
|
31
|
+
"""
|
|
32
|
+
Stands in for sys.stdout or sys.stderr. Everything written is passed on to
|
|
33
|
+
the original stream unchanged, and also to the given callback.
|
|
34
|
+
"""
|
|
35
|
+
|
|
36
|
+
def __init__(self, stream, callback):
|
|
37
|
+
self.stream = stream
|
|
38
|
+
self.callback = callback
|
|
39
|
+
|
|
40
|
+
def write(self, text):
|
|
41
|
+
result = self.stream.write(text)
|
|
42
|
+
|
|
43
|
+
if text:
|
|
44
|
+
self.callback(text)
|
|
45
|
+
|
|
46
|
+
return result
|
|
47
|
+
|
|
48
|
+
def flush(self):
|
|
49
|
+
self.stream.flush()
|
|
50
|
+
|
|
51
|
+
def __getattr__(self, name):
|
|
52
|
+
return getattr(self.stream, name)
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
# --------------------------------------------------------------------------------------
|
|
56
|
+
class _StreamCapture:
|
|
57
|
+
"""
|
|
58
|
+
Swaps sys.stdout and sys.stderr for tees while anything is subscribed, and
|
|
59
|
+
puts the original streams back once nothing is. Several log views can
|
|
60
|
+
capture at once without unwinding each other's streams.
|
|
61
|
+
"""
|
|
62
|
+
|
|
63
|
+
_lock = threading.Lock()
|
|
64
|
+
_subscribers = []
|
|
65
|
+
_tees = None
|
|
66
|
+
|
|
67
|
+
# ----------------------------------------------------------------------------------
|
|
68
|
+
@classmethod
|
|
69
|
+
def subscribe(cls, callback):
|
|
70
|
+
with cls._lock:
|
|
71
|
+
if callback in cls._subscribers:
|
|
72
|
+
return
|
|
73
|
+
|
|
74
|
+
cls._subscribers.append(callback)
|
|
75
|
+
|
|
76
|
+
if cls._tees is None:
|
|
77
|
+
cls._tees = (
|
|
78
|
+
_StreamTee(sys.stdout, cls._dispatch_stdout),
|
|
79
|
+
_StreamTee(sys.stderr, cls._dispatch_stderr),
|
|
80
|
+
)
|
|
81
|
+
sys.stdout, sys.stderr = cls._tees
|
|
82
|
+
|
|
83
|
+
# ----------------------------------------------------------------------------------
|
|
84
|
+
@classmethod
|
|
85
|
+
def unsubscribe(cls, callback):
|
|
86
|
+
with cls._lock:
|
|
87
|
+
if callback in cls._subscribers:
|
|
88
|
+
cls._subscribers.remove(callback)
|
|
89
|
+
|
|
90
|
+
if cls._subscribers or cls._tees is None:
|
|
91
|
+
return
|
|
92
|
+
|
|
93
|
+
stdout_tee, stderr_tee = cls._tees
|
|
94
|
+
cls._tees = None
|
|
95
|
+
|
|
96
|
+
# -- Only put a stream back if nothing else has replaced it since.
|
|
97
|
+
# -- Otherwise the tee stays in the chain, passing text straight
|
|
98
|
+
# -- through, rather than cutting out whatever was put on top.
|
|
99
|
+
if sys.stdout is stdout_tee:
|
|
100
|
+
sys.stdout = stdout_tee.stream
|
|
101
|
+
|
|
102
|
+
if sys.stderr is stderr_tee:
|
|
103
|
+
sys.stderr = stderr_tee.stream
|
|
104
|
+
|
|
105
|
+
# ----------------------------------------------------------------------------------
|
|
106
|
+
@classmethod
|
|
107
|
+
def _dispatch_stdout(cls, text):
|
|
108
|
+
for callback in list(cls._subscribers):
|
|
109
|
+
callback(text, STDOUT)
|
|
110
|
+
|
|
111
|
+
# ----------------------------------------------------------------------------------
|
|
112
|
+
@classmethod
|
|
113
|
+
def _dispatch_stderr(cls, text):
|
|
114
|
+
for callback in list(cls._subscribers):
|
|
115
|
+
callback(text, STDERR)
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
# --------------------------------------------------------------------------------------
|
|
119
|
+
class _Bridge(QtCore.QObject):
|
|
120
|
+
"""
|
|
121
|
+
Carries text and build events to the log view. Builds can run on a worker
|
|
122
|
+
thread, and Qt queues signals emitted from another thread, so the view is
|
|
123
|
+
only ever updated on the thread it lives on.
|
|
124
|
+
"""
|
|
125
|
+
text_received = QtCore.Signal(str, int)
|
|
126
|
+
build_started = QtCore.Signal()
|
|
127
|
+
build_finished = QtCore.Signal(bool)
|
|
128
|
+
component_reporting = QtCore.Signal(str, str)
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
# --------------------------------------------------------------------------------------
|
|
132
|
+
class _LogHandler(logging.Handler):
|
|
133
|
+
"""
|
|
134
|
+
Passes log records to the log view. It sits on the root logger and lets a
|
|
135
|
+
record through if it comes from one of the given loggers, or if a build is
|
|
136
|
+
running.
|
|
137
|
+
"""
|
|
138
|
+
|
|
139
|
+
def __init__(self, bridge: _Bridge, logger_names):
|
|
140
|
+
super().__init__()
|
|
141
|
+
self.bridge = bridge
|
|
142
|
+
self.logger_names = list(logger_names)
|
|
143
|
+
self.capturing = False
|
|
144
|
+
self.setFormatter(logging.Formatter("%(levelname)s : %(name)s : %(message)s"))
|
|
145
|
+
|
|
146
|
+
def emit(self, record):
|
|
147
|
+
if not self.capturing and not self._is_watched(record.name):
|
|
148
|
+
return
|
|
149
|
+
|
|
150
|
+
try:
|
|
151
|
+
self.bridge.text_received.emit(self.format(record) + "\n", record.levelno)
|
|
152
|
+
|
|
153
|
+
except RuntimeError:
|
|
154
|
+
# -- The view has been deleted without detaching
|
|
155
|
+
logging.getLogger().removeHandler(self)
|
|
156
|
+
|
|
157
|
+
def _is_watched(self, logger_name: str) -> bool:
|
|
158
|
+
return any(
|
|
159
|
+
logger_name == name or logger_name.startswith(name + ".")
|
|
160
|
+
for name in self.logger_names
|
|
161
|
+
)
|
|
162
|
+
|
|
163
|
+
|
|
164
|
+
# --------------------------------------------------------------------------------------
|
|
165
|
+
class _Colours:
|
|
166
|
+
"""
|
|
167
|
+
The colours the log uses, chosen to read on both light and dark palettes.
|
|
168
|
+
"""
|
|
169
|
+
warning = QtGui.QColor(230, 165, 40)
|
|
170
|
+
error = QtGui.QColor(235, 85, 85)
|
|
171
|
+
success = QtGui.QColor(95, 195, 95)
|
|
172
|
+
accent = QtGui.QColor(90, 160, 235)
|
|
173
|
+
|
|
174
|
+
|
|
175
|
+
# --------------------------------------------------------------------------------------
|
|
176
|
+
class _ReportHighlighter(QtGui.QSyntaxHighlighter):
|
|
177
|
+
"""
|
|
178
|
+
Highlights the build report that xstack prints: the header line of each
|
|
179
|
+
component, its build status, and the names in its option, input and output
|
|
180
|
+
listings. Lines that are already coloured (warnings, errors and stderr) are
|
|
181
|
+
left as they are.
|
|
182
|
+
"""
|
|
183
|
+
|
|
184
|
+
_ABOUT_TO_BUILD = re.compile(r"^(About to Build) : (.*)$")
|
|
185
|
+
_ABOUT_TO_VALIDATE = re.compile(r"^About to Validate : ")
|
|
186
|
+
_COMPONENT_TYPE = re.compile(r"^(Component Type) : (.*)$")
|
|
187
|
+
_STATUS = re.compile(r"^Component Build Status : (\w+)")
|
|
188
|
+
_SUCCEEDED = re.compile(r"^Build Succeeded")
|
|
189
|
+
_SECTION = re.compile(r"^\s+(Identifier|Options|Inputs|Outputs)\s*:")
|
|
190
|
+
_KEY_VALUE = re.compile(r"^(\s{8,})(\S.*?)(\s+: )")
|
|
191
|
+
_MARKER = re.compile(r"^-{4} .* -{4}$")
|
|
192
|
+
|
|
193
|
+
# ----------------------------------------------------------------------------------
|
|
194
|
+
def __init__(self, document, dim_colour: QtGui.QColor):
|
|
195
|
+
super().__init__(document)
|
|
196
|
+
self.dim_colour = dim_colour
|
|
197
|
+
|
|
198
|
+
# ----------------------------------------------------------------------------------
|
|
199
|
+
def highlightBlock(self, text): # noqa: N802 (Qt override)
|
|
200
|
+
|
|
201
|
+
# -- Text inserted with a colour of its own is not report text. An empty
|
|
202
|
+
# -- block has no fragments, and asking an end iterator for its fragment
|
|
203
|
+
# -- is undefined (it can hang), so check for that first.
|
|
204
|
+
fragments = self.currentBlock().begin()
|
|
205
|
+
if fragments.atEnd():
|
|
206
|
+
return
|
|
207
|
+
|
|
208
|
+
if fragments.fragment().charFormat().foreground().style() != QtCore.Qt.NoBrush:
|
|
209
|
+
return
|
|
210
|
+
|
|
211
|
+
match = self._ABOUT_TO_BUILD.match(text)
|
|
212
|
+
if match:
|
|
213
|
+
self.setFormat(0, len(text), self._format(bold=True))
|
|
214
|
+
self.setFormat(match.start(2), len(match.group(2)), self._format(colour=_Colours.accent, bold=True))
|
|
215
|
+
return
|
|
216
|
+
|
|
217
|
+
# -- Validation lists every component up front, so it is kept quiet to
|
|
218
|
+
# -- let the build headers stand out
|
|
219
|
+
if self._ABOUT_TO_VALIDATE.match(text):
|
|
220
|
+
self.setFormat(0, len(text), self._format(colour=self.dim_colour))
|
|
221
|
+
return
|
|
222
|
+
|
|
223
|
+
match = self._COMPONENT_TYPE.match(text)
|
|
224
|
+
if match:
|
|
225
|
+
self.setFormat(0, len(match.group(1)) + 2, self._format(colour=self.dim_colour))
|
|
226
|
+
return
|
|
227
|
+
|
|
228
|
+
match = self._STATUS.match(text)
|
|
229
|
+
if match:
|
|
230
|
+
colours = {
|
|
231
|
+
"success": _Colours.success,
|
|
232
|
+
"failed": _Colours.error,
|
|
233
|
+
"invalid": _Colours.warning,
|
|
234
|
+
}
|
|
235
|
+
colour = colours.get(match.group(1).lower())
|
|
236
|
+
if colour:
|
|
237
|
+
self.setFormat(0, len(text), self._format(colour=colour, bold=True))
|
|
238
|
+
return
|
|
239
|
+
|
|
240
|
+
if self._SUCCEEDED.match(text):
|
|
241
|
+
self.setFormat(0, len(text), self._format(colour=_Colours.success, bold=True))
|
|
242
|
+
return
|
|
243
|
+
|
|
244
|
+
if self._MARKER.match(text):
|
|
245
|
+
self.setFormat(0, len(text), self._format(colour=self.dim_colour, italic=True))
|
|
246
|
+
return
|
|
247
|
+
|
|
248
|
+
match = self._SECTION.match(text)
|
|
249
|
+
if match:
|
|
250
|
+
self.setFormat(0, match.end(), self._format(colour=self.dim_colour, italic=True))
|
|
251
|
+
return
|
|
252
|
+
|
|
253
|
+
match = self._KEY_VALUE.match(text)
|
|
254
|
+
if match:
|
|
255
|
+
self.setFormat(match.start(2), match.end(3) - match.start(2), self._format(colour=self.dim_colour))
|
|
256
|
+
|
|
257
|
+
# ----------------------------------------------------------------------------------
|
|
258
|
+
@staticmethod
|
|
259
|
+
def _format(colour: QtGui.QColor = None, bold: bool = False, italic: bool = False):
|
|
260
|
+
text_format = QtGui.QTextCharFormat()
|
|
261
|
+
|
|
262
|
+
if colour is not None:
|
|
263
|
+
text_format.setForeground(colour)
|
|
264
|
+
|
|
265
|
+
if bold:
|
|
266
|
+
text_format.setFontWeight(QtGui.QFont.Bold)
|
|
267
|
+
|
|
268
|
+
text_format.setFontItalic(italic)
|
|
269
|
+
return text_format
|
|
270
|
+
|
|
271
|
+
|
|
272
|
+
# --------------------------------------------------------------------------------------
|
|
273
|
+
class LogView(QtWidgets.QWidget):
|
|
274
|
+
"""
|
|
275
|
+
The content of the app's Log tab. Call watch_stack with the stack the app
|
|
276
|
+
is showing so the view can capture that stack's builds, and detach before
|
|
277
|
+
the view is thrown away.
|
|
278
|
+
|
|
279
|
+
The log is cleared when each build starts, so it always holds the whole of
|
|
280
|
+
the latest build however long it is. It records where each component's part
|
|
281
|
+
of the report starts, so show_component can scroll to it.
|
|
282
|
+
|
|
283
|
+
Signals:
|
|
284
|
+
counts_changed(warnings, errors): Emitted when the number of warnings
|
|
285
|
+
or errors shown changes. Both are reset when a build starts.
|
|
286
|
+
build_failed(): Emitted, on the view's thread, when a watched build
|
|
287
|
+
fails.
|
|
288
|
+
"""
|
|
289
|
+
|
|
290
|
+
counts_changed = QtCore.Signal(int, int)
|
|
291
|
+
build_failed = QtCore.Signal()
|
|
292
|
+
|
|
293
|
+
# ----------------------------------------------------------------------------------
|
|
294
|
+
# -- How long show_component highlights a component's section for
|
|
295
|
+
HIGHLIGHT_DURATION_MS = 2500
|
|
296
|
+
|
|
297
|
+
# ----------------------------------------------------------------------------------
|
|
298
|
+
def __init__(self, logger_names, parent=None):
|
|
299
|
+
super().__init__(parent=parent)
|
|
300
|
+
|
|
301
|
+
self.stack = None
|
|
302
|
+
self.warnings = 0
|
|
303
|
+
self.errors = 0
|
|
304
|
+
self._build_start_time = None
|
|
305
|
+
|
|
306
|
+
# -- The line each component's part of the report starts on, by uuid,
|
|
307
|
+
# -- for the validation and build stages of the latest build
|
|
308
|
+
self._sections = {
|
|
309
|
+
"validate": {},
|
|
310
|
+
"build": {},
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
self._bridge = _Bridge(self)
|
|
314
|
+
self._bridge.text_received.connect(self.append)
|
|
315
|
+
self._bridge.build_started.connect(self._on_build_started)
|
|
316
|
+
self._bridge.build_finished.connect(self._on_build_finished)
|
|
317
|
+
self._bridge.component_reporting.connect(self._on_component_reporting)
|
|
318
|
+
|
|
319
|
+
self._handler = _LogHandler(self._bridge, logger_names)
|
|
320
|
+
logging.getLogger().addHandler(self._handler)
|
|
321
|
+
|
|
322
|
+
# -- Stop capturing if the view is deleted without being detached, such
|
|
323
|
+
# -- as when the window it sits in is closed. detach only touches python
|
|
324
|
+
# -- state, so it is safe to call once the widget itself has gone.
|
|
325
|
+
self.destroyed.connect(functools.partial(LogView.detach, self))
|
|
326
|
+
|
|
327
|
+
# -- The text area
|
|
328
|
+
self.text_edit = QtWidgets.QPlainTextEdit(self)
|
|
329
|
+
self.text_edit.setReadOnly(True)
|
|
330
|
+
self.text_edit.setLineWrapMode(QtWidgets.QPlainTextEdit.NoWrap)
|
|
331
|
+
|
|
332
|
+
# -- A fixed width font keeps the report's columns aligned. It is set
|
|
333
|
+
# -- through a style sheet as well as setFont, because a host's
|
|
334
|
+
# -- application style sheet (Maya has one) overrides setFont.
|
|
335
|
+
self.text_edit.setFont(QtGui.QFontDatabase.systemFont(QtGui.QFontDatabase.FixedFont))
|
|
336
|
+
self.text_edit.setStyleSheet(
|
|
337
|
+
'QPlainTextEdit { font-family: Consolas, "Courier New", monospace; }'
|
|
338
|
+
)
|
|
339
|
+
|
|
340
|
+
# -- Owned by the view, so it cannot fire once the view has gone
|
|
341
|
+
self._highlight_timer = QtCore.QTimer(self)
|
|
342
|
+
self._highlight_timer.setSingleShot(True)
|
|
343
|
+
self._highlight_timer.timeout.connect(self._clear_highlight)
|
|
344
|
+
|
|
345
|
+
palette = self.text_edit.palette()
|
|
346
|
+
dim_colour = QtGui.QColor(palette.color(QtGui.QPalette.Text))
|
|
347
|
+
dim_colour.setAlphaF(0.55)
|
|
348
|
+
self._highlighter = _ReportHighlighter(self.text_edit.document(), dim_colour)
|
|
349
|
+
|
|
350
|
+
# -- The buttons
|
|
351
|
+
self.auto_scroll = QtWidgets.QCheckBox("Auto Scroll", self)
|
|
352
|
+
self.auto_scroll.setChecked(True)
|
|
353
|
+
|
|
354
|
+
copy_button = QtWidgets.QPushButton("Copy", self)
|
|
355
|
+
copy_button.clicked.connect(self.copy)
|
|
356
|
+
|
|
357
|
+
clear_button = QtWidgets.QPushButton("Clear", self)
|
|
358
|
+
clear_button.clicked.connect(self.clear)
|
|
359
|
+
|
|
360
|
+
button_layout = QtWidgets.QHBoxLayout()
|
|
361
|
+
button_layout.setContentsMargins(0, 0, 0, 0)
|
|
362
|
+
button_layout.addWidget(self.auto_scroll)
|
|
363
|
+
button_layout.addStretch(1)
|
|
364
|
+
button_layout.addWidget(copy_button)
|
|
365
|
+
button_layout.addWidget(clear_button)
|
|
366
|
+
|
|
367
|
+
layout = QtWidgets.QVBoxLayout(self)
|
|
368
|
+
layout.setContentsMargins(2, 2, 2, 2)
|
|
369
|
+
layout.addWidget(self.text_edit)
|
|
370
|
+
layout.addLayout(button_layout)
|
|
371
|
+
|
|
372
|
+
# ----------------------------------------------------------------------------------
|
|
373
|
+
def watch_stack(self, stack):
|
|
374
|
+
"""
|
|
375
|
+
Captures the builds of the given stack from now on, and stops capturing
|
|
376
|
+
the builds of the stack watched before. Pass None to watch nothing.
|
|
377
|
+
|
|
378
|
+
This always reconnects, even to the stack already watched: an app can
|
|
379
|
+
clear a stack's listeners without the view knowing (aniseed does each
|
|
380
|
+
time it shows a rig, including the rig already shown).
|
|
381
|
+
"""
|
|
382
|
+
if self.stack is not None:
|
|
383
|
+
# -- disconnect returns False rather than raising if the callback
|
|
384
|
+
# -- was already cleared
|
|
385
|
+
self.stack.build_started.disconnect(self._on_stack_build_started)
|
|
386
|
+
self.stack.build_finished.disconnect(self._on_stack_build_finished)
|
|
387
|
+
self.stack.component_reporting.disconnect(self._on_stack_component_reporting)
|
|
388
|
+
|
|
389
|
+
self.stack = stack
|
|
390
|
+
|
|
391
|
+
if stack is not None:
|
|
392
|
+
stack.build_started.connect(self._on_stack_build_started)
|
|
393
|
+
stack.build_finished.connect(self._on_stack_build_finished)
|
|
394
|
+
stack.component_reporting.connect(self._on_stack_component_reporting)
|
|
395
|
+
|
|
396
|
+
# ----------------------------------------------------------------------------------
|
|
397
|
+
def detach(self):
|
|
398
|
+
"""
|
|
399
|
+
Stops all capturing. Call this before the view is thrown away.
|
|
400
|
+
"""
|
|
401
|
+
self.watch_stack(None)
|
|
402
|
+
self._stop_capturing()
|
|
403
|
+
logging.getLogger().removeHandler(self._handler)
|
|
404
|
+
|
|
405
|
+
# ----------------------------------------------------------------------------------
|
|
406
|
+
def append(self, text: str, kind: int = STDOUT):
|
|
407
|
+
"""
|
|
408
|
+
Adds the given text to the end of the log. Warnings, errors and stderr
|
|
409
|
+
are coloured; anything else is left to the report highlighter.
|
|
410
|
+
|
|
411
|
+
Args:
|
|
412
|
+
text: The text to add
|
|
413
|
+
kind: STDOUT, STDERR or a logging level
|
|
414
|
+
"""
|
|
415
|
+
colour = None
|
|
416
|
+
|
|
417
|
+
if kind == STDERR or kind >= logging.ERROR:
|
|
418
|
+
colour = _Colours.error
|
|
419
|
+
|
|
420
|
+
elif kind >= logging.WARNING:
|
|
421
|
+
colour = _Colours.warning
|
|
422
|
+
|
|
423
|
+
self._count(text, kind)
|
|
424
|
+
self._insert(text, colour)
|
|
425
|
+
|
|
426
|
+
# ----------------------------------------------------------------------------------
|
|
427
|
+
def _insert(self, text: str, colour: QtGui.QColor = None):
|
|
428
|
+
text_format = QtGui.QTextCharFormat()
|
|
429
|
+
|
|
430
|
+
if colour is not None:
|
|
431
|
+
text_format.setForeground(colour)
|
|
432
|
+
|
|
433
|
+
cursor = QtGui.QTextCursor(self.text_edit.document())
|
|
434
|
+
cursor.movePosition(QtGui.QTextCursor.End)
|
|
435
|
+
cursor.insertText(text, text_format)
|
|
436
|
+
|
|
437
|
+
if self.auto_scroll.isChecked():
|
|
438
|
+
scroll_bar = self.text_edit.verticalScrollBar()
|
|
439
|
+
scroll_bar.setValue(scroll_bar.maximum())
|
|
440
|
+
|
|
441
|
+
# ----------------------------------------------------------------------------------
|
|
442
|
+
def clear(self):
|
|
443
|
+
self.text_edit.clear()
|
|
444
|
+
self._set_counts(0, 0)
|
|
445
|
+
|
|
446
|
+
for sections in self._sections.values():
|
|
447
|
+
sections.clear()
|
|
448
|
+
|
|
449
|
+
# ----------------------------------------------------------------------------------
|
|
450
|
+
def has_component(self, component) -> bool:
|
|
451
|
+
"""
|
|
452
|
+
Returns True if the log holds part of the report for the given component
|
|
453
|
+
"""
|
|
454
|
+
return any(
|
|
455
|
+
component.uuid() in sections
|
|
456
|
+
for sections in self._sections.values()
|
|
457
|
+
)
|
|
458
|
+
|
|
459
|
+
# ----------------------------------------------------------------------------------
|
|
460
|
+
def show_component(self, component) -> bool:
|
|
461
|
+
"""
|
|
462
|
+
Scrolls the log so the given component's part of the report is at the
|
|
463
|
+
top, and highlights it for a moment. Its build section is preferred,
|
|
464
|
+
falling back to its validation line where it never got to build (it
|
|
465
|
+
was invalid, or the build stopped before it).
|
|
466
|
+
|
|
467
|
+
Returns False if the log holds nothing for the component.
|
|
468
|
+
"""
|
|
469
|
+
uuid = component.uuid()
|
|
470
|
+
start = self._sections["build"].get(uuid)
|
|
471
|
+
building = start is not None
|
|
472
|
+
|
|
473
|
+
if not building:
|
|
474
|
+
start = self._sections["validate"].get(uuid)
|
|
475
|
+
|
|
476
|
+
if start is None:
|
|
477
|
+
return False
|
|
478
|
+
|
|
479
|
+
document = self.text_edit.document()
|
|
480
|
+
first_block = document.findBlockByNumber(start)
|
|
481
|
+
|
|
482
|
+
if not first_block.isValid():
|
|
483
|
+
return False
|
|
484
|
+
|
|
485
|
+
# -- A build section runs up to its status line; a validation entry is
|
|
486
|
+
# -- just the one line
|
|
487
|
+
last_block = first_block
|
|
488
|
+
if building:
|
|
489
|
+
block = first_block
|
|
490
|
+
while block.isValid():
|
|
491
|
+
last_block = block
|
|
492
|
+
if block.text().startswith("Component Build Status"):
|
|
493
|
+
break
|
|
494
|
+
block = block.next()
|
|
495
|
+
|
|
496
|
+
self.text_edit.verticalScrollBar().setValue(first_block.blockNumber())
|
|
497
|
+
|
|
498
|
+
selection = QtWidgets.QTextEdit.ExtraSelection()
|
|
499
|
+
highlight = QtGui.QColor(_Colours.accent)
|
|
500
|
+
highlight.setAlpha(60)
|
|
501
|
+
selection.format.setBackground(highlight)
|
|
502
|
+
selection.format.setProperty(QtGui.QTextFormat.FullWidthSelection, True)
|
|
503
|
+
selection.cursor = QtGui.QTextCursor(first_block)
|
|
504
|
+
selection.cursor.setPosition(
|
|
505
|
+
last_block.position() + last_block.length() - 1,
|
|
506
|
+
QtGui.QTextCursor.KeepAnchor,
|
|
507
|
+
)
|
|
508
|
+
self.text_edit.setExtraSelections([selection])
|
|
509
|
+
self._highlight_timer.start(self.HIGHLIGHT_DURATION_MS)
|
|
510
|
+
return True
|
|
511
|
+
|
|
512
|
+
# ----------------------------------------------------------------------------------
|
|
513
|
+
def _clear_highlight(self):
|
|
514
|
+
self.text_edit.setExtraSelections([])
|
|
515
|
+
|
|
516
|
+
# ----------------------------------------------------------------------------------
|
|
517
|
+
def copy(self):
|
|
518
|
+
QtWidgets.QApplication.clipboard().setText(self.text_edit.toPlainText())
|
|
519
|
+
|
|
520
|
+
# ----------------------------------------------------------------------------------
|
|
521
|
+
def _count(self, text: str, kind: int):
|
|
522
|
+
warnings = self.warnings
|
|
523
|
+
errors = self.errors
|
|
524
|
+
|
|
525
|
+
if kind == STDERR:
|
|
526
|
+
# -- A traceback arrives as many writes, so count the header only
|
|
527
|
+
if "Traceback (most recent call last)" in text:
|
|
528
|
+
errors += 1
|
|
529
|
+
|
|
530
|
+
elif kind >= logging.ERROR:
|
|
531
|
+
errors += 1
|
|
532
|
+
|
|
533
|
+
elif kind >= logging.WARNING:
|
|
534
|
+
warnings += 1
|
|
535
|
+
|
|
536
|
+
self._set_counts(warnings, errors)
|
|
537
|
+
|
|
538
|
+
# ----------------------------------------------------------------------------------
|
|
539
|
+
def _set_counts(self, warnings: int, errors: int):
|
|
540
|
+
if (warnings, errors) == (self.warnings, self.errors):
|
|
541
|
+
return
|
|
542
|
+
|
|
543
|
+
self.warnings = warnings
|
|
544
|
+
self.errors = errors
|
|
545
|
+
self.counts_changed.emit(warnings, errors)
|
|
546
|
+
|
|
547
|
+
# ----------------------------------------------------------------------------------
|
|
548
|
+
def _stdout_received(self, text: str, kind: int):
|
|
549
|
+
try:
|
|
550
|
+
self._bridge.text_received.emit(text, kind)
|
|
551
|
+
|
|
552
|
+
except RuntimeError:
|
|
553
|
+
# -- The view has been deleted without detaching
|
|
554
|
+
_StreamCapture.unsubscribe(self._stdout_received)
|
|
555
|
+
|
|
556
|
+
# ----------------------------------------------------------------------------------
|
|
557
|
+
def _start_capturing(self):
|
|
558
|
+
self._handler.capturing = True
|
|
559
|
+
_StreamCapture.subscribe(self._stdout_received)
|
|
560
|
+
|
|
561
|
+
# ----------------------------------------------------------------------------------
|
|
562
|
+
def _stop_capturing(self):
|
|
563
|
+
self._handler.capturing = False
|
|
564
|
+
_StreamCapture.unsubscribe(self._stdout_received)
|
|
565
|
+
|
|
566
|
+
# ----------------------------------------------------------------------------------
|
|
567
|
+
def _on_stack_build_started(self):
|
|
568
|
+
# -- This runs on the thread doing the build, so it only swaps the
|
|
569
|
+
# -- streams (which is thread agnostic) and hands the rest to the bridge
|
|
570
|
+
try:
|
|
571
|
+
self._bridge.build_started.emit()
|
|
572
|
+
|
|
573
|
+
except RuntimeError:
|
|
574
|
+
# -- The view has been deleted without detaching
|
|
575
|
+
self.watch_stack(None)
|
|
576
|
+
return
|
|
577
|
+
|
|
578
|
+
self._start_capturing()
|
|
579
|
+
|
|
580
|
+
# ----------------------------------------------------------------------------------
|
|
581
|
+
def _on_stack_build_finished(self, successful=True):
|
|
582
|
+
self._stop_capturing()
|
|
583
|
+
|
|
584
|
+
try:
|
|
585
|
+
self._bridge.build_finished.emit(bool(successful))
|
|
586
|
+
|
|
587
|
+
except RuntimeError:
|
|
588
|
+
# -- The view has been deleted without detaching
|
|
589
|
+
self.watch_stack(None)
|
|
590
|
+
|
|
591
|
+
# ----------------------------------------------------------------------------------
|
|
592
|
+
def _on_stack_component_reporting(self, component, stage: str):
|
|
593
|
+
# -- Like the other stack callbacks, this runs on the thread doing the
|
|
594
|
+
# -- build. Going through the bridge keeps it in order with the report
|
|
595
|
+
# -- text, so the line recorded is the one the report starts on.
|
|
596
|
+
try:
|
|
597
|
+
self._bridge.component_reporting.emit(component.uuid(), stage)
|
|
598
|
+
|
|
599
|
+
except RuntimeError:
|
|
600
|
+
# -- The view has been deleted without detaching
|
|
601
|
+
self.watch_stack(None)
|
|
602
|
+
|
|
603
|
+
# ----------------------------------------------------------------------------------
|
|
604
|
+
def _on_component_reporting(self, uuid: str, stage: str):
|
|
605
|
+
if stage not in self._sections:
|
|
606
|
+
return
|
|
607
|
+
|
|
608
|
+
# -- The report text has not arrived yet, so it will start on the
|
|
609
|
+
# -- (empty) last line of the document
|
|
610
|
+
self._sections[stage][uuid] = self.text_edit.document().blockCount() - 1
|
|
611
|
+
|
|
612
|
+
# ----------------------------------------------------------------------------------
|
|
613
|
+
def _on_build_started(self):
|
|
614
|
+
self._build_start_time = time.time()
|
|
615
|
+
|
|
616
|
+
# -- Each build starts with an empty log, so the whole of the latest
|
|
617
|
+
# -- build is always there however long it is
|
|
618
|
+
self.clear()
|
|
619
|
+
self._insert(f"---- Build started {time.strftime('%H:%M:%S')} ----\n")
|
|
620
|
+
|
|
621
|
+
# ----------------------------------------------------------------------------------
|
|
622
|
+
def _on_build_finished(self, successful: bool):
|
|
623
|
+
duration = ""
|
|
624
|
+
if self._build_start_time is not None:
|
|
625
|
+
duration = f" ({time.time() - self._build_start_time:.3f}s)"
|
|
626
|
+
|
|
627
|
+
if successful:
|
|
628
|
+
self._insert(f"---- Build finished{duration} ----\n")
|
|
629
|
+
return
|
|
630
|
+
|
|
631
|
+
self._insert(f"---- Build failed{duration} ----\n", _Colours.error)
|
|
632
|
+
self.build_failed.emit()
|