]>
Commit | Line | Data |
---|---|---|
c19bc311 | 1 | import hashlib |
d5ed35b6 | 2 | import json |
ce02ed60 | 3 | import os |
e5813e53 | 4 | import platform |
d2790370 | 5 | import subprocess |
46353f67 | 6 | import sys |
d5ed35b6 FV |
7 | from zipimport import zipimporter |
8 | ||
b5899f4f | 9 | from .compat import functools # isort: split |
10 | from .compat import compat_realpath | |
11 | from .utils import Popen, traverse_obj, version_tuple | |
d5ed35b6 FV |
12 | from .version import __version__ |
13 | ||
57e0f077 | 14 | REPOSITORY = 'yt-dlp/yt-dlp' |
15 | API_URL = f'https://api.github.com/repos/{REPOSITORY}/releases/latest' | |
b5899f4f | 16 | |
17 | ||
0b9c08b4 | 18 | @functools.cache |
b5899f4f | 19 | def _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, '..') | |
233ad894 | 32 | elif (os.path.basename(sys.argv[0]) in ('__main__.py', '-m') |
33 | and os.path.exists(os.path.join(path, '../.git/HEAD'))): | |
c487cf00 | 34 | return 'source', path |
35 | return 'unknown', path | |
36 | ||
37 | ||
38 | def detect_variant(): | |
b5899f4f | 39 | return _get_variant_and_executable_path()[0] |
4c88ff87 | 40 | |
41 | ||
b5899f4f | 42 | _FILE_SUFFIXES = { |
43 | 'zip': '', | |
44 | 'py2exe': '_min.exe', | |
45 | 'win32_exe': '.exe', | |
46 | 'darwin_exe': '_macos', | |
47 | } | |
48 | ||
5d535b4a | 49 | _NON_UPDATEABLE_REASONS = { |
b5899f4f | 50 | **{variant: None for variant in _FILE_SUFFIXES}, # Updatable |
51 | **{variant: f'Auto-update is not supported for unpackaged {name} executable; Re-download the latest release' | |
52 | for variant, name in {'win32_dir': 'Windows', 'darwin_dir': 'MacOS'}.items()}, | |
e6faf2be | 53 | 'source': 'You cannot update when running from source code; Use git to pull the latest changes', |
455a15e2 | 54 | 'unknown': 'It looks like you installed yt-dlp with a package manager, pip or setup.py; Use that to update', |
b5899f4f | 55 | 'other': 'It looks like you are using an unofficial build of yt-dlp; Build the executable again', |
5d535b4a | 56 | } |
57 | ||
58 | ||
59 | def is_non_updateable(): | |
b5899f4f | 60 | return _NON_UPDATEABLE_REASONS.get(detect_variant(), _NON_UPDATEABLE_REASONS['other']) |
5d535b4a | 61 | |
62 | ||
57e0f077 | 63 | def _sha256_file(path): |
64 | h = hashlib.sha256() | |
65 | mv = memoryview(bytearray(128 * 1024)) | |
66 | with open(os.path.realpath(path), 'rb', buffering=0) as f: | |
67 | for n in iter(lambda: f.readinto(mv), 0): | |
68 | h.update(mv[:n]) | |
69 | return h.hexdigest() | |
70 | ||
71 | ||
72 | class Updater: | |
73 | def __init__(self, ydl): | |
74 | self.ydl = ydl | |
75 | ||
76 | @functools.cached_property | |
77 | def _new_version_info(self): | |
78 | self.ydl.write_debug(f'Fetching release info: {API_URL}') | |
79 | return json.loads(self.ydl.urlopen(API_URL).read().decode()) | |
80 | ||
81 | @property | |
82 | def current_version(self): | |
83 | """Current version""" | |
84 | return __version__ | |
85 | ||
86 | @property | |
87 | def new_version(self): | |
88 | """Version of the latest release""" | |
89 | return self._new_version_info['tag_name'] | |
90 | ||
91 | @property | |
92 | def has_update(self): | |
93 | """Whether there is an update available""" | |
94 | return version_tuple(__version__) < version_tuple(self.new_version) | |
95 | ||
96 | @functools.cached_property | |
97 | def filename(self): | |
98 | """Filename of the executable""" | |
99 | return compat_realpath(_get_variant_and_executable_path()[1]) | |
100 | ||
101 | def _download(self, name=None): | |
102 | name = name or self.release_name | |
103 | url = traverse_obj(self._new_version_info, ( | |
104 | 'assets', lambda _, v: v['name'] == name, 'browser_download_url'), get_all=False) | |
105 | if not url: | |
106 | raise Exception('Unable to find download URL') | |
107 | self.ydl.write_debug(f'Downloading {name} from {url}') | |
108 | return self.ydl.urlopen(url).read() | |
109 | ||
110 | @functools.cached_property | |
111 | def release_name(self): | |
112 | """The release filename""" | |
113 | label = _FILE_SUFFIXES[detect_variant()] | |
114 | if label and platform.architecture()[0][:2] == '32': | |
115 | label = f'_x86{label}' | |
116 | return f'yt-dlp{label}' | |
117 | ||
118 | @functools.cached_property | |
119 | def release_hash(self): | |
120 | """Hash of the latest release""" | |
121 | hash_data = dict(ln.split()[::-1] for ln in self._download('SHA2-256SUMS').decode().splitlines()) | |
122 | return hash_data[self.release_name] | |
123 | ||
124 | def _report_error(self, msg, expected=False): | |
125 | self.ydl.report_error(msg, tb=False if expected else None) | |
126 | ||
127 | def _report_permission_error(self, file): | |
128 | self._report_error(f'Unable to write to {file}; Try running as administrator', True) | |
129 | ||
130 | def _report_network_error(self, action, delim=';'): | |
131 | self._report_error(f'Unable to {action}{delim} Visit https://github.com/{REPOSITORY}/releases/latest', True) | |
132 | ||
133 | def check_update(self): | |
134 | """Report whether there is an update available""" | |
135 | try: | |
136 | self.ydl.to_screen( | |
137 | f'Latest version: {self.new_version}, Current version: {self.current_version}') | |
138 | except Exception: | |
139 | return self._report_network_error('obtain version info', delim='; Please try again later or') | |
e6faf2be | 140 | |
57e0f077 | 141 | if not self.has_update: |
142 | return self.ydl.to_screen(f'yt-dlp is up to date ({__version__})') | |
e6faf2be | 143 | |
57e0f077 | 144 | if not is_non_updateable(): |
145 | self.ydl.to_screen(f'Current Build Hash {_sha256_file(self.filename)}') | |
146 | return True | |
c19bc311 | 147 | |
57e0f077 | 148 | def update(self): |
149 | """Update yt-dlp executable to the latest version""" | |
150 | if not self.check_update(): | |
151 | return | |
152 | err = is_non_updateable() | |
153 | if err: | |
154 | return self._report_error(err, True) | |
155 | self.ydl.to_screen(f'Updating to version {self.new_version} ...') | |
156 | ||
157 | directory = os.path.dirname(self.filename) | |
158 | if not os.access(self.filename, os.W_OK): | |
159 | return self._report_permission_error(self.filename) | |
160 | elif not os.access(directory, os.W_OK): | |
161 | return self._report_permission_error(directory) | |
162 | ||
163 | new_filename, old_filename = f'{self.filename}.new', f'{self.filename}.old' | |
164 | if detect_variant() == 'zip': # Can be replaced in-place | |
165 | new_filename, old_filename = self.filename, None | |
fa57af1e | 166 | |
57e0f077 | 167 | try: |
168 | if os.path.exists(old_filename or ''): | |
169 | os.remove(old_filename) | |
170 | except OSError: | |
171 | return self._report_error('Unable to remove the old version') | |
28234287 | 172 | |
57e0f077 | 173 | try: |
174 | newcontent = self._download() | |
175 | except OSError: | |
176 | return self._report_network_error('download latest version') | |
177 | except Exception: | |
178 | return self._report_network_error('fetch updates') | |
28234287 | 179 | |
57e0f077 | 180 | try: |
181 | expected_hash = self.release_hash | |
182 | except Exception: | |
183 | self.ydl.report_warning('no hash information found for the release') | |
184 | else: | |
185 | if hashlib.sha256(newcontent).hexdigest() != expected_hash: | |
186 | return self._report_network_error('verify the new executable') | |
d5ed35b6 | 187 | |
57e0f077 | 188 | try: |
189 | with open(new_filename, 'wb') as outf: | |
190 | outf.write(newcontent) | |
191 | except OSError: | |
192 | return self._report_permission_error(new_filename) | |
c487cf00 | 193 | |
57e0f077 | 194 | try: |
195 | if old_filename: | |
196 | os.rename(self.filename, old_filename) | |
197 | except OSError: | |
198 | return self._report_error('Unable to move current version') | |
199 | try: | |
200 | if old_filename: | |
201 | os.rename(new_filename, self.filename) | |
202 | except OSError: | |
203 | self._report_error('Unable to overwrite current version') | |
204 | return os.rename(old_filename, self.filename) | |
b5899f4f | 205 | |
57e0f077 | 206 | if detect_variant() not in ('win32_exe', 'py2exe'): |
207 | if old_filename: | |
208 | os.remove(old_filename) | |
209 | self.ydl.to_screen(f'Updated yt-dlp to version {self.new_version}; Restart yt-dlp to use the new version') | |
210 | return | |
3bf79c75 | 211 | |
b25522ba | 212 | try: |
57e0f077 | 213 | # Continues to run in the background |
214 | Popen(f'ping 127.0.0.1 -n 5 -w 1000 & del /F "{old_filename}"', | |
215 | shell=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) | |
216 | self.ydl.to_screen(f'Updated yt-dlp to version {self.new_version}') | |
217 | return True # Exit app | |
86e5f3ed | 218 | except OSError: |
57e0f077 | 219 | self._report_unable('delete the old version') |
220 | ||
44f705d0 | 221 | |
57e0f077 | 222 | def run_update(ydl): |
223 | """Update the program file with the latest version from the repository | |
224 | @returns Whether there was a successfull update (No update = False) | |
225 | """ | |
226 | return Updater(ydl).update() | |
3bf79c75 | 227 | |
5f6a1245 | 228 | |
ee8dd27a | 229 | # Deprecated |
e6faf2be | 230 | def update_self(to_screen, verbose, opener): |
b5899f4f | 231 | import traceback |
57e0f077 | 232 | |
b5899f4f | 233 | from .utils import write_string |
e6faf2be | 234 | |
ee8dd27a | 235 | write_string( |
236 | 'DeprecationWarning: "yt_dlp.update.update_self" is deprecated and may be removed in a future version. ' | |
b69fd25c | 237 | 'Use "yt_dlp.update.run_update(ydl)" instead\n') |
e6faf2be | 238 | |
b5899f4f | 239 | printfn = to_screen |
240 | ||
e6faf2be | 241 | class FakeYDL(): |
e6faf2be | 242 | to_screen = printfn |
243 | ||
57e0f077 | 244 | def report_warning(self, msg, *args, **kwargs): |
b5899f4f | 245 | return printfn(f'WARNING: {msg}', *args, **kwargs) |
e6faf2be | 246 | |
57e0f077 | 247 | def report_error(self, msg, tb=None): |
b5899f4f | 248 | printfn(f'ERROR: {msg}') |
e6faf2be | 249 | if not verbose: |
250 | return | |
251 | if tb is None: | |
b5899f4f | 252 | # Copied from YoutubeDL.trouble |
e6faf2be | 253 | if sys.exc_info()[0]: |
254 | tb = '' | |
255 | if hasattr(sys.exc_info()[1], 'exc_info') and sys.exc_info()[1].exc_info[0]: | |
256 | tb += ''.join(traceback.format_exception(*sys.exc_info()[1].exc_info)) | |
b5899f4f | 257 | tb += traceback.format_exc() |
e6faf2be | 258 | else: |
259 | tb_data = traceback.format_list(traceback.extract_stack()) | |
260 | tb = ''.join(tb_data) | |
261 | if tb: | |
262 | printfn(tb) | |
263 | ||
57e0f077 | 264 | def write_debug(self, msg, *args, **kwargs): |
265 | printfn(f'[debug] {msg}', *args, **kwargs) | |
266 | ||
b5899f4f | 267 | def urlopen(self, url): |
268 | return opener.open(url) | |
269 | ||
e6faf2be | 270 | return run_update(FakeYDL()) |