]> jfr.im git - yt-dlp.git/blame - pyinst.py
Populate `filename` and `urls` fields at all stages of `--print`
[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
17fc3dc4
M
15OS_NAME, MACHINE, ARCH = sys.platform, platform.machine().lower(), platform.architecture()[0][:2]
16if MACHINE in ('x86', 'x86_64', 'amd64', 'i386', 'i686'):
d08e1e68 17 MACHINE = 'x86' if ARCH == '32' else ''
e38df8f9 18
0e5927ee 19
733d8e8f 20def main():
115add43 21 opts, version = parse_options(), read_version()
c1714454 22
23 onedir = '--onedir' in opts or '-D' in opts
24 if not onedir and '-F' not in opts and '--onefile' not in opts:
25 opts.append('--onefile')
5d535b4a 26
b5899f4f 27 name, final_file = exe(onedir)
52009769 28 print(f'Building yt-dlp v{version} for {OS_NAME} {platform.machine()} with options {opts}')
49a57e70 29 print('Remember to update the version using "devscripts/update-version.py"')
733d8e8f 30 if not os.path.isfile('yt_dlp/extractor/lazy_extractors.py'):
31 print('WARNING: Building without lazy_extractors. Run '
49a57e70 32 '"devscripts/make_lazy_extractors.py" to build lazy extractors', file=sys.stderr)
733d8e8f 33 print(f'Destination: {final_file}\n')
e38df8f9 34
733d8e8f 35 opts = [
e1e1ea54 36 f'--name={name}',
733d8e8f 37 '--icon=devscripts/logo.ico',
38 '--upx-exclude=vcruntime140.dll',
39 '--noconfirm',
acb1042a 40 '--additional-hooks-dir=yt_dlp/__pyinstaller',
733d8e8f 41 *opts,
42 'yt_dlp/__main__.py',
43 ]
733d8e8f 44
c1714454 45 print(f'Running PyInstaller with {opts}')
46 run_pyinstaller(opts)
733d8e8f 47 set_version_info(final_file, version)
48
49
50def parse_options():
962ffcf8 51 # Compatibility with older arguments
733d8e8f 52 opts = sys.argv[1:]
53 if opts[0:1] in (['32'], ['64']):
54 if ARCH != opts[0]:
55 raise Exception(f'{opts[0]}bit executable cannot be built on a {ARCH}bit system')
56 opts = opts[1:]
c1714454 57 return opts
e38df8f9 58
733d8e8f 59
b5899f4f 60def exe(onedir):
61 """@returns (name, path)"""
62 name = '_'.join(filter(None, (
63 'yt-dlp',
e4afcfde 64 {'win32': '', 'darwin': 'macos'}.get(OS_NAME, OS_NAME),
17fc3dc4 65 MACHINE,
b5899f4f 66 )))
67 return name, ''.join(filter(None, (
68 'dist/',
69 onedir and f'{name}/',
70 name,
71 OS_NAME == 'win32' and '.exe'
72 )))
73
74
733d8e8f 75def version_to_list(version):
76 version_list = version.split('.')
77 return list(map(int, version_list)) + [0] * (4 - len(version_list))
78
79
733d8e8f 80def set_version_info(exe, version):
1890fc63 81 if OS_NAME == 'win32':
733d8e8f 82 windows_set_version(exe, version)
83
84
85def windows_set_version(exe, version):
b5899f4f 86 from PyInstaller.utils.win32.versioninfo import (
87 FixedFileInfo,
b5899f4f 88 StringFileInfo,
89 StringStruct,
90 StringTable,
91 VarFileInfo,
92 VarStruct,
93 VSVersionInfo,
94 )
95
2e269bd9 96 try:
97 from PyInstaller.utils.win32.versioninfo import SetVersion
98 except ImportError: # Pyinstaller >= 5.8
99 from PyInstaller.utils.win32.versioninfo import write_version_info_to_executable as SetVersion
100
733d8e8f 101 version_list = version_to_list(version)
52009769 102 suffix = MACHINE and f'_{MACHINE}'
733d8e8f 103 SetVersion(exe, VSVersionInfo(
104 ffi=FixedFileInfo(
105 filevers=version_list,
106 prodvers=version_list,
107 mask=0x3F,
108 flags=0x0,
109 OS=0x4,
110 fileType=0x1,
111 subtype=0x0,
112 date=(0, 0),
113 ),
114 kids=[
115 StringFileInfo([StringTable('040904B0', [
52009769 116 StringStruct('Comments', 'yt-dlp%s Command Line Interface' % suffix),
733d8e8f 117 StringStruct('CompanyName', 'https://github.com/yt-dlp'),
52009769 118 StringStruct('FileDescription', 'yt-dlp%s' % (MACHINE and f' ({MACHINE})')),
733d8e8f 119 StringStruct('FileVersion', version),
120 StringStruct('InternalName', f'yt-dlp{suffix}'),
121 StringStruct('LegalCopyright', 'pukkandan.ytdlp@gmail.com | UNLICENSE'),
122 StringStruct('OriginalFilename', f'yt-dlp{suffix}.exe'),
123 StringStruct('ProductName', f'yt-dlp{suffix}'),
124 StringStruct(
125 'ProductVersion', f'{version}{suffix} on Python {platform.python_version()}'),
126 ])]), VarFileInfo([VarStruct('Translation', [0, 1200])])
127 ]
128 ))
e36d50c5 129
0e5927ee 130
733d8e8f 131if __name__ == '__main__':
132 main()