]> jfr.im git - yt-dlp.git/blob - pyinst.py
[build] Fix architecture suffix of executables (#4355)
[yt-dlp.git] / pyinst.py
1 #!/usr/bin/env python3
2
3 import os
4 import platform
5 import sys
6
7 from PyInstaller.__main__ import run as run_pyinstaller
8
9 OS_NAME, MACHINE = sys.platform, platform.machine()
10 if MACHINE in ('x86_64', 'amd64'):
11 MACHINE = ''
12 elif 'i' in MACHINE and '86' in MACHINE:
13 MACHINE = 'x86'
14
15
16 def main():
17 opts = parse_options()
18 version = read_version('yt_dlp/version.py')
19
20 onedir = '--onedir' in opts or '-D' in opts
21 if not onedir and '-F' not in opts and '--onefile' not in opts:
22 opts.append('--onefile')
23
24 name, final_file = exe(onedir)
25 print(f'Building yt-dlp v{version} for {OS_NAME} {platform.machine()} with options {opts}')
26 print('Remember to update the version using "devscripts/update-version.py"')
27 if not os.path.isfile('yt_dlp/extractor/lazy_extractors.py'):
28 print('WARNING: Building without lazy_extractors. Run '
29 '"devscripts/make_lazy_extractors.py" to build lazy extractors', file=sys.stderr)
30 print(f'Destination: {final_file}\n')
31
32 opts = [
33 f'--name={name}',
34 '--icon=devscripts/logo.ico',
35 '--upx-exclude=vcruntime140.dll',
36 '--noconfirm',
37 # NB: Modules that are only imported dynamically must be added here.
38 # --collect-submodules may not work correctly if user has a yt-dlp installed via PIP
39 '--hidden-import=yt_dlp.compat._legacy',
40 *dependency_options(),
41 *opts,
42 'yt_dlp/__main__.py',
43 ]
44
45 print(f'Running PyInstaller with {opts}')
46 run_pyinstaller(opts)
47 set_version_info(final_file, version)
48
49
50 def parse_options():
51 # Compatibility with older arguments
52 opts = sys.argv[1:]
53 if opts[0:1] in (['32'], ['64']):
54 ARCH = platform.architecture()[0][:2]
55 if ARCH != opts[0]:
56 raise Exception(f'{opts[0]}bit executable cannot be built on a {ARCH}bit system')
57 opts = opts[1:]
58 return opts
59
60
61 # Get the version from yt_dlp/version.py without importing the package
62 def read_version(fname):
63 with open(fname, encoding='utf-8') as f:
64 exec(compile(f.read(), fname, 'exec'))
65 return locals()['__version__']
66
67
68 def exe(onedir):
69 """@returns (name, path)"""
70 name = '_'.join(filter(None, (
71 'yt-dlp',
72 {'win32': '', 'darwin': 'macos'}.get(OS_NAME, OS_NAME),
73 MACHINE
74 )))
75 return name, ''.join(filter(None, (
76 'dist/',
77 onedir and f'{name}/',
78 name,
79 OS_NAME == 'win32' and '.exe'
80 )))
81
82
83 def version_to_list(version):
84 version_list = version.split('.')
85 return list(map(int, version_list)) + [0] * (4 - len(version_list))
86
87
88 def dependency_options():
89 # Due to the current implementation, these are auto-detected, but explicitly add them just in case
90 dependencies = [pycryptodome_module(), 'mutagen', 'brotli', 'certifi', 'websockets']
91 excluded_modules = ['test', 'ytdlp_plugins', 'youtube_dl', 'youtube_dlc']
92
93 yield from (f'--hidden-import={module}' for module in dependencies)
94 yield '--collect-submodules=websockets'
95 yield from (f'--exclude-module={module}' for module in excluded_modules)
96
97
98 def pycryptodome_module():
99 try:
100 import Cryptodome # noqa: F401
101 except ImportError:
102 try:
103 import Crypto # noqa: F401
104 print('WARNING: Using Crypto since Cryptodome is not available. '
105 'Install with: pip install pycryptodomex', file=sys.stderr)
106 return 'Crypto'
107 except ImportError:
108 pass
109 return 'Cryptodome'
110
111
112 def set_version_info(exe, version):
113 if OS_NAME == 'win32':
114 windows_set_version(exe, version)
115
116
117 def windows_set_version(exe, version):
118 from PyInstaller.utils.win32.versioninfo import (
119 FixedFileInfo,
120 SetVersion,
121 StringFileInfo,
122 StringStruct,
123 StringTable,
124 VarFileInfo,
125 VarStruct,
126 VSVersionInfo,
127 )
128
129 version_list = version_to_list(version)
130 suffix = MACHINE and f'_{MACHINE}'
131 SetVersion(exe, VSVersionInfo(
132 ffi=FixedFileInfo(
133 filevers=version_list,
134 prodvers=version_list,
135 mask=0x3F,
136 flags=0x0,
137 OS=0x4,
138 fileType=0x1,
139 subtype=0x0,
140 date=(0, 0),
141 ),
142 kids=[
143 StringFileInfo([StringTable('040904B0', [
144 StringStruct('Comments', 'yt-dlp%s Command Line Interface' % suffix),
145 StringStruct('CompanyName', 'https://github.com/yt-dlp'),
146 StringStruct('FileDescription', 'yt-dlp%s' % (MACHINE and f' ({MACHINE})')),
147 StringStruct('FileVersion', version),
148 StringStruct('InternalName', f'yt-dlp{suffix}'),
149 StringStruct('LegalCopyright', 'pukkandan.ytdlp@gmail.com | UNLICENSE'),
150 StringStruct('OriginalFilename', f'yt-dlp{suffix}.exe'),
151 StringStruct('ProductName', f'yt-dlp{suffix}'),
152 StringStruct(
153 'ProductVersion', f'{version}{suffix} on Python {platform.python_version()}'),
154 ])]), VarFileInfo([VarStruct('Translation', [0, 1200])])
155 ]
156 ))
157
158
159 if __name__ == '__main__':
160 main()