]> jfr.im git - yt-dlp.git/blob - yt_dlp/update.py
[build] Use `workflow_dispatch` for release
[yt-dlp.git] / yt_dlp / update.py
1 from __future__ import unicode_literals
2
3 import hashlib
4 import json
5 import os
6 import platform
7 import subprocess
8 import sys
9 import traceback
10 from zipimport import zipimporter
11
12 from .compat import compat_realpath
13 from .utils import encode_compat_str, Popen
14
15 from .version import __version__
16
17
18 ''' # Not signed
19 def rsa_verify(message, signature, key):
20 from hashlib import sha256
21 assert isinstance(message, bytes)
22 byte_size = (len(bin(key[0])) - 2 + 8 - 1) // 8
23 signature = ('%x' % pow(int(signature, 16), key[1], key[0])).encode()
24 signature = (byte_size * 2 - len(signature)) * b'0' + signature
25 asn1 = b'3031300d060960864801650304020105000420'
26 asn1 += sha256(message).hexdigest().encode()
27 if byte_size < len(asn1) // 2 + 11:
28 return False
29 expected = b'0001' + (byte_size - len(asn1) // 2 - 3) * b'ff' + b'00' + asn1
30 return expected == signature
31 '''
32
33
34 def detect_variant():
35 if hasattr(sys, 'frozen'):
36 prefix = 'mac' if sys.platform == 'darwin' else 'win'
37 if getattr(sys, '_MEIPASS', None):
38 if sys._MEIPASS == os.path.dirname(sys.executable):
39 return f'{prefix}_dir'
40 return f'{prefix}_exe'
41 return 'py2exe'
42 elif isinstance(globals().get('__loader__'), zipimporter):
43 return 'zip'
44 elif os.path.basename(sys.argv[0]) == '__main__.py':
45 return 'source'
46 return 'unknown'
47
48
49 _NON_UPDATEABLE_REASONS = {
50 'win_exe': None,
51 'zip': None,
52 'mac_exe': None,
53 'py2exe': None,
54 'win_dir': 'Auto-update is not supported for unpackaged windows executable; Re-download the latest release',
55 'mac_dir': 'Auto-update is not supported for unpackaged MacOS executable; Re-download the latest release',
56 'source': 'You cannot update when running from source code; Use git to pull the latest changes',
57 'unknown': 'It looks like you installed yt-dlp with a package manager, pip, setup.py or a tarball; Use that to update',
58 }
59
60
61 def is_non_updateable():
62 return _NON_UPDATEABLE_REASONS.get(detect_variant(), _NON_UPDATEABLE_REASONS['unknown'])
63
64
65 def run_update(ydl):
66 """
67 Update the program file with the latest version from the repository
68 Returns whether the program should terminate
69 """
70
71 JSON_URL = 'https://api.github.com/repos/yt-dlp/yt-dlp/releases/latest'
72
73 def report_error(msg, expected=False):
74 ydl.report_error(msg, tb='' if expected else None)
75
76 def report_unable(action, expected=False):
77 report_error(f'Unable to {action}', expected)
78
79 def report_permission_error(file):
80 report_unable(f'write to {file}; Try running as administrator', True)
81
82 def report_network_error(action, delim=';'):
83 report_unable(f'{action}{delim} Visit https://github.com/yt-dlp/yt-dlp/releases/latest', True)
84
85 def calc_sha256sum(path):
86 h = hashlib.sha256()
87 b = bytearray(128 * 1024)
88 mv = memoryview(b)
89 with open(os.path.realpath(path), 'rb', buffering=0) as f:
90 for n in iter(lambda: f.readinto(mv), 0):
91 h.update(mv[:n])
92 return h.hexdigest()
93
94 # Download and check versions info
95 try:
96 version_info = ydl._opener.open(JSON_URL).read().decode('utf-8')
97 version_info = json.loads(version_info)
98 except Exception:
99 return report_network_error('obtain version info', delim='; Please try again later or')
100
101 def version_tuple(version_str):
102 return tuple(map(int, version_str.split('.')))
103
104 version_id = version_info['tag_name']
105 ydl.to_screen(f'Latest version: {version_id}, Current version: {__version__}')
106 if version_tuple(__version__) >= version_tuple(version_id):
107 ydl.to_screen(f'yt-dlp is up to date ({__version__})')
108 return
109
110 err = is_non_updateable()
111 if err:
112 ydl.to_screen(f'Latest version: {version_id}, Current version: {__version__}')
113 return report_error(err, True)
114
115 # sys.executable is set to the full pathname of the exe-file for py2exe
116 # though symlinks are not followed so that we need to do this manually
117 # with help of realpath
118 filename = compat_realpath(sys.executable if hasattr(sys, 'frozen') else sys.argv[0])
119 ydl.to_screen(f'Current version {__version__}; Build Hash {calc_sha256sum(filename)}')
120 ydl.to_screen(f'Updating to version {version_id} ...')
121
122 version_labels = {
123 'zip_3': '',
124 'win_exe_64': '.exe',
125 'py2exe_64': '_min.exe',
126 'win_exe_32': '_x86.exe',
127 'mac_exe_64': '_macos',
128 }
129
130 def get_bin_info(bin_or_exe, version):
131 label = version_labels['%s_%s' % (bin_or_exe, version)]
132 return next((i for i in version_info['assets'] if i['name'] == 'yt-dlp%s' % label), {})
133
134 def get_sha256sum(bin_or_exe, version):
135 filename = 'yt-dlp%s' % version_labels['%s_%s' % (bin_or_exe, version)]
136 urlh = next(
137 (i for i in version_info['assets'] if i['name'] in ('SHA2-256SUMS')),
138 {}).get('browser_download_url')
139 if not urlh:
140 return None
141 hash_data = ydl._opener.open(urlh).read().decode('utf-8')
142 return dict(ln.split()[::-1] for ln in hash_data.splitlines()).get(filename)
143
144 if not os.access(filename, os.W_OK):
145 return report_permission_error(filename)
146
147 # PyInstaller
148 variant = detect_variant()
149 if variant in ('win_exe', 'py2exe'):
150 directory = os.path.dirname(filename)
151 if not os.access(directory, os.W_OK):
152 return report_permission_error(directory)
153 try:
154 if os.path.exists(filename + '.old'):
155 os.remove(filename + '.old')
156 except (IOError, OSError):
157 return report_unable('remove the old version')
158
159 try:
160 arch = platform.architecture()[0][:2]
161 url = get_bin_info(variant, arch).get('browser_download_url')
162 if not url:
163 return report_network_error('fetch updates')
164 urlh = ydl._opener.open(url)
165 newcontent = urlh.read()
166 urlh.close()
167 except (IOError, OSError):
168 return report_network_error('download latest version')
169
170 try:
171 with open(filename + '.new', 'wb') as outf:
172 outf.write(newcontent)
173 except (IOError, OSError):
174 return report_permission_error(f'{filename}.new')
175
176 expected_sum = get_sha256sum(variant, arch)
177 if not expected_sum:
178 ydl.report_warning('no hash information found for the release')
179 elif calc_sha256sum(filename + '.new') != expected_sum:
180 report_network_error('verify the new executable')
181 try:
182 os.remove(filename + '.new')
183 except OSError:
184 return report_unable('remove corrupt download')
185
186 try:
187 os.rename(filename, filename + '.old')
188 except (IOError, OSError):
189 return report_unable('move current version')
190 try:
191 os.rename(filename + '.new', filename)
192 except (IOError, OSError):
193 report_unable('overwrite current version')
194 os.rename(filename + '.old', filename)
195 return
196 try:
197 # Continues to run in the background
198 Popen(
199 'ping 127.0.0.1 -n 5 -w 1000 & del /F "%s.old"' % filename,
200 shell=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
201 ydl.to_screen('Updated yt-dlp to version %s' % version_id)
202 return True # Exit app
203 except OSError:
204 report_unable('delete the old version')
205
206 elif variant in ('zip', 'mac_exe'):
207 pack_type = '3' if variant == 'zip' else '64'
208 try:
209 url = get_bin_info(variant, pack_type).get('browser_download_url')
210 if not url:
211 return report_network_error('fetch updates')
212 urlh = ydl._opener.open(url)
213 newcontent = urlh.read()
214 urlh.close()
215 except (IOError, OSError):
216 return report_network_error('download the latest version')
217
218 expected_sum = get_sha256sum(variant, pack_type)
219 if not expected_sum:
220 ydl.report_warning('no hash information found for the release')
221 elif hashlib.sha256(newcontent).hexdigest() != expected_sum:
222 return report_network_error('verify the new package')
223
224 try:
225 with open(filename, 'wb') as outf:
226 outf.write(newcontent)
227 except (IOError, OSError):
228 return report_unable('overwrite current version')
229
230 ydl.to_screen('Updated yt-dlp to version %s; Restart yt-dlp to use the new version' % version_id)
231 return
232
233 assert False, f'Unhandled variant: {variant}'
234
235
236 ''' # UNUSED
237 def get_notes(versions, fromVersion):
238 notes = []
239 for v, vdata in sorted(versions.items()):
240 if v > fromVersion:
241 notes.extend(vdata.get('notes', []))
242 return notes
243
244
245 def print_notes(to_screen, versions, fromVersion=__version__):
246 notes = get_notes(versions, fromVersion)
247 if notes:
248 to_screen('PLEASE NOTE:')
249 for note in notes:
250 to_screen(note)
251 '''
252
253
254 def update_self(to_screen, verbose, opener):
255 ''' Exists for backward compatibility '''
256
257 printfn = to_screen
258
259 printfn(
260 'WARNING: "yt_dlp.update.update_self" is deprecated and may be removed in a future version. '
261 'Use "yt_dlp.update.run_update(ydl)" instead')
262
263 class FakeYDL():
264 _opener = opener
265 to_screen = printfn
266
267 @staticmethod
268 def report_warning(msg, *args, **kwargs):
269 return printfn('WARNING: %s' % msg, *args, **kwargs)
270
271 @staticmethod
272 def report_error(msg, tb=None):
273 printfn('ERROR: %s' % msg)
274 if not verbose:
275 return
276 if tb is None:
277 # Copied from YoutubeDl.trouble
278 if sys.exc_info()[0]:
279 tb = ''
280 if hasattr(sys.exc_info()[1], 'exc_info') and sys.exc_info()[1].exc_info[0]:
281 tb += ''.join(traceback.format_exception(*sys.exc_info()[1].exc_info))
282 tb += encode_compat_str(traceback.format_exc())
283 else:
284 tb_data = traceback.format_list(traceback.extract_stack())
285 tb = ''.join(tb_data)
286 if tb:
287 printfn(tb)
288
289 return run_update(FakeYDL())