]> jfr.im git - yt-dlp.git/blob - pyinst.py
Allow extractors to specify section_start/end for clips
[yt-dlp.git] / pyinst.py
1 #!/usr/bin/env python3
2 import os
3 import platform
4 import sys
5
6 from PyInstaller.__main__ import run as run_pyinstaller
7
8 OS_NAME, ARCH = sys.platform, platform.architecture()[0][:2]
9
10
11 def main():
12 opts = parse_options()
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')
18
19 name, final_file = exe(onedir)
20 print(f'Building yt-dlp v{version} {ARCH}bit for {OS_NAME} with options {opts}')
21 print('Remember to update the version using "devscripts/update-version.py"')
22 if not os.path.isfile('yt_dlp/extractor/lazy_extractors.py'):
23 print('WARNING: Building without lazy_extractors. Run '
24 '"devscripts/make_lazy_extractors.py" to build lazy extractors', file=sys.stderr)
25 print(f'Destination: {final_file}\n')
26
27 opts = [
28 f'--name={name}',
29 '--icon=devscripts/logo.ico',
30 '--upx-exclude=vcruntime140.dll',
31 '--noconfirm',
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',
35 *dependency_options(),
36 *opts,
37 'yt_dlp/__main__.py',
38 ]
39
40 print(f'Running PyInstaller with {opts}')
41 run_pyinstaller(opts)
42 set_version_info(final_file, version)
43
44
45 def 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:]
52 return opts
53
54
55 # Get the version from yt_dlp/version.py without importing the package
56 def read_version(fname):
57 with open(fname, encoding='utf-8') as f:
58 exec(compile(f.read(), fname, 'exec'))
59 return locals()['__version__']
60
61
62 def exe(onedir):
63 """@returns (name, path)"""
64 name = '_'.join(filter(None, (
65 'yt-dlp',
66 {'win32': '', 'darwin': 'macos'}.get(OS_NAME, OS_NAME),
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
77 def version_to_list(version):
78 version_list = version.split('.')
79 return list(map(int, version_list)) + [0] * (4 - len(version_list))
80
81
82 def dependency_options():
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']
86
87 yield from (f'--hidden-import={module}' for module in dependencies)
88 yield '--collect-submodules=websockets'
89 yield from (f'--exclude-module={module}' for module in excluded_modules)
90
91
92 def 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
106 def set_version_info(exe, version):
107 if OS_NAME == 'win32':
108 windows_set_version(exe, version)
109
110
111 def windows_set_version(exe, version):
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
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 ))
151
152
153 if __name__ == '__main__':
154 main()