-
Notifications
You must be signed in to change notification settings - Fork 0
/
Builder.py
172 lines (138 loc) · 6.24 KB
/
Builder.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
"""
Copyright (C) 2022 Fern Lane, SonicEval (aka Pulsely) project
Licensed under the GNU Affero General Public License, Version 3.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.gnu.org/licenses/agpl-3.0.en.html
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
"""
import os
import shutil
import subprocess
import time
import platform
import SonicEval
# Remove all files before building?
CLEAN = True
# Name of main file and output directory
MAIN_FILE = 'SonicEval'
# Text to add to the spec file
SPEC_FILE_HEADER = 'import PyInstaller.config\n' \
'PyInstaller.config.CONF[\'workpath\'] = \'./build\'\n'
# Files and folders to include in final build directory (dist/MAIN_FILE folder)
INCLUDE_FILES = ['icon.png',
'examples',
'gui.ui',
'README.md',
'LICENCE']
# Files and folders to exclude from final build directory (dist/MAIN_FILE folder)
EXCLUDE_FILES = []
# *.py files to exclude from final build
EXCLUDE_FROM_BUILD = []
if __name__ == '__main__':
pyi_command = []
if CLEAN:
# Remove dist folder is exists
if 'dist' in os.listdir('./'):
shutil.rmtree('dist', ignore_errors=True)
print('dist folder deleted')
# Remove build folder is exists
if 'build' in os.listdir('./'):
shutil.rmtree('build', ignore_errors=True)
print('build folder deleted')
# Add all .py files to pyi_command
for file in os.listdir('./'):
if file.endswith('.py') and str(file) != MAIN_FILE \
and str(file) != os.path.basename(__file__) \
and str(file) not in EXCLUDE_FROM_BUILD:
pyi_command.append(str(file))
# Add main file to pyi_command
pyi_command.insert(0, MAIN_FILE + '.py')
# Add icon
pyi_command.insert(0, '--icon=./icon.ico')
# Other command arguments
pyi_command.insert(0, '--windowed')
pyi_command.insert(0, '_bootlocale')
pyi_command.insert(0, '--exclude-module')
# pyi_command.insert(0, '--onefile')
pyi_command.insert(0, 'pyi-makespec')
# Delete previous spec
if os.path.exists(MAIN_FILE + '.spec'):
os.remove(MAIN_FILE + '.spec')
# Execute pyi
print(" ".join(pyi_command))
subprocess.run(pyi_command, text=True)
# Spec file generated
if os.path.exists(MAIN_FILE + '.spec'):
with open(MAIN_FILE + '.spec', 'r') as spec_file:
# Read spec file
spec_data = spec_file.read()
spec_file.close()
# Add header to spec file
spec_data = SPEC_FILE_HEADER + spec_data
# Disable console
spec_data = spec_data.replace('console=True', 'console=False')
# spec_data = spec_data.replace('excludes=[]', 'excludes=[\'cv2\']')
# spec_data = spec_data.replace('hiddenimports=[]', 'hiddenimports=[\'cv2\']')
with open(MAIN_FILE + '.spec', 'w') as spec_file_output:
# Write updated spec file
spec_file_output.write(spec_data)
spec_file_output.close()
# Create new pyi command
pyi_command = ['pyinstaller', MAIN_FILE + '.spec']
if CLEAN:
pyi_command.append('--clean')
# Execute pyi
subprocess.run(pyi_command, text=True)
# If dist folder created
if 'dist' in os.listdir('.') and MAIN_FILE in os.listdir('./dist'):
# Remove build folder is exists
if CLEAN:
if 'build' in os.listdir('./'):
shutil.rmtree('build', ignore_errors=True)
print('build folder deleted')
# Wait some time
print('Waiting 1 second...')
time.sleep(1)
# Copy include files to it
for file in INCLUDE_FILES:
try:
if os.path.isfile(file):
shutil.copy(file, 'dist/' + MAIN_FILE + '/' + file)
elif os.path.isdir(file):
shutil.copytree(file, 'dist/' + MAIN_FILE + '/' + file)
print('Added', file, 'to dist/', MAIN_FILE, 'folder')
except Exception as e:
print('Error copying file!', e)
# Wait some time
print('Waiting 1 second...')
time.sleep(1)
# Exclude files to it
for file in EXCLUDE_FILES:
try:
if os.path.isfile('dist/' + MAIN_FILE + '/' + file):
os.remove('dist/' + MAIN_FILE + '/' + file)
elif os.path.isdir('dist/' + MAIN_FILE + '/' + file):
shutil.rmtree('dist/' + MAIN_FILE + '/' + file)
print('Removed', file, 'from dist/', MAIN_FILE, 'folder')
except Exception as e:
print('Error excluding file!', e)
# Wait some time
print('Waiting 1 second...')
time.sleep(1)
# Rename final folder
os.rename('dist/' + MAIN_FILE, 'dist/' + MAIN_FILE + '-' + SonicEval.APP_VERSION
+ '-' + str(platform.system() + '-' + str(platform.machine())))
else:
print('Error. No dist/' + MAIN_FILE + ' folder!')
# Spec file not generated
else:
print('Error generating spec!')