-
Notifications
You must be signed in to change notification settings - Fork 24
/
build.py
131 lines (104 loc) · 3.71 KB
/
build.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
import os
import platform
import subprocess
from pathlib import Path
from zipfile import ZIP_DEFLATED, ZipFile
from setuptools.errors import PlatformError
from src.constant import APPLICATION_VERSION, EXECUTIVE_NAME
SYSTEM = platform.system()
ARCH = platform.architecture()[0][:2]
if ARCH == '32':
raise PlatformError('x86 is not supported.')
PROJECT_DIR = Path(__file__).absolute().parent
SOURCE_DIR = Path('src')
BUILD_DIR = Path('build')
OUTPUT_DIR = BUILD_DIR
RELEASE_DIR = Path('release')
RELEASE_DIR.mkdir(exist_ok=True)
RELEASE_DIR /= APPLICATION_VERSION
RELEASE_DIR.mkdir(exist_ok=True)
def build():
nuitka_cmd = [
'nuitka',
'--clang',
'--mingw64',
# '--show-progress',
'--show-memory',
'--standalone',
'--include-data-dir=src/data=data',
'--include-data-dir=src/locale=locale',
'--include-data-file=LICENSE=LICENSE',
'--include-data-file=README.md=README.md',
'--include-data-file=README.zh_CN.md=README.zh_CN.md',
'--include-data-file=CHANGELOG.md=CHANGELOG.md',
'--include-data-file=CHANGELOG.zh_CN.md=CHANGELOG.zh_CN.md',
'--plugin-enable=tk-inter',
f'--output-dir={OUTPUT_DIR}'
]
if platform.system() == 'Windows':
nuitka_cmd.extend(
(
'--windows-disable-console',
'--windows-icon-from-ico=src/data/PDFeXpress.ico',
)
)
nuitka_cmd.append(SOURCE_DIR / f'{EXECUTIVE_NAME}.py')
process = subprocess.run(nuitka_cmd, shell=True)
if process.returncode != 0:
raise ChildProcessError('Nuitka building failed.')
print('Building done.')
def create_portable():
dist_dir = OUTPUT_DIR / f'{EXECUTIVE_NAME}.dist'
file_list = dist_dir.glob('**/*')
# file_list.sort()
portable_file = RELEASE_DIR / f'{EXECUTIVE_NAME}-{APPLICATION_VERSION}-Portable-{SYSTEM}-{ARCH}.zip'
print('Creating portable package...')
with ZipFile(portable_file, 'w', compression=ZIP_DEFLATED) as zf:
for file in file_list:
name_in_zip = file.relative_to(dist_dir)
print(name_in_zip)
if file.is_file():
zf.write(file, name_in_zip)
print('Creating portable package done.')
def update_iss():
settings = {
'APP_VERSION': APPLICATION_VERSION,
'PROJECT_DIR': str(PROJECT_DIR),
'OUTPUT_DIR': str(OUTPUT_DIR),
'RELEASE_DIR': str(RELEASE_DIR),
'ARCH': ARCH,
'ARCH_MODE': 'ArchitecturesInstallIn64BitMode=x64' if ARCH == '64' else ''
}
iss_template = f'{EXECUTIVE_NAME}-template.iss'
iss_work = Path(BUILD_DIR) / f'{EXECUTIVE_NAME}-{ARCH}.iss'
with open(iss_template) as template:
iss_script = template.read()
for key in settings:
iss_script = iss_script.replace(f'%%{key}%%', settings.get(key))
with open(iss_work, 'w') as iss:
iss.write(iss_script)
return iss_work
def check_iss():
if ARCH == '64':
program_files = os.environ.get('ProgramFiles(x86)')
else:
program_files = os.environ.get('ProgramFiles')
iss_compiler = Path(program_files) / 'Inno Setup 6' / 'Compil32.exe'
if iss_compiler.exists():
return iss_compiler
return None
def create_setup():
iss_work = update_iss()
iss_compiler = check_iss()
if iss_compiler:
print('Creating Windows Installer...', end='')
compiler_cmd = [str(iss_compiler), '/cc', str(iss_work)]
process = subprocess.run(compiler_cmd)
if process.returncode != 0:
raise ChildProcessError('Creating Windows installer failed.')
print('done')
if __name__ == '__main__':
build()
create_portable()
if SYSTEM == 'Windows':
create_setup()