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