]> jfr.im git - yt-dlp.git/blame - yt_dlp/update.py
[expressen] Fix extractor (#4006)
[yt-dlp.git] / yt_dlp / update.py
CommitLineData
c19bc311 1import hashlib
d5ed35b6 2import json
ce02ed60 3import os
e5813e53 4import platform
d2790370 5import subprocess
46353f67 6import sys
d5ed35b6
FV
7from zipimport import zipimporter
8
b5899f4f 9from .compat import functools # isort: split
10from .compat import compat_realpath
11from .utils import Popen, traverse_obj, version_tuple
d5ed35b6
FV
12from .version import __version__
13
5f6a1245 14
b5899f4f 15RELEASE_JSON_URL = 'https://api.github.com/repos/yt-dlp/yt-dlp/releases/latest'
16
17
0b9c08b4 18@functools.cache
b5899f4f 19def _get_variant_and_executable_path():
c487cf00 20 """@returns (variant, executable_path)"""
5d535b4a 21 if hasattr(sys, 'frozen'):
c487cf00 22 path = sys.executable
b5899f4f 23 if not hasattr(sys, '_MEIPASS'):
24 return 'py2exe', path
25 if sys._MEIPASS == os.path.dirname(path):
26 return f'{sys.platform}_dir', path
27 return f'{sys.platform}_exe', path
28
29 path = os.path.dirname(__file__)
c487cf00 30 if isinstance(__loader__, zipimporter):
31 return 'zip', os.path.join(path, '..')
4c88ff87 32 elif os.path.basename(sys.argv[0]) == '__main__.py':
c487cf00 33 return 'source', path
34 return 'unknown', path
35
36
37def detect_variant():
b5899f4f 38 return _get_variant_and_executable_path()[0]
4c88ff87 39
40
b5899f4f 41_FILE_SUFFIXES = {
42 'zip': '',
43 'py2exe': '_min.exe',
44 'win32_exe': '.exe',
45 'darwin_exe': '_macos',
46}
47
5d535b4a 48_NON_UPDATEABLE_REASONS = {
b5899f4f 49 **{variant: None for variant in _FILE_SUFFIXES}, # Updatable
50 **{variant: f'Auto-update is not supported for unpackaged {name} executable; Re-download the latest release'
51 for variant, name in {'win32_dir': 'Windows', 'darwin_dir': 'MacOS'}.items()},
e6faf2be 52 'source': 'You cannot update when running from source code; Use git to pull the latest changes',
455a15e2 53 'unknown': 'It looks like you installed yt-dlp with a package manager, pip or setup.py; Use that to update',
b5899f4f 54 'other': 'It looks like you are using an unofficial build of yt-dlp; Build the executable again',
5d535b4a 55}
56
57
58def is_non_updateable():
b5899f4f 59 return _NON_UPDATEABLE_REASONS.get(detect_variant(), _NON_UPDATEABLE_REASONS['other'])
5d535b4a 60
61
c19bc311 62def run_update(ydl):
e5813e53 63 """
64 Update the program file with the latest version from the repository
65 Returns whether the program should terminate
66 """
d5ed35b6 67
e6faf2be 68 def report_error(msg, expected=False):
c487cf00 69 ydl.report_error(msg, tb=False if expected else None)
e6faf2be 70
71 def report_unable(action, expected=False):
72 report_error(f'Unable to {action}', expected)
73
74 def report_permission_error(file):
75 report_unable(f'write to {file}; Try running as administrator', True)
76
77 def report_network_error(action, delim=';'):
78 report_unable(f'{action}{delim} Visit https://github.com/yt-dlp/yt-dlp/releases/latest', True)
c19bc311 79
44f705d0 80 def calc_sha256sum(path):
fa57af1e 81 h = hashlib.sha256()
b5899f4f 82 mv = memoryview(bytearray(128 * 1024))
44f705d0 83 with open(os.path.realpath(path), 'rb', buffering=0) as f:
fa57af1e
U
84 for n in iter(lambda: f.readinto(mv), 0):
85 h.update(mv[:n])
86 return h.hexdigest()
87
28234287 88 try:
b5899f4f 89 version_info = json.loads(ydl.urlopen(RELEASE_JSON_URL).read().decode())
28234287 90 except Exception:
e6faf2be 91 return report_network_error('obtain version info', delim='; Please try again later or')
28234287 92
28234287 93 version_id = version_info['tag_name']
75b725a7 94 ydl.to_screen(f'Latest version: {version_id}, Current version: {__version__}')
28234287 95 if version_tuple(__version__) >= version_tuple(version_id):
96 ydl.to_screen(f'yt-dlp is up to date ({__version__})')
97 return
98
5d535b4a 99 err = is_non_updateable()
4040428e 100 if err:
e6faf2be 101 return report_error(err, True)
d5ed35b6 102
b5899f4f 103 variant, filename = _get_variant_and_executable_path()
c487cf00 104 filename = compat_realpath(filename) # Absolute path, following symlinks
105
b5899f4f 106 label = _FILE_SUFFIXES[variant]
107 if label and platform.architecture()[0][:2] == '32':
108 label = f'_x86{label}'
109 release_name = f'yt-dlp{label}'
110
91f071af 111 ydl.to_screen(f'Current Build Hash {calc_sha256sum(filename)}')
28234287 112 ydl.to_screen(f'Updating to version {version_id} ...')
3bf79c75 113
b5899f4f 114 def get_file(name, fatal=True):
115 error = report_network_error if fatal else lambda _: None
116 url = traverse_obj(
117 version_info, ('assets', lambda _, v: v['name'] == name, 'browser_download_url'), get_all=False)
118 if not url:
119 return error('fetch updates')
b25522ba 120 try:
b5899f4f 121 return ydl.urlopen(url).read()
86e5f3ed 122 except OSError:
b5899f4f 123 return error('download latest version')
124
125 def verify(content):
126 if not content:
127 return False
128 hash_data = get_file('SHA2-256SUMS', fatal=False) or b''
129 expected = dict(ln.split()[::-1] for ln in hash_data.decode().splitlines()).get(release_name)
130 if not expected:
c19bc311 131 ydl.report_warning('no hash information found for the release')
b5899f4f 132 elif hashlib.sha256(content).hexdigest() != expected:
133 return report_network_error('verify the new executable')
134 return True
44f705d0 135
b5899f4f 136 directory = os.path.dirname(filename)
137 if not os.access(filename, os.W_OK):
138 return report_permission_error(filename)
139 elif not os.access(directory, os.W_OK):
140 return report_permission_error(directory)
d5ed35b6 141
b5899f4f 142 new_filename, old_filename = f'{filename}.new', f'{filename}.old'
143 if variant == 'zip': # Can be replaced in-place
144 new_filename, old_filename = filename, None
d5ed35b6 145
b5899f4f 146 try:
147 if os.path.exists(old_filename or ''):
148 os.remove(old_filename)
149 except OSError:
150 return report_unable('remove the old version')
44f705d0 151
b5899f4f 152 newcontent = get_file(release_name)
153 if not verify(newcontent):
154 return
155 try:
156 with open(new_filename, 'wb') as outf:
157 outf.write(newcontent)
158 except OSError:
159 return report_permission_error(new_filename)
160
161 try:
162 if old_filename:
163 os.rename(filename, old_filename)
164 except OSError:
165 return report_unable('move current version')
166 try:
167 if old_filename:
168 os.rename(new_filename, filename)
169 except OSError:
170 report_unable('overwrite current version')
171 os.rename(old_filename, filename)
172 return
d5ed35b6 173
b5899f4f 174 if variant not in ('win32_exe', 'py2exe'):
175 if old_filename:
176 os.remove(old_filename)
177 ydl.to_screen(f'Updated yt-dlp to version {version_id}; Restart yt-dlp to use the new version')
0e5927ee
R
178 return
179
b5899f4f 180 try:
181 # Continues to run in the background
182 Popen(f'ping 127.0.0.1 -n 5 -w 1000 & del /F "{old_filename}"',
183 shell=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
184 ydl.to_screen(f'Updated yt-dlp to version {version_id}')
185 return True # Exit app
186 except OSError:
187 report_unable('delete the old version')
3bf79c75 188
5f6a1245 189
ee8dd27a 190# Deprecated
e6faf2be 191def update_self(to_screen, verbose, opener):
b5899f4f 192 import traceback
193 from .utils import write_string
e6faf2be 194
ee8dd27a 195 write_string(
196 'DeprecationWarning: "yt_dlp.update.update_self" is deprecated and may be removed in a future version. '
b69fd25c 197 'Use "yt_dlp.update.run_update(ydl)" instead\n')
e6faf2be 198
b5899f4f 199 printfn = to_screen
200
e6faf2be 201 class FakeYDL():
e6faf2be 202 to_screen = printfn
203
204 @staticmethod
205 def report_warning(msg, *args, **kwargs):
b5899f4f 206 return printfn(f'WARNING: {msg}', *args, **kwargs)
e6faf2be 207
208 @staticmethod
209 def report_error(msg, tb=None):
b5899f4f 210 printfn(f'ERROR: {msg}')
e6faf2be 211 if not verbose:
212 return
213 if tb is None:
b5899f4f 214 # Copied from YoutubeDL.trouble
e6faf2be 215 if sys.exc_info()[0]:
216 tb = ''
217 if hasattr(sys.exc_info()[1], 'exc_info') and sys.exc_info()[1].exc_info[0]:
218 tb += ''.join(traceback.format_exception(*sys.exc_info()[1].exc_info))
b5899f4f 219 tb += traceback.format_exc()
e6faf2be 220 else:
221 tb_data = traceback.format_list(traceback.extract_stack())
222 tb = ''.join(tb_data)
223 if tb:
224 printfn(tb)
225
b5899f4f 226 def urlopen(self, url):
227 return opener.open(url)
228
e6faf2be 229 return run_update(FakeYDL())