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