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