]> jfr.im git - yt-dlp.git/blame - pyinst.py
[build] Refactor `pyinst.py` and misc cleanup
[yt-dlp.git] / pyinst.py
CommitLineData
cc52de43 1#!/usr/bin/env python3
ff88a05c 2# coding: utf-8
733d8e8f 3import os
ff88a05c 4import platform
733d8e8f 5import sys
e36d50c5 6from PyInstaller.utils.hooks import collect_submodules
0e5927ee 7
733d8e8f 8
9OS_NAME = platform.system()
10if OS_NAME == 'Windows':
0e5927ee
R
11 from PyInstaller.utils.win32.versioninfo import (
12 VarStruct, VarFileInfo, StringStruct, StringTable,
13 StringFileInfo, FixedFileInfo, VSVersionInfo, SetVersion,
14 )
733d8e8f 15elif OS_NAME == 'Darwin':
16 pass
17else:
18 raise Exception('{OS_NAME} is not supported')
e38df8f9 19
733d8e8f 20ARCH = platform.architecture()[0][:2]
e38df8f9 21
0e5927ee 22
733d8e8f 23def main():
24 opts = parse_options()
25 version = read_version()
5d535b4a 26
733d8e8f 27 suffix = '_x86' if ARCH == '32' else '_macos' if OS_NAME == 'Darwin' else ''
28 final_file = 'dist/%syt-dlp%s%s' % (
29 'yt-dlp/' if '--onedir' in opts else '', suffix, '.exe' if OS_NAME == 'Windows' else '')
4c88ff87 30
733d8e8f 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" "yt_dlp/extractor/lazy_extractors.py" '
36 'to build lazy extractors', file=sys.stderr)
37 print(f'Destination: {final_file}\n')
e38df8f9 38
733d8e8f 39 opts = [
40 f'--name=yt-dlp{suffix}',
41 '--icon=devscripts/logo.ico',
42 '--upx-exclude=vcruntime140.dll',
43 '--noconfirm',
44 *dependancy_options(),
45 *opts,
46 'yt_dlp/__main__.py',
47 ]
48 print(f'Running PyInstaller with {opts}')
e38df8f9 49
733d8e8f 50 import PyInstaller.__main__
e38df8f9 51
733d8e8f 52 PyInstaller.__main__.run(opts)
53
54 set_version_info(final_file, version)
55
56
57def parse_options():
58 # Compatability with older arguments
59 opts = sys.argv[1:]
60 if opts[0:1] in (['32'], ['64']):
61 if ARCH != opts[0]:
62 raise Exception(f'{opts[0]}bit executable cannot be built on a {ARCH}bit system')
63 opts = opts[1:]
64 return opts or ['--onefile']
e38df8f9 65
733d8e8f 66
67def read_version():
68 exec(compile(open('yt_dlp/version.py').read(), 'yt_dlp/version.py', 'exec'))
69 return locals()['__version__']
70
71
72def version_to_list(version):
73 version_list = version.split('.')
74 return list(map(int, version_list)) + [0] * (4 - len(version_list))
75
76
77def dependancy_options():
78 dependancies = [pycryptodome_module(), 'mutagen'] + collect_submodules('websockets')
79 excluded_modules = ['test', 'ytdlp_plugins', 'youtube-dl', 'youtube-dlc']
80
81 yield from (f'--hidden-import={module}' for module in dependancies)
82 yield from (f'--exclude-module={module}' for module in excluded_modules)
e38df8f9 83
49e7e9c3 84
85def pycryptodome_module():
86 try:
87 import Cryptodome # noqa: F401
88 except ImportError:
89 try:
90 import Crypto # noqa: F401
91 print('WARNING: Using Crypto since Cryptodome is not available. '
92 'Install with: pip install pycryptodomex', file=sys.stderr)
93 return 'Crypto'
94 except ImportError:
95 pass
96 return 'Cryptodome'
97
98
733d8e8f 99def set_version_info(exe, version):
100 if OS_NAME == 'Windows':
101 windows_set_version(exe, version)
102
103
104def windows_set_version(exe, version):
105 version_list = version_to_list(version)
106 suffix = '_x86' if ARCH == '32' else ''
107 SetVersion(exe, VSVersionInfo(
108 ffi=FixedFileInfo(
109 filevers=version_list,
110 prodvers=version_list,
111 mask=0x3F,
112 flags=0x0,
113 OS=0x4,
114 fileType=0x1,
115 subtype=0x0,
116 date=(0, 0),
117 ),
118 kids=[
119 StringFileInfo([StringTable('040904B0', [
120 StringStruct('Comments', 'yt-dlp%s Command Line Interface.' % suffix),
121 StringStruct('CompanyName', 'https://github.com/yt-dlp'),
122 StringStruct('FileDescription', 'yt-dlp%s' % (' (32 Bit)' if ARCH == '32' else '')),
123 StringStruct('FileVersion', version),
124 StringStruct('InternalName', f'yt-dlp{suffix}'),
125 StringStruct('LegalCopyright', 'pukkandan.ytdlp@gmail.com | UNLICENSE'),
126 StringStruct('OriginalFilename', f'yt-dlp{suffix}.exe'),
127 StringStruct('ProductName', f'yt-dlp{suffix}'),
128 StringStruct(
129 'ProductVersion', f'{version}{suffix} on Python {platform.python_version()}'),
130 ])]), VarFileInfo([VarStruct('Translation', [0, 1200])])
131 ]
132 ))
e36d50c5 133
0e5927ee 134
733d8e8f 135if __name__ == '__main__':
136 main()