-
Notifications
You must be signed in to change notification settings - Fork 24
/
BlenderUpdater.py
621 lines (543 loc) · 21.9 KB
/
BlenderUpdater.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
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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
"""
Copyright 2016-2019 Tobias Kummer/Overmind Studios.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
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, see <https://www.gnu.org/licenses/>.
"""
import configparser
import json
import logging
import os
import os.path
import platform
import shutil
import ssl
import subprocess
import sys
import urllib.parse
import urllib.request
import webbrowser
from datetime import datetime
from shutil import copytree
from packaging.utils import Version
import requests
import re
import mainwindow
import qdarkstyle
from PySide6 import QtWidgets, QtCore, QtGui
os.environ["QT_AUTO_SCREEN_SCALE_FACTOR"] = "1"
QtWidgets.QApplication.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling)
app = QtWidgets.QApplication(sys.argv)
appversion = "1.11.0"
dir_ = ""
config = configparser.ConfigParser()
btn = {}
lastversion = ""
installedversion = ""
LOG_FORMAT = "%(levelname)s %(asctime)s - %(message)s"
logging.basicConfig(
filename="BlenderUpdater.log", format=LOG_FORMAT, level=logging.DEBUG, filemode="w"
)
logger = logging.getLogger()
class WorkerThread(QtCore.QThread):
"""Does all the actual work in the background, informs GUI about status"""
update = QtCore.Signal(int)
finishedDL = QtCore.Signal()
finishedEX = QtCore.Signal()
finishedCP = QtCore.Signal()
finishedCL = QtCore.Signal()
def __init__(self, url, file):
super(WorkerThread, self).__init__(parent=app)
self.filename = file
self.url = url
if "macOS" in file:
config.set("main", "lastdl", "OSX")
with open("config.ini", "w") as f:
config.write(f)
f.close()
elif "win32" in file:
config.set("main", "lastdl", "Windows 32bit")
with open("config.ini", "w") as f:
config.write(f)
f.close()
elif "win64" in file:
config.set("main", "lastdl", "Windows 64bit")
with open("config.ini", "w") as f:
config.write(f)
f.close()
elif "glibc211-i686" in file:
config.set("main", "lastdl", "Linux glibc211 i686")
with open("config.ini", "w") as f:
config.write(f)
f.close()
elif "glibc211-x86_64" in file:
config.set("main", "lastdl", "Linux glibc211 x86_64")
with open("config.ini", "w") as f:
config.write(f)
f.close()
elif "glibc219-i686" in file:
config.set("main", "lastdl", "Linux glibc219 i686")
with open("config.ini", "w") as f:
config.write(f)
f.close()
elif "glibc219-x86_64" in file:
config.set("main", "lastdl", "Linux glibc219 x86_64")
with open("config.ini", "w") as f:
config.write(f)
f.close()
def progress(self, count, blockSize, totalSize):
"""Updates progress bar"""
percent = int(count * blockSize * 100 / totalSize)
self.update.emit(percent)
def run(self):
"""
It downloads the file, emits a signal when it's done, and then unpacks the file
"""
urllib.request.urlretrieve(self.url, self.filename, reporthook=self.progress)
self.finishedDL.emit()
shutil.unpack_archive(self.filename, "./blendertemp/")
self.finishedEX.emit()
source = next(os.walk("./blendertemp/"))[1]
copytree(os.path.join("./blendertemp/", source[0]), dir_)
self.finishedCP.emit()
shutil.rmtree("./blendertemp")
self.finishedCL.emit()
class BlenderUpdater(QtWidgets.QMainWindow, mainwindow.Ui_MainWindow):
def __init__(self, parent=None):
logger.info(f"Running version {appversion}")
logger.debug("Constructing UI")
super(BlenderUpdater, self).__init__(parent)
self.setupUi(self)
self.btn_oneclick.hide()
self.lbl_quick.hide()
self.lbl_caution.hide()
self.btn_newVersion.hide()
self.btn_execute.hide()
self.lbl_caution.setStyleSheet("background: rgb(255, 155, 8);\n" "color: white")
global lastversion
global dir_
global config
global installedversion
if os.path.isfile("./config.ini"):
config_exist = True
logger.info("Reading existing configuration file")
config.read("config.ini")
dir_ = config.get("main", "path")
lastcheck = config.get("main", "lastcheck")
lastversion = config.get("main", "lastdl")
installedversion = config.get("main", "installed")
flavor = config.get("main", "flavor")
if lastversion != "":
self.btn_oneclick.setText(f"{flavor} | {lastversion}")
else:
pass
else:
logger.debug("No previous config found")
self.btn_oneclick.hide()
config_exist = False
config.read("config.ini")
config.add_section("main")
config.set("main", "path", "")
lastcheck = "Never"
config.set("main", "lastcheck", lastcheck)
config.set("main", "lastdl", "")
config.set("main", "installed", "")
config.set("main", "flavor", "")
with open("config.ini", "w") as f:
config.write(f)
if config_exist:
self.line_path.setText(dir_)
else:
pass
dir_ = self.line_path.text()
self.btn_cancel.hide()
self.frm_progress.hide()
self.btngrp_filter.hide()
self.btn_Check.setFocus()
self.lbl_available.hide()
self.progressBar.setValue(0)
self.progressBar.hide()
self.lbl_task.hide()
self.statusbar.showMessage(f"Ready - Last check: {lastcheck}")
self.btn_Quit.clicked.connect(QtCore.QCoreApplication.instance().quit)
self.btn_Check.clicked.connect(self.check_dir)
self.btn_about.clicked.connect(self.about)
self.btn_path.clicked.connect(self.select_path)
# Check internet connection, disable SSL
# FIXME - should be changed! (preliminary fix to work in OSX)
ssl._create_default_https_context = ssl._create_unverified_context
try:
_ = requests.get("http://www.github.com")
except Exception:
QtWidgets.QMessageBox.critical(
self, "Error", "Please check your internet connection"
)
logger.critical("No internet connection")
# Check for new version on github
try:
Appupdate = requests.get(
"https://api.github.com/repos/overmindstudios/BlenderUpdater/releases/latest"
).text
logger.info("Getting update info - success")
except Exception:
logger.error("Unable to get update information from GitHub")
try:
UpdateData = json.loads(Appupdate)
applatestversion = UpdateData["tag_name"]
logger.info(f"Version found online: {applatestversion}")
if Version(applatestversion) > Version(appversion):
logger.info("Newer version found on Github")
self.btn_newVersion.clicked.connect(self.getAppUpdate)
self.btn_newVersion.setStyleSheet("background: rgb(73, 50, 20)")
self.btn_newVersion.show()
except Exception:
QtWidgets.QMessageBox.critical(
self, "Error", "Unable to get Github update information"
)
def select_path(self):
global dir_
dir_ = QtWidgets.QFileDialog.getExistingDirectory(
None, "Select a folder:", "C:\\", QtWidgets.QFileDialog.ShowDirsOnly
)
if dir_:
self.line_path.setText(dir_)
else:
pass
def getAppUpdate(self):
webbrowser.open(
"https://github.com/overmindstudios/BlenderUpdater/releases/latest"
)
def about(self):
aboutText = (
'<html><head/><body><p>Utility to update Blender to the latest buildbot version available at<br> \
<a href="https://builder.blender.org/download/"><span style=" text-decoration: underline; color:#2980b9;">\
https://builder.blender.org/download/</span></a></p><p><br/>Developed by Tobias Kummer for \
<a href="http://www.overmind-studios.de"><span style="text-decoration:underline; color:#2980b9;"> \
Overmind Studios</span></a></p><p>\
Licensed under the <a href="https://www.gnu.org/licenses/gpl-3.0-standalone.html"><span style=" text-decoration:\
underline; color:#2980b9;">GPL v3 license</span></a></p><p>Project home: \
<a href="https://overmindstudios.github.io/BlenderUpdater/"><span style=" text-decoration:\
underline; color:#2980b9;">https://overmindstudios.github.io/BlenderUpdater/</a></p> \
<p style="text-align: center;"><a href="https://ko-fi.com/tobkum" target="_blank"> \
<img src="qrc://newPrefix/images/orange_img.png"></a></p> \
<p>Application version: '
+ appversion
+ "</p></body></html> "
)
QtWidgets.QMessageBox.about(self, "About", aboutText)
def check_dir(self):
"""
Check if a valid directory has been set by the user
"""
global dir_
dir_ = self.line_path.text()
if not os.path.exists(dir_):
QtWidgets.QMessageBox.about(
self,
"Directory not set",
"Please choose a valid destination directory first",
)
logger.error("No valid directory")
else:
logger.info("Checking for Blender versions")
self.check()
def hbytes(self, num):
"""
Return a human readable file size
:param num: The number of bytes to convert
:return: a string of the number of bytes in a more human readable way.
"""
for x in [" bytes", " KB", " MB", " GB"]:
if num < 1024.0:
return "%3.1f%s" % (num, x)
num /= 1024.0
return "%3.1f%s" % (num, " TB")
def check(self):
global dir_
global lastversion
global installedversion
dir_ = self.line_path.text()
self.frm_start.hide()
self.frm_progress.hide()
self.btn_oneclick.hide()
self.lbl_quick.hide()
self.btn_newVersion.hide()
self.progressBar.hide()
self.lbl_task.hide()
self.btn_newVersion.hide()
self.btn_execute.hide()
appleicon = QtGui.QIcon(":/newPrefix/images/Apple-icon.png")
windowsicon = QtGui.QIcon(":/newPrefix/images/Windows-icon.png")
linuxicon = QtGui.QIcon(":/newPrefix/images/Linux-icon.png")
url = "https://builder.blender.org/download/"
# Do path settings save here, in case user has manually edited it
global config
config.read("config.ini")
config.set("main", "path", dir_)
with open("config.ini", "w") as f:
config.write(f)
f.close()
try:
req = requests.get(url)
except Exception:
self.statusBar().showMessage(
"Error reaching server - check your internet connection"
)
logger.error("No connection to Blender nightly builds server")
self.frm_start.show()
def clean(text):
"""Removes spaces and uneeded characters from the given text."""
return text.strip().strip("\xa0")
def parse_description(element):
"""Parses the given description element.
Input text may look like:
<span>May 24, 07:44:19 - blender-v283-release - d553edeb7dbb - zip - 171.79MB</span>
"""
if element is None:
return None
parts = element.text.split(" - ")
output = {}
output["date"] = clean(parts[0])
# output["name"] = clean(parts[1])
output["hash"] = clean(parts[1])
output["type"] = clean(parts[2])
output["size"] = clean(parts[3])
return output
# iterate through the found versions
templist = []
filenames = re.findall(
r'blender-[^\s][^"]+',
req.text,
)
for el in filenames:
if "sha256" not in el and "/" not in el and "msi" not in el:
templist.append(el)
finallist = list(set(templist))
finallist.sort(reverse=True)
def render_buttons(os_filter=["windows", "darwin", "linux"]):
"""Renders the download buttons on screen.
os_filter: Will modify the buttons to be rendered.
If an empty list is being provided, all operating systems
will be shown.
"""
# Generate buttons for downloadable versions.
global btn
opsys = platform.system()
logger.info(f"Operating system: {opsys}")
for i in btn:
btn[i].hide()
i = 0
btn = {}
for index, entry in enumerate(finallist):
btn[index] = QtWidgets.QPushButton(self)
skip_entry = False
# Skip based on os_filter entries
if any(filter not in entry for filter in os_filter):
skip_entry = True
if skip_entry:
continue
buttontext = f"{entry}"
logger.debug(buttontext)
if "windows" in entry:
btn[index].setIcon(windowsicon)
elif "darwin" in entry:
btn[index].setIcon(appleicon)
elif "linux" in entry:
btn[index].setIcon(linuxicon)
btn[index].setIconSize(QtCore.QSize(24, 24))
btn[index].setText(buttontext)
btn[index].setFixedWidth(686)
btn[index].move(6, 50 + i)
i += 32
btn[index].clicked.connect(
lambda throwaway=0, entry=entry: self.download(entry)
)
btn[index].show()
def filterall():
render_buttons(os_filter=[])
def filterosx():
render_buttons(
os_filter=[
"darwin",
]
)
def filterlinux():
render_buttons(
os_filter=[
"linux",
]
)
def filterwindows():
render_buttons(
os_filter=[
"windows",
]
)
self.lbl_available.show()
self.lbl_caution.show()
self.btngrp_filter.show()
self.btn_osx.clicked.connect(filterosx)
self.btn_linux.clicked.connect(filterlinux)
self.btn_windows.clicked.connect(filterwindows)
self.btn_allos.clicked.connect(filterall)
lastcheck = datetime.now().strftime("%a %b %d %H:%M:%S %Y")
self.statusbar.showMessage(f"Ready - Last check: {str(lastcheck)}")
config.read("config.ini")
config.set("main", "lastcheck", str(lastcheck))
with open("config.ini", "w") as f:
config.write(f)
f.close()
filterall()
def download(self, entry):
"""
Download the file
:param entry: The version of Blender you want to download
:return: Nothing.
"""
global dir_
url = "https://builder.blender.org/download/daily/" + entry
if entry == installedversion:
reply = QtWidgets.QMessageBox.question(
self,
"Warning",
"This version is already installed. Do you still want to continue?",
QtWidgets.QMessageBox.Yes | QtWidgets.QMessageBox.No,
QtWidgets.QMessageBox.No,
)
logger.info("Duplicated version detected")
if reply == QtWidgets.QMessageBox.No:
logger.debug("Skipping download of existing version")
return
else:
pass
else:
pass
if os.path.isdir("./blendertemp"):
shutil.rmtree("./blendertemp")
os.makedirs("./blendertemp")
file = urllib.request.urlopen(url)
totalsize = file.info()["Content-Length"]
size_readable = self.hbytes(float(totalsize))
global config
config.read("config.ini")
config.set("main", "path", dir_)
config.set("main", "flavor", entry)
config.set("main", "installed", entry)
with open("config.ini", "w") as f:
config.write(f)
f.close()
##########################
# Do the actual download #
##########################
dir_ = os.path.join(dir_, "")
filename = "./blendertemp/" + entry
for i in btn:
btn[i].hide()
logger.info(f"Starting download thread for {url}{entry}")
self.lbl_available.hide()
self.lbl_caution.hide()
self.progressBar.show()
self.btngrp_filter.hide()
self.lbl_task.setText("Downloading")
self.lbl_task.show()
self.frm_progress.show()
nowpixmap = QtGui.QPixmap(":/newPrefix/images/Actions-arrow-right-icon.png")
self.lbl_download_pic.setPixmap(nowpixmap)
self.lbl_downloading.setText(f"<b>Downloading {entry}</b>")
self.progressBar.setValue(0)
self.btn_Check.setDisabled(True)
self.statusbar.showMessage(f"Downloading {size_readable}")
thread = WorkerThread(url, filename)
thread.update.connect(self.updatepb)
thread.finishedDL.connect(self.extraction)
thread.finishedEX.connect(self.finalcopy)
thread.finishedCP.connect(self.cleanup)
thread.finishedCL.connect(self.done)
thread.start()
def updatepb(self, percent):
self.progressBar.setValue(percent)
def extraction(self):
"""
Extract the downloaded file to a temporary directory
"""
logger.info("Extracting to temp directory")
self.lbl_task.setText("Extracting...")
self.btn_Quit.setEnabled(False)
nowpixmap = QtGui.QPixmap(":/newPrefix/images/Actions-arrow-right-icon.png")
donepixmap = QtGui.QPixmap(":/newPrefix/images/Check-icon.png")
self.lbl_download_pic.setPixmap(donepixmap)
self.lbl_extract_pic.setPixmap(nowpixmap)
self.lbl_extraction.setText("<b>Extraction</b>")
self.statusbar.showMessage("Extracting to temporary folder, please wait...")
self.progressBar.setMaximum(0)
self.progressBar.setMinimum(0)
self.progressBar.setValue(-1)
def finalcopy(self):
logger.info("Copying to " + dir_)
nowpixmap = QtGui.QPixmap(":/newPrefix/images/Actions-arrow-right-icon.png")
donepixmap = QtGui.QPixmap(":/newPrefix/images/Check-icon.png")
self.lbl_extract_pic.setPixmap(donepixmap)
self.lbl_copy_pic.setPixmap(nowpixmap)
self.lbl_copying.setText("<b>Copying</b>")
self.lbl_task.setText("Copying files...")
self.statusbar.showMessage(f"Copying files to {dir_}, please wait... ")
def cleanup(self):
logger.info("Cleaning up temp files")
nowpixmap = QtGui.QPixmap(":/newPrefix/images/Actions-arrow-right-icon.png")
donepixmap = QtGui.QPixmap(":/newPrefix/images/Check-icon.png")
self.lbl_copy_pic.setPixmap(donepixmap)
self.lbl_clean_pic.setPixmap(nowpixmap)
self.lbl_cleanup.setText("<b>Cleaning up</b>")
self.lbl_task.setText("Cleaning up...")
self.statusbar.showMessage("Cleaning temporary files")
def done(self):
logger.info("Finished")
donepixmap = QtGui.QPixmap(":/newPrefix/images/Check-icon.png")
self.lbl_clean_pic.setPixmap(donepixmap)
self.statusbar.showMessage("Ready")
self.progressBar.setMinimum(0)
self.progressBar.setMaximum(100)
self.progressBar.setValue(100)
self.lbl_task.setText("Finished")
self.btn_Quit.setEnabled(True)
self.btn_Check.setEnabled(True)
self.btn_execute.show()
opsys = platform.system()
if opsys == "Windows":
self.btn_execute.clicked.connect(self.exec_windows)
if opsys.lower == "darwin":
self.btn_execute.clicked.connect(self.exec_osx)
if opsys == "Linux":
self.btn_execute.clicked.connect(self.exec_linux)
def exec_windows(self):
_ = subprocess.Popen(os.path.join('"' + dir_ + "\\blender.exe" + '"'))
logger.info(f"Executing {dir_}blender.exe")
def exec_osx(self):
BlenderOSXPath = os.path.join(
'"' + dir_ + "\\blender.app/Contents/MacOS/blender" + '"'
)
os.system("chmod +x " + BlenderOSXPath)
_ = subprocess.Popen(BlenderOSXPath)
logger.info(f"Executing {BlenderOSXPath}")
def exec_linux(self):
_ = subprocess.Popen(os.path.join(f"{dir_}/blender"))
logger.info(f"Executing {dir_}blender")
def main():
"""
This function creates an instance of the BlenderUpdater class and passes it to the Qt application
"""
app.setStyleSheet(qdarkstyle.load_stylesheet_pyside2())
window = BlenderUpdater()
window.setWindowTitle(f"Overmind Studios Blender Updater {appversion}")
window.statusbar.setSizeGripEnabled(False)
window.show()
app.exec()
if __name__ == "__main__":
main()