[wget] Fix proxy (#3152)
[yt-dlp3.git] / pyinst.py
blobca115fd78fbab1ef3db9ab2e2f71ee4f1cb05e2a
1 #!/usr/bin/env python3
2 # coding: utf-8
3 import os
4 import platform
5 import sys
6 from PyInstaller.utils.hooks import collect_submodules
9 OS_NAME = platform.system()
10 if OS_NAME == 'Windows':
11 from PyInstaller.utils.win32.versioninfo import (
12 VarStruct, VarFileInfo, StringStruct, StringTable,
13 StringFileInfo, FixedFileInfo, VSVersionInfo, SetVersion,
15 elif OS_NAME == 'Darwin':
16 pass
17 else:
18 raise Exception('{OS_NAME} is not supported')
20 ARCH = platform.architecture()[0][:2]
23 def main():
24 opts = parse_options()
25 version = read_version()
27 suffix = '_macos' if OS_NAME == 'Darwin' else '_x86' if ARCH == '32' else ''
28 final_file = 'dist/%syt-dlp%s%s' % (
29 'yt-dlp/' if '--onedir' in opts else '', suffix, '.exe' if OS_NAME == 'Windows' else '')
31 print(f'Building yt-dlp v{version} {ARCH}bit for {OS_NAME} with options {opts}')
32 print('Remember to update the version using "devscripts/update-version.py"')
33 if not os.path.isfile('yt_dlp/extractor/lazy_extractors.py'):
34 print('WARNING: Building without lazy_extractors. Run '
35 '"devscripts/make_lazy_extractors.py" to build lazy extractors', file=sys.stderr)
36 print(f'Destination: {final_file}\n')
38 opts = [
39 f'--name=yt-dlp{suffix}',
40 '--icon=devscripts/logo.ico',
41 '--upx-exclude=vcruntime140.dll',
42 '--noconfirm',
43 *dependency_options(),
44 *opts,
45 'yt_dlp/__main__.py',
47 print(f'Running PyInstaller with {opts}')
49 import PyInstaller.__main__
51 PyInstaller.__main__.run(opts)
53 set_version_info(final_file, version)
56 def parse_options():
57 # Compatability with older arguments
58 opts = sys.argv[1:]
59 if opts[0:1] in (['32'], ['64']):
60 if ARCH != opts[0]:
61 raise Exception(f'{opts[0]}bit executable cannot be built on a {ARCH}bit system')
62 opts = opts[1:]
63 return opts or ['--onefile']
66 def read_version():
67 exec(compile(open('yt_dlp/version.py').read(), 'yt_dlp/version.py', 'exec'))
68 return locals()['__version__']
71 def version_to_list(version):
72 version_list = version.split('.')
73 return list(map(int, version_list)) + [0] * (4 - len(version_list))
76 def dependency_options():
77 dependencies = [pycryptodome_module(), 'mutagen', 'brotli'] + collect_submodules('websockets')
78 excluded_modules = ['test', 'ytdlp_plugins', 'youtube-dl', 'youtube-dlc']
80 yield from (f'--hidden-import={module}' for module in dependencies)
81 yield from (f'--exclude-module={module}' for module in excluded_modules)
84 def pycryptodome_module():
85 try:
86 import Cryptodome # noqa: F401
87 except ImportError:
88 try:
89 import Crypto # noqa: F401
90 print('WARNING: Using Crypto since Cryptodome is not available. '
91 'Install with: pip install pycryptodomex', file=sys.stderr)
92 return 'Crypto'
93 except ImportError:
94 pass
95 return 'Cryptodome'
98 def set_version_info(exe, version):
99 if OS_NAME == 'Windows':
100 windows_set_version(exe, version)
103 def windows_set_version(exe, version):
104 version_list = version_to_list(version)
105 suffix = '_x86' if ARCH == '32' else ''
106 SetVersion(exe, VSVersionInfo(
107 ffi=FixedFileInfo(
108 filevers=version_list,
109 prodvers=version_list,
110 mask=0x3F,
111 flags=0x0,
112 OS=0x4,
113 fileType=0x1,
114 subtype=0x0,
115 date=(0, 0),
117 kids=[
118 StringFileInfo([StringTable('040904B0', [
119 StringStruct('Comments', 'yt-dlp%s Command Line Interface.' % suffix),
120 StringStruct('CompanyName', 'https://github.com/yt-dlp'),
121 StringStruct('FileDescription', 'yt-dlp%s' % (' (32 Bit)' if ARCH == '32' else '')),
122 StringStruct('FileVersion', version),
123 StringStruct('InternalName', f'yt-dlp{suffix}'),
124 StringStruct('LegalCopyright', 'pukkandan.ytdlp@gmail.com | UNLICENSE'),
125 StringStruct('OriginalFilename', f'yt-dlp{suffix}.exe'),
126 StringStruct('ProductName', f'yt-dlp{suffix}'),
127 StringStruct(
128 'ProductVersion', f'{version}{suffix} on Python {platform.python_version()}'),
129 ])]), VarFileInfo([VarStruct('Translation', [0, 1200])])
134 if __name__ == '__main__':
135 main()