]>
Commit | Line | Data |
---|---|---|
1 | import atexit | |
2 | import contextlib | |
3 | import hashlib | |
4 | import json | |
5 | import os | |
6 | import platform | |
7 | import re | |
8 | import subprocess | |
9 | import sys | |
10 | from zipimport import zipimporter | |
11 | ||
12 | from .compat import functools # isort: split | |
13 | from .compat import compat_realpath, compat_shlex_quote | |
14 | from .utils import ( | |
15 | Popen, | |
16 | cached_method, | |
17 | shell_quote, | |
18 | system_identifier, | |
19 | traverse_obj, | |
20 | version_tuple, | |
21 | ) | |
22 | from .version import UPDATE_HINT, VARIANT, __version__ | |
23 | ||
24 | REPOSITORY = 'yt-dlp/yt-dlp' | |
25 | API_URL = f'https://api.github.com/repos/{REPOSITORY}/releases' | |
26 | ||
27 | ||
28 | @functools.cache | |
29 | def _get_variant_and_executable_path(): | |
30 | """@returns (variant, executable_path)""" | |
31 | if hasattr(sys, 'frozen'): | |
32 | path = sys.executable | |
33 | if not hasattr(sys, '_MEIPASS'): | |
34 | return 'py2exe', path | |
35 | if sys._MEIPASS == os.path.dirname(path): | |
36 | return f'{sys.platform}_dir', path | |
37 | if sys.platform == 'darwin' and version_tuple(platform.mac_ver()[0]) < (10, 15): | |
38 | return 'darwin_legacy_exe', path | |
39 | return f'{sys.platform}_exe', path | |
40 | ||
41 | path = os.path.dirname(__file__) | |
42 | if isinstance(__loader__, zipimporter): | |
43 | return 'zip', os.path.join(path, '..') | |
44 | elif (os.path.basename(sys.argv[0]) in ('__main__.py', '-m') | |
45 | and os.path.exists(os.path.join(path, '../.git/HEAD'))): | |
46 | return 'source', path | |
47 | return 'unknown', path | |
48 | ||
49 | ||
50 | def detect_variant(): | |
51 | return VARIANT or _get_variant_and_executable_path()[0] | |
52 | ||
53 | ||
54 | @functools.cache | |
55 | def current_git_head(): | |
56 | if detect_variant() != 'source': | |
57 | return | |
58 | with contextlib.suppress(Exception): | |
59 | stdout, _, _ = Popen.run( | |
60 | ['git', 'rev-parse', '--short', 'HEAD'], | |
61 | text=True, cwd=os.path.dirname(os.path.abspath(__file__)), | |
62 | stdout=subprocess.PIPE, stderr=subprocess.PIPE) | |
63 | if re.fullmatch('[0-9a-f]+', stdout.strip()): | |
64 | return stdout.strip() | |
65 | ||
66 | ||
67 | _FILE_SUFFIXES = { | |
68 | 'zip': '', | |
69 | 'py2exe': '_min.exe', | |
70 | 'win32_exe': '.exe', | |
71 | 'darwin_exe': '_macos', | |
72 | 'darwin_legacy_exe': '_macos_legacy', | |
73 | 'linux_exe': '_linux', | |
74 | } | |
75 | ||
76 | _NON_UPDATEABLE_REASONS = { | |
77 | **{variant: None for variant in _FILE_SUFFIXES}, # Updatable | |
78 | **{variant: f'Auto-update is not supported for unpackaged {name} executable; Re-download the latest release' | |
79 | for variant, name in {'win32_dir': 'Windows', 'darwin_dir': 'MacOS', 'linux_dir': 'Linux'}.items()}, | |
80 | 'source': 'You cannot update when running from source code; Use git to pull the latest changes', | |
81 | 'unknown': 'You installed yt-dlp with a package manager or setup.py; Use that to update', | |
82 | 'other': 'You are using an unofficial build of yt-dlp; Build the executable again', | |
83 | } | |
84 | ||
85 | ||
86 | def is_non_updateable(): | |
87 | if UPDATE_HINT: | |
88 | return UPDATE_HINT | |
89 | return _NON_UPDATEABLE_REASONS.get( | |
90 | detect_variant(), _NON_UPDATEABLE_REASONS['unknown' if VARIANT else 'other']) | |
91 | ||
92 | ||
93 | def _sha256_file(path): | |
94 | h = hashlib.sha256() | |
95 | mv = memoryview(bytearray(128 * 1024)) | |
96 | with open(os.path.realpath(path), 'rb', buffering=0) as f: | |
97 | for n in iter(lambda: f.readinto(mv), 0): | |
98 | h.update(mv[:n]) | |
99 | return h.hexdigest() | |
100 | ||
101 | ||
102 | class Updater: | |
103 | def __init__(self, ydl): | |
104 | self.ydl = ydl | |
105 | ||
106 | @functools.cached_property | |
107 | def _tag(self): | |
108 | if version_tuple(__version__) >= version_tuple(self.latest_version): | |
109 | return 'latest' | |
110 | ||
111 | identifier = f'{detect_variant()} {system_identifier()}' | |
112 | for line in self._download('_update_spec', 'latest').decode().splitlines(): | |
113 | if not line.startswith('lock '): | |
114 | continue | |
115 | _, tag, pattern = line.split(' ', 2) | |
116 | if re.match(pattern, identifier): | |
117 | return f'tags/{tag}' | |
118 | return 'latest' | |
119 | ||
120 | @cached_method | |
121 | def _get_version_info(self, tag): | |
122 | self.ydl.write_debug(f'Fetching release info: {API_URL}/{tag}') | |
123 | return json.loads(self.ydl.urlopen(f'{API_URL}/{tag}').read().decode()) | |
124 | ||
125 | @property | |
126 | def current_version(self): | |
127 | """Current version""" | |
128 | return __version__ | |
129 | ||
130 | @property | |
131 | def new_version(self): | |
132 | """Version of the latest release we can update to""" | |
133 | if self._tag.startswith('tags/'): | |
134 | return self._tag[5:] | |
135 | return self._get_version_info(self._tag)['tag_name'] | |
136 | ||
137 | @property | |
138 | def latest_version(self): | |
139 | """Version of the latest release""" | |
140 | return self._get_version_info('latest')['tag_name'] | |
141 | ||
142 | @property | |
143 | def has_update(self): | |
144 | """Whether there is an update available""" | |
145 | return version_tuple(__version__) < version_tuple(self.new_version) | |
146 | ||
147 | @functools.cached_property | |
148 | def filename(self): | |
149 | """Filename of the executable""" | |
150 | return compat_realpath(_get_variant_and_executable_path()[1]) | |
151 | ||
152 | def _download(self, name, tag): | |
153 | url = traverse_obj(self._get_version_info(tag), ( | |
154 | 'assets', lambda _, v: v['name'] == name, 'browser_download_url'), get_all=False) | |
155 | if not url: | |
156 | raise Exception('Unable to find download URL') | |
157 | self.ydl.write_debug(f'Downloading {name} from {url}') | |
158 | return self.ydl.urlopen(url).read() | |
159 | ||
160 | @functools.cached_property | |
161 | def release_name(self): | |
162 | """The release filename""" | |
163 | label = _FILE_SUFFIXES[detect_variant()] | |
164 | if label and platform.architecture()[0][:2] == '32': | |
165 | label = f'_x86{label}' | |
166 | return f'yt-dlp{label}' | |
167 | ||
168 | @functools.cached_property | |
169 | def release_hash(self): | |
170 | """Hash of the latest release""" | |
171 | hash_data = dict(ln.split()[::-1] for ln in self._download('SHA2-256SUMS', self._tag).decode().splitlines()) | |
172 | return hash_data[self.release_name] | |
173 | ||
174 | def _report_error(self, msg, expected=False): | |
175 | self.ydl.report_error(msg, tb=False if expected else None) | |
176 | ||
177 | def _report_permission_error(self, file): | |
178 | self._report_error(f'Unable to write to {file}; Try running as administrator', True) | |
179 | ||
180 | def _report_network_error(self, action, delim=';'): | |
181 | self._report_error(f'Unable to {action}{delim} Visit https://github.com/{REPOSITORY}/releases/latest', True) | |
182 | ||
183 | def check_update(self): | |
184 | """Report whether there is an update available""" | |
185 | try: | |
186 | self.ydl.to_screen( | |
187 | f'Latest version: {self.latest_version}, Current version: {self.current_version}') | |
188 | if not self.has_update: | |
189 | if self._tag == 'latest': | |
190 | return self.ydl.to_screen(f'yt-dlp is up to date ({__version__})') | |
191 | return self.ydl.report_warning( | |
192 | 'yt-dlp cannot be updated any further since you are on an older Python version') | |
193 | except Exception: | |
194 | return self._report_network_error('obtain version info', delim='; Please try again later or') | |
195 | ||
196 | if not is_non_updateable(): | |
197 | self.ydl.to_screen(f'Current Build Hash {_sha256_file(self.filename)}') | |
198 | return True | |
199 | ||
200 | def update(self): | |
201 | """Update yt-dlp executable to the latest version""" | |
202 | if not self.check_update(): | |
203 | return | |
204 | err = is_non_updateable() | |
205 | if err: | |
206 | return self._report_error(err, True) | |
207 | self.ydl.to_screen(f'Updating to version {self.new_version} ...') | |
208 | ||
209 | directory = os.path.dirname(self.filename) | |
210 | if not os.access(self.filename, os.W_OK): | |
211 | return self._report_permission_error(self.filename) | |
212 | elif not os.access(directory, os.W_OK): | |
213 | return self._report_permission_error(directory) | |
214 | ||
215 | new_filename, old_filename = f'{self.filename}.new', f'{self.filename}.old' | |
216 | if detect_variant() == 'zip': # Can be replaced in-place | |
217 | new_filename, old_filename = self.filename, None | |
218 | ||
219 | try: | |
220 | if os.path.exists(old_filename or ''): | |
221 | os.remove(old_filename) | |
222 | except OSError: | |
223 | return self._report_error('Unable to remove the old version') | |
224 | ||
225 | try: | |
226 | newcontent = self._download(self.release_name, self._tag) | |
227 | except OSError: | |
228 | return self._report_network_error('download latest version') | |
229 | except Exception: | |
230 | return self._report_network_error('fetch updates') | |
231 | ||
232 | try: | |
233 | expected_hash = self.release_hash | |
234 | except Exception: | |
235 | self.ydl.report_warning('no hash information found for the release') | |
236 | else: | |
237 | if hashlib.sha256(newcontent).hexdigest() != expected_hash: | |
238 | return self._report_network_error('verify the new executable') | |
239 | ||
240 | try: | |
241 | with open(new_filename, 'wb') as outf: | |
242 | outf.write(newcontent) | |
243 | except OSError: | |
244 | return self._report_permission_error(new_filename) | |
245 | ||
246 | if old_filename: | |
247 | mask = os.stat(self.filename).st_mode | |
248 | try: | |
249 | os.rename(self.filename, old_filename) | |
250 | except OSError: | |
251 | return self._report_error('Unable to move current version') | |
252 | ||
253 | try: | |
254 | os.rename(new_filename, self.filename) | |
255 | except OSError: | |
256 | self._report_error('Unable to overwrite current version') | |
257 | return os.rename(old_filename, self.filename) | |
258 | ||
259 | if detect_variant() in ('win32_exe', 'py2exe'): | |
260 | atexit.register(Popen, f'ping 127.0.0.1 -n 5 -w 1000 & del /F "{old_filename}"', | |
261 | shell=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) | |
262 | elif old_filename: | |
263 | try: | |
264 | os.remove(old_filename) | |
265 | except OSError: | |
266 | self._report_error('Unable to remove the old version') | |
267 | ||
268 | try: | |
269 | os.chmod(self.filename, mask) | |
270 | except OSError: | |
271 | return self._report_error( | |
272 | f'Unable to set permissions. Run: sudo chmod a+rx {compat_shlex_quote(self.filename)}') | |
273 | ||
274 | self.ydl.to_screen(f'Updated yt-dlp to version {self.new_version}') | |
275 | return True | |
276 | ||
277 | @functools.cached_property | |
278 | def cmd(self): | |
279 | """The command-line to run the executable, if known""" | |
280 | # There is no sys.orig_argv in py < 3.10. Also, it can be [] when frozen | |
281 | if getattr(sys, 'orig_argv', None): | |
282 | return sys.orig_argv | |
283 | elif hasattr(sys, 'frozen'): | |
284 | return sys.argv | |
285 | ||
286 | def restart(self): | |
287 | """Restart the executable""" | |
288 | assert self.cmd, 'Must be frozen or Py >= 3.10' | |
289 | self.ydl.write_debug(f'Restarting: {shell_quote(self.cmd)}') | |
290 | _, _, returncode = Popen.run(self.cmd) | |
291 | return returncode | |
292 | ||
293 | ||
294 | def run_update(ydl): | |
295 | """Update the program file with the latest version from the repository | |
296 | @returns Whether there was a successful update (No update = False) | |
297 | """ | |
298 | return Updater(ydl).update() | |
299 | ||
300 | ||
301 | # Deprecated | |
302 | def update_self(to_screen, verbose, opener): | |
303 | import traceback | |
304 | ||
305 | from .utils import write_string | |
306 | ||
307 | write_string( | |
308 | 'DeprecationWarning: "yt_dlp.update.update_self" is deprecated and may be removed in a future version. ' | |
309 | 'Use "yt_dlp.update.run_update(ydl)" instead\n') | |
310 | ||
311 | printfn = to_screen | |
312 | ||
313 | class FakeYDL(): | |
314 | to_screen = printfn | |
315 | ||
316 | def report_warning(self, msg, *args, **kwargs): | |
317 | return printfn(f'WARNING: {msg}', *args, **kwargs) | |
318 | ||
319 | def report_error(self, msg, tb=None): | |
320 | printfn(f'ERROR: {msg}') | |
321 | if not verbose: | |
322 | return | |
323 | if tb is None: | |
324 | # Copied from YoutubeDL.trouble | |
325 | if sys.exc_info()[0]: | |
326 | tb = '' | |
327 | if hasattr(sys.exc_info()[1], 'exc_info') and sys.exc_info()[1].exc_info[0]: | |
328 | tb += ''.join(traceback.format_exception(*sys.exc_info()[1].exc_info)) | |
329 | tb += traceback.format_exc() | |
330 | else: | |
331 | tb_data = traceback.format_list(traceback.extract_stack()) | |
332 | tb = ''.join(tb_data) | |
333 | if tb: | |
334 | printfn(tb) | |
335 | ||
336 | def write_debug(self, msg, *args, **kwargs): | |
337 | printfn(f'[debug] {msg}', *args, **kwargs) | |
338 | ||
339 | def urlopen(self, url): | |
340 | return opener.open(url) | |
341 | ||
342 | return run_update(FakeYDL()) |