This repository has been archived by the owner on Jan 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathbuild_loader.py
153 lines (125 loc) · 5.23 KB
/
build_loader.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
import locale
import os
import re
import shutil
import subprocess
import tarfile
import time
import zipfile
from pathlib import Path
from subprocess import DEVNULL, PIPE, STDOUT
from urllib.request import urlopen
from PyQt5.QtCore import QThread, pyqtSignal
from _platform import get_platform
if get_platform() == 'Windows':
from subprocess import CREATE_NO_WINDOW
class BuildLoader(QThread):
finished = pyqtSignal('PyQt_PyObject')
block_abortion = pyqtSignal()
progress_changed = pyqtSignal(
'PyQt_PyObject', 'PyQt_PyObject', 'PyQt_PyObject')
def __init__(self, parent, url, strptime):
QThread.__init__(self)
self.parent = parent
self.url = url
self.strptime = strptime
self.root_folder = self.parent.settings.value('root_folder')
self.is_running = False
self.platform = get_platform()
def run(self):
self.is_running = True
blender_zip = urlopen(self.url)
size = blender_zip.info()['Content-Length']
temp_path = os.path.join(self.root_folder, "temp")
path = os.path.join(temp_path, self.url.split('/', -1)[-1])
# Create temp directory
if not os.path.isdir(temp_path):
os.makedirs(temp_path)
# Download
with open(path, 'wb') as file:
while True:
chunk = blender_zip.read(16 * 1024)
if not chunk:
break
file.write(chunk)
progress = os.stat(path).st_size / int(size)
self.progress_changed.emit(
progress, progress * 0.5, "Downloading: %p%")
if not self.is_running:
file.close()
if os.path.isdir(temp_path):
shutil.rmtree(temp_path)
self.finished.emit(None)
return
# Show extraction progress at 0
self.progress_changed.emit(0, 0, "Extracting: %p%")
# Extract
if self.platform == 'Windows':
zf = zipfile.ZipFile(path)
version = zf.infolist()[0].filename.split('/')[0]
uncompress_size = sum((file.file_size for file in zf.infolist()))
extracted_size = 0
for file in zf.infolist():
zf.extract(file, self.root_folder)
extracted_size += file.file_size
progress = extracted_size / uncompress_size
self.progress_changed.emit(
progress, progress * 0.5 + 0.5, "Extracting: %p%")
if not self.is_running:
zf.close()
shutil.rmtree(os.path.join(self.root_folder, version))
if os.path.isdir(temp_path):
shutil.rmtree(temp_path)
self.finished.emit(None)
return
zf.close()
elif self.platform == 'Linux':
tar = tarfile.open(path)
version = tar.getnames()[0].split('/')[0]
uncompress_size = sum((member.size for member in tar.getmembers()))
extracted_size = 0
for member in tar.getmembers():
tar.extract(member, path=self.root_folder)
extracted_size += member.size
progress = extracted_size / uncompress_size
self.progress_changed.emit(
progress, progress * 0.5 + 0.5, "Extracting: %p%")
if not self.is_running:
tar.close()
shutil.rmtree(os.path.join(self.root_folder, version))
if os.path.isdir(temp_path):
shutil.rmtree(temp_path)
self.finished.emit(None)
return
tar.close()
self.block_abortion.emit()
self.progress_changed.emit(0, 0, "Finishing...")
# Delete temp folder
if os.path.isdir(temp_path):
shutil.rmtree(temp_path)
if self.platform == 'Windows':
locale.setlocale(locale.LC_ALL, 'eng_usa')
elif self.platform == 'Linux':
locale.setlocale(locale.LC_ALL, 'en_US.UTF-8')
# Make nice name for dir
git = self.url.split('-',)[-2]
nice_name = "Git-%s-%s" % \
(git, time.strftime("%d-%b-%H-%M", self.strptime))
source_path = Path(os.path.join(self.root_folder, version))
target_path = Path(os.path.join(self.root_folder, nice_name))
source_path.rename(target_path)
# Register .blend extension
if self.parent.settings.value('is_register_blend', type=bool):
if self.platform == 'Windows':
b3d_exe = target_path / "blender.exe"
subprocess.call([str(b3d_exe), "-r"], creationflags=CREATE_NO_WINDOW,
shell=True, stdout=PIPE, stderr=STDOUT, stdin=DEVNULL)
elif self.platform == 'Linux':
pass
# TODO Linux has different file association method?
# b3d_exe = target_path / "blender"
# subprocess.call([str(b3d_exe), "-r"],
# shell=True, stdout=PIPE, stderr=STDOUT, stdin=DEVNULL)
self.finished.emit(nice_name)
def stop(self):
self.is_running = False