]> jfr.im git - yt-dlp.git/blame - pyinst.py
[extractor] Fix format sorting of `channels`
[yt-dlp.git] / pyinst.py
CommitLineData
cc52de43 1#!/usr/bin/env python3
54007a45 2
115add43 3# Allow direct execution
733d8e8f 4import os
733d8e8f 5import sys
0e5927ee 6
115add43 7sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
8
9import platform
10
c1714454 11from PyInstaller.__main__ import run as run_pyinstaller
733d8e8f 12
115add43 13from devscripts.utils import read_version
14
d08e1e68 15OS_NAME, MACHINE, ARCH = sys.platform, platform.machine(), platform.architecture()[0][:2]
16if 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 ''
e38df8f9 19
0e5927ee 20
733d8e8f 21def main():
115add43 22 opts, version = parse_options(), read_version()
c1714454 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')
5d535b4a 27
b5899f4f 28 name, final_file = exe(onedir)
52009769 29 print(f'Building yt-dlp v{version} for {OS_NAME} {platform.machine()} with options {opts}')
49a57e70 30 print('Remember to update the version using "devscripts/update-version.py"')
733d8e8f 31 if not os.path.isfile('yt_dlp/extractor/lazy_extractors.py'):
32 print('WARNING: Building without lazy_extractors. Run '
49a57e70 33 '"devscripts/make_lazy_extractors.py" to build lazy extractors', file=sys.stderr)
733d8e8f 34 print(f'Destination: {final_file}\n')
e38df8f9 35
733d8e8f 36 opts = [
e1e1ea54 37 f'--name={name}',
733d8e8f 38 '--icon=devscripts/logo.ico',
39 '--upx-exclude=vcruntime140.dll',
40 '--noconfirm',
e75bb0d6 41 *dependency_options(),
733d8e8f 42 *opts,
43 'yt_dlp/__main__.py',
44 ]
733d8e8f 45
c1714454 46 print(f'Running PyInstaller with {opts}')
47 run_pyinstaller(opts)
733d8e8f 48 set_version_info(final_file, version)
49
50
51def parse_options():
962ffcf8 52 # Compatibility with older arguments
733d8e8f 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:]
c1714454 58 return opts
e38df8f9 59
733d8e8f 60
b5899f4f 61def exe(onedir):
62 """@returns (name, path)"""
63 name = '_'.join(filter(None, (
64 'yt-dlp',
e4afcfde 65 {'win32': '', 'darwin': 'macos'}.get(OS_NAME, OS_NAME),
52009769 66 MACHINE
b5899f4f 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
733d8e8f 76def version_to_list(version):
77 version_list = version.split('.')
78 return list(map(int, version_list)) + [0] * (4 - len(version_list))
79
80
e75bb0d6 81def dependency_options():
c1714454 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 = ['test', 'ytdlp_plugins', 'youtube_dl', 'youtube_dlc']
733d8e8f 85
e75bb0d6 86 yield from (f'--hidden-import={module}' for module in dependencies)
c1714454 87 yield '--collect-submodules=websockets'
733d8e8f 88 yield from (f'--exclude-module={module}' for module in excluded_modules)
e38df8f9 89
49e7e9c3 90
91def 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
733d8e8f 105def set_version_info(exe, version):
1890fc63 106 if OS_NAME == 'win32':
733d8e8f 107 windows_set_version(exe, version)
108
109
110def windows_set_version(exe, version):
b5899f4f 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
733d8e8f 122 version_list = version_to_list(version)
52009769 123 suffix = MACHINE and f'_{MACHINE}'
733d8e8f 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', [
52009769 137 StringStruct('Comments', 'yt-dlp%s Command Line Interface' % suffix),
733d8e8f 138 StringStruct('CompanyName', 'https://github.com/yt-dlp'),
52009769 139 StringStruct('FileDescription', 'yt-dlp%s' % (MACHINE and f' ({MACHINE})')),
733d8e8f 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 ))
e36d50c5 150
0e5927ee 151
733d8e8f 152if __name__ == '__main__':
153 main()