]> jfr.im git - yt-dlp.git/blob - yt_dlp/update.py
3de7c720927b8f0946228ef5380702c5ae50da5c
[yt-dlp.git] / yt_dlp / update.py
1 import hashlib
2 import json
3 import os
4 import platform
5 import subprocess
6 import sys
7 from zipimport import zipimporter
8
9 from .compat import functools # isort: split
10 from .compat import compat_realpath
11 from .utils import Popen, traverse_obj, version_tuple
12 from .version import __version__
13
14
15 RELEASE_JSON_URL = 'https://api.github.com/repos/yt-dlp/yt-dlp/releases/latest'
16
17
18 @functools.cache
19 def _get_variant_and_executable_path():
20 """@returns (variant, executable_path)"""
21 if hasattr(sys, 'frozen'):
22 path = sys.executable
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__)
30 if isinstance(__loader__, zipimporter):
31 return 'zip', os.path.join(path, '..')
32 elif os.path.basename(sys.argv[0]) == '__main__.py':
33 return 'source', path
34 return 'unknown', path
35
36
37 def detect_variant():
38 return _get_variant_and_executable_path()[0]
39
40
41 _FILE_SUFFIXES = {
42 'zip': '',
43 'py2exe': '_min.exe',
44 'win32_exe': '.exe',
45 'darwin_exe': '_macos',
46 }
47
48 _NON_UPDATEABLE_REASONS = {
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()},
52 'source': 'You cannot update when running from source code; Use git to pull the latest changes',
53 'unknown': 'It looks like you installed yt-dlp with a package manager, pip or setup.py; Use that to update',
54 'other': 'It looks like you are using an unofficial build of yt-dlp; Build the executable again',
55 }
56
57
58 def is_non_updateable():
59 return _NON_UPDATEABLE_REASONS.get(detect_variant(), _NON_UPDATEABLE_REASONS['other'])
60
61
62 def run_update(ydl):
63 """
64 Update the program file with the latest version from the repository
65 Returns whether the program should terminate
66 """
67
68 def report_error(msg, expected=False):
69 ydl.report_error(msg, tb=False if expected else None)
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)
79
80 def calc_sha256sum(path):
81 h = hashlib.sha256()
82 mv = memoryview(bytearray(128 * 1024))
83 with open(os.path.realpath(path), 'rb', buffering=0) as f:
84 for n in iter(lambda: f.readinto(mv), 0):
85 h.update(mv[:n])
86 return h.hexdigest()
87
88 try:
89 version_info = json.loads(ydl.urlopen(RELEASE_JSON_URL).read().decode())
90 except Exception:
91 return report_network_error('obtain version info', delim='; Please try again later or')
92
93 version_id = version_info['tag_name']
94 ydl.to_screen(f'Latest version: {version_id}, Current version: {__version__}')
95 if version_tuple(__version__) >= version_tuple(version_id):
96 ydl.to_screen(f'yt-dlp is up to date ({__version__})')
97 return
98
99 err = is_non_updateable()
100 if err:
101 return report_error(err, True)
102
103 variant, filename = _get_variant_and_executable_path()
104 filename = compat_realpath(filename) # Absolute path, following symlinks
105
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
111 ydl.to_screen(f'Current Build Hash {calc_sha256sum(filename)}')
112 ydl.to_screen(f'Updating to version {version_id} ...')
113
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')
120 try:
121 return ydl.urlopen(url).read()
122 except OSError:
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:
131 ydl.report_warning('no hash information found for the release')
132 elif hashlib.sha256(content).hexdigest() != expected:
133 return report_network_error('verify the new executable')
134 return True
135
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)
141
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
145
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')
151
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
173
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')
178 return
179
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')
188
189
190 # Deprecated
191 def update_self(to_screen, verbose, opener):
192 import traceback
193 from .utils import write_string
194
195 write_string(
196 'DeprecationWarning: "yt_dlp.update.update_self" is deprecated and may be removed in a future version. '
197 'Use "yt_dlp.update.run_update(ydl)" instead\n')
198
199 printfn = to_screen
200
201 class FakeYDL():
202 to_screen = printfn
203
204 @staticmethod
205 def report_warning(msg, *args, **kwargs):
206 return printfn(f'WARNING: {msg}', *args, **kwargs)
207
208 @staticmethod
209 def report_error(msg, tb=None):
210 printfn(f'ERROR: {msg}')
211 if not verbose:
212 return
213 if tb is None:
214 # Copied from YoutubeDL.trouble
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))
219 tb += traceback.format_exc()
220 else:
221 tb_data = traceback.format_list(traceback.extract_stack())
222 tb = ''.join(tb_data)
223 if tb:
224 printfn(tb)
225
226 def urlopen(self, url):
227 return opener.open(url)
228
229 return run_update(FakeYDL())