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