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