-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.py
106 lines (82 loc) · 3.26 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
import platform
import shutil
import subprocess
import sys
from pathlib import Path
from constants import VERSION
OS_NAME, MACHINE, ARCH = sys.platform, platform.machine().lower(), platform.architecture()[0][:2]
if MACHINE in ('x86', 'x86_64', 'amd64', 'i386', 'i686'):
MACHINE = 'x86' if ARCH == '32' else ''
def main() -> None:
'Build full command list and run build process.'
opts = sys.argv[1:]
standalone = '--standalone' in opts
name, path = get_dist_path(standalone)
if not standalone and '--onefile' not in opts:
opts.extend(('--onefile', f'--output-filename={name}'))
powershell_hook = get_powershell_alias() if OS_NAME == 'win32' else ''
print(f"Building lj-dl-img v{VERSION} for {OS_NAME}{'_'.join((MACHINE,))} with options:\n{opts}\n")
print(f'Build destination:\n{path}\n')
cmd = [
*powershell_hook,
get_python_alias(),
'-m',
'nuitka',
f'--onefile-tempdir-spec={get_cache_dir()}',
f'--windows-icon-from-ico={Path("assets/logo.ico")}',
'--company-name=https://github.com/mezhgano/lj-dl-img',
f'--product-version={VERSION}',
'--file-description="Download Livejournal photo albums"',
'--copyright="[email protected] | UNLICENSE"',
f'--output-dir={path}',
*opts,
'lj_dl_img.py'
]
print(f'Running Nuitka with options:\n{cmd}\n')
run_nuitka(cmd)
def get_cache_dir() -> Path:
'''
Return Path object with cache path populated with single version number.\n
Using this instead nuitka %VERSION% token wich is combination of --file-version & --product-version.
'''
path = Path(f'%CACHE_DIR%/dmitrymeshkoff/lj-dl-img/{VERSION}')
return path
def get_dist_path(standalone: bool) -> tuple:
'Return tuple with executable name and relative path.'
name = '_'.join(filter(None, ('lj-dl-img', {'win32': '', 'darwin': 'macos'}.get(OS_NAME, OS_NAME), MACHINE)))
path = Path(f'dist/{name}')
name = ''.join(filter(None, (name, OS_NAME == 'win32' and '.exe')))
return name, path
def get_powershell_alias() -> tuple | str:
'Return Powershell alias if found in system path.'
for alias in ('pwsh', 'powershell'):
path = shutil.which(alias)
if path:
# Uncomment to pass a null input if Nuitka will asking to download something.
# return (alias, '-NoProfile', '-Command', '"$null" |')
return (alias, '-NoProfile', '-Command')
else:
# if not found pass empty string, this will lead to
# subprocess run executable directly
return ''
def get_python_alias() -> str:
'Return python alias if found in system path.'
for alias in ('py', 'python'):
path = shutil.which(alias)
if path:
return alias
else:
raise FileNotFoundError('Can\'t find python path, exiting.')
def run_nuitka(cmd: list) -> None:
'Run nuitka build in subprocess and print output.'
process = subprocess.Popen(cmd,
stdout=subprocess.PIPE,
bufsize=1,
text=True)
while True:
line = process.stdout.readline()
if not line:
break
print(line.strip())
if __name__ == '__main__':
main()