-
Notifications
You must be signed in to change notification settings - Fork 0
/
widget.py
328 lines (244 loc) · 9.81 KB
/
widget.py
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
import sys
from pathlib import Path
from queue import Queue
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QLabel, QSpacerItem, QTextEdit, QProgressBar
from PyQt5.QtWidgets import QHBoxLayout, QVBoxLayout, QMessageBox, QDesktopWidget, QFileDialog, QDialog
from PyQt5.QtGui import QIcon, QTextCursor
from PyQt5.QtCore import QStandardPaths, QSize, QFileInfo, QThread, QObject, QDir, pyqtSignal, pyqtSlot, QRunnable, QThreadPool
from ota_from_target_files import startBuildingIncrementalOta
first_tf_path = ""
second_tf_path = ""
output_dir = QStandardPaths.writableLocation(QStandardPaths.HomeLocation) + '/'
# The new Stream Object which replaces the default stream associated with sys.stdout
# This object just puts data in a queue!
class WriteStream(object):
def __init__(self,queue):
self.queue = queue
def write(self, text):
self.queue.put(text)
def flush(self):
pass
# A QObject (to be run in a QThread) which sits waiting for data to come through a Queue.Queue().
# It blocks until data is available, and one it has got something from the queue, it sends
# it to the "MainThread" by emitting a Qt Signal
class MyReceiver(QObject):
mysignal = pyqtSignal(str)
def __init__(self,queue, *args, **kwargs):
QObject.__init__(self, *args, **kwargs)
self.queue = queue
@pyqtSlot()
def run(self):
while True:
text = self.queue.get()
self.mysignal.emit(text)
class WorkerSignals(QObject):
'''
Defines the signals available from a running worker thread.
Supported signals are:
started
No data
finished
No data
error
tuple (exctype, value, traceback.format_exc() )
result
object data returned from processing, anything
progress
int indicating % progress
'''
started = pyqtSignal()
finished = pyqtSignal()
error = pyqtSignal(tuple)
result = pyqtSignal(object)
class Worker(QRunnable):
def __init__(self, fn, *args, **kwargs):
super(Worker, self).__init__()
self.fn = fn
self.args = args
self.kwargs = kwargs
self.signals = WorkerSignals()
def run(self):
self.signals.started.emit()
try:
result = self.fn(*self.args, **self.kwargs)
except:
exctype, value = sys.exc_info()[:2]
else:
# Return the result of the processing
self.signals.result.emit(result)
finally:
# Done
self.signals.finished.emit()
class UiWidget(QWidget):
def __init__(self):
super().__init__()
self.threadpool = QThreadPool()
self.init()
def init(self):
self.output_dir_button = QPushButton("Output Dir")
self.output_dir_button.setFixedSize(QSize(100, 40))
self.output_dir_button.clicked.connect(self.showOutputDirDialog)
self.output_dir_label = QLabel(output_dir)
self.first_tf_button = QPushButton("First TF")
self.first_tf_button.setFixedSize(QSize(100, 40))
self.first_tf_button.clicked.connect(self.showDialogFirst)
self.first_tf_label = QLabel("")
self.second_tf_button = QPushButton("Second TF")
self.second_tf_button.setFixedSize(QSize(100, 40))
self.second_tf_button.clicked.connect(self.showDialogSecond)
self.second_tf_label = QLabel("")
hbox0 = QHBoxLayout()
hbox0.addWidget(self.output_dir_button)
hbox0.addWidget(self.output_dir_label)
hbox1 = QHBoxLayout()
hbox1.addWidget(self.first_tf_button)
hbox1.addWidget(self.first_tf_label)
hbox2 = QHBoxLayout()
hbox2.addWidget(self.second_tf_button)
hbox2.addWidget(self.second_tf_label)
self.text_field = QTextEdit()
self.text_field.setReadOnly(True)
vbox1 = QVBoxLayout()
vbox1.addLayout(hbox0)
vbox1.addLayout(hbox1)
vbox1.addLayout(hbox2)
vbox1.addWidget(self.text_field)
self.progress_bar = QProgressBar(self)
self.progress_bar.setRange(0, 1)
self.start_button = QPushButton("Start")
self.start_button.clicked.connect(self.startButtonClicked)
self.cancel_button = QPushButton("Cancel")
self.cancel_button.clicked.connect(self.close)
hbox3 = QHBoxLayout()
hbox3.addWidget(self.progress_bar)
hbox3.addStretch(1)
hbox3.addWidget(self.start_button)
hbox3.addWidget(self.cancel_button)
main_vbox = QVBoxLayout()
main_vbox.addLayout(vbox1)
main_vbox.addLayout(hbox3)
self.setLayout(main_vbox)
self.center()
def progressBarOnStart(self):
self.progress_bar.setRange(0, 0)
def progressBarOnFinished(self):
self.progress_bar.setRange(0, 1)
def center(self):
qr = self.frameGeometry()
cp = QDesktopWidget().availableGeometry().center()
qr.moveCenter(cp)
self.move(qr.topLeft())
def closeEvent(self, event):
reply = QMessageBox.question(self, 'Message',
"Are you sure to quit?", QMessageBox.Yes |
QMessageBox.No, QMessageBox.No)
if reply == QMessageBox.Yes:
event.accept()
else:
event.ignore()
def showOutputDirDialog(self):
options = QFileDialog.Options()
options |= QFileDialog.DontUseNativeDialog
options |= QFileDialog.DontUseCustomDirectoryIcons
dialog = QFileDialog()
dialog.setOptions(options)
dialog.setFileMode(QFileDialog.DirectoryOnly)
dialog.setFilter(dialog.filter() | QDir.Hidden)
if dialog.exec_() == QDialog.Accepted:
temp = dialog.selectedFiles()[0]
if temp:
global output_dir
output_dir = temp + "/"
self.output_dir_label.setText(output_dir)
def showDialogFirst(self):
options = QFileDialog.Options()
options |= QFileDialog.DontUseNativeDialog
options |= QFileDialog.DontUseCustomDirectoryIcons
dialog = QFileDialog()
dialog.setOptions(options)
dialog.setNameFilters(["Zip archives (*.zip)"])
dialog.setFilter(dialog.filter() | QDir.Hidden)
if dialog.exec_() == QDialog.Accepted:
temp = dialog.selectedFiles()[0]
if temp:
global first_tf_path
first_tf_path = temp
filename = Path(first_tf_path).name
self.first_tf_label.setText(filename)
def showDialogSecond(self):
options = QFileDialog.Options()
options |= QFileDialog.DontUseNativeDialog
options |= QFileDialog.DontUseCustomDirectoryIcons
dialog = QFileDialog()
dialog.setOptions(options)
dialog.setNameFilters(["Zip archives (*.zip)"])
dialog.setFilter(dialog.filter() | QDir.Hidden)
if dialog.exec_() == QDialog.Accepted:
temp = dialog.selectedFiles()[0]
if temp:
global second_tf_path
second_tf_path = temp
filename = Path(second_tf_path).name
self.second_tf_label.setText(filename)
def showWarningDialog(self):
QMessageBox.warning(self, 'Select target files', "Select First and Second target files!")
def disableWidgetsOnBuildingStarted(self):
self.output_dir_button.setDisabled(True)
self.first_tf_button.setDisabled(True)
self.second_tf_button.setDisabled(True)
self.start_button.setDisabled(True)
def enableWidgetsOnBuildingFinished(self):
self.output_dir_button.setEnabled(True)
self.first_tf_button.setEnabled(True)
self.second_tf_button.setEnabled(True)
self.start_button.setEnabled(True)
def startButtonClicked(self):
global first_tf_path
global second_tf_path
if first_tf_path and second_tf_path:
self.text_field.clear()
try:
first_tf = self.parseTargetFileVersion(self.first_tf_label.text())
second_tf = self.parseTargetFileVersion(self.second_tf_label.text())
self.createOTA(first_tf, second_tf)
except:
print("Bad things happened during parsing target file version!")
else:
self.showWarningDialog()
def parseTargetFileVersion(self, target_file):
dash = target_file.rfind("-")
dot = target_file.rfind(".")
return target_file[dash + 1:dot]
def createOTA(self, first_tf, second_tf):
outname = output_dir + first_tf + "-" + second_tf + ".zip"
exec_list_options = ['--block', '-n', '-v', '-d', 'MMC', '-v', '-p', '.', '-m', 'linux_embedded', '--no_signing', '-i']
exec_list_options.append(first_tf_path)
exec_list_options.append(second_tf_path)
exec_list_options.append(outname)
worker = Worker(startBuildingIncrementalOta, exec_list_options)
worker.signals.started.connect(self.disableWidgetsOnBuildingStarted)
worker.signals.started.connect(self.progressBarOnStart)
worker.signals.finished.connect(self.enableWidgetsOnBuildingFinished)
worker.signals.finished.connect(self.progressBarOnFinished)
self.threadpool.start(worker)
@pyqtSlot(str)
def appendText(self, text):
self.text_field.moveCursor(QTextCursor.End)
self.text_field.insertPlainText(text)
if __name__ == '__main__':
queue = Queue()
ws = WriteStream(queue)
sys.stdout = ws
sys.stderr = ws
app = QApplication(sys.argv)
widget = UiWidget()
widget.resize(600, 450)
widget.setWindowTitle('OTATool')
widget.show()
thread = QThread()
my_receiver = MyReceiver(queue)
my_receiver.mysignal.connect(widget.appendText)
my_receiver.moveToThread(thread)
thread.started.connect(my_receiver.run)
thread.start()
sys.exit(app.exec_())