]>
Commit | Line | Data |
---|---|---|
1 | import hashlib | |
2 | import json | |
3 | import os | |
4 | import platform | |
5 | import subprocess | |
6 | import sys | |
7 | import traceback | |
8 | from zipimport import zipimporter | |
9 | ||
10 | from .compat import compat_realpath, functools | |
11 | from .utils import Popen, encode_compat_str, write_string | |
12 | from .version import __version__ | |
13 | ||
14 | ||
15 | @functools.cache | |
16 | def get_variant_and_executable_path(): | |
17 | """@returns (variant, executable_path)""" | |
18 | if hasattr(sys, 'frozen'): | |
19 | path = sys.executable | |
20 | prefix = 'mac' if sys.platform == 'darwin' else 'win' | |
21 | if getattr(sys, '_MEIPASS', None): | |
22 | if sys._MEIPASS == os.path.dirname(sys.executable): | |
23 | return f'{prefix}_dir', path | |
24 | return f'{prefix}_exe', path | |
25 | return 'py2exe', path | |
26 | ||
27 | path = os.path.join(os.path.dirname(__file__), '..') | |
28 | if isinstance(__loader__, zipimporter): | |
29 | return 'zip', os.path.join(path, '..') | |
30 | elif os.path.basename(sys.argv[0]) == '__main__.py': | |
31 | return 'source', path | |
32 | return 'unknown', path | |
33 | ||
34 | ||
35 | def detect_variant(): | |
36 | return get_variant_and_executable_path()[0] | |
37 | ||
38 | ||
39 | _NON_UPDATEABLE_REASONS = { | |
40 | 'win_exe': None, | |
41 | 'zip': None, | |
42 | 'mac_exe': None, | |
43 | 'py2exe': None, | |
44 | 'win_dir': 'Auto-update is not supported for unpackaged windows executable; Re-download the latest release', | |
45 | 'mac_dir': 'Auto-update is not supported for unpackaged MacOS executable; Re-download the latest release', | |
46 | 'source': 'You cannot update when running from source code; Use git to pull the latest changes', | |
47 | 'unknown': 'It looks like you installed yt-dlp with a package manager, pip or setup.py; Use that to update', | |
48 | } | |
49 | ||
50 | ||
51 | def is_non_updateable(): | |
52 | return _NON_UPDATEABLE_REASONS.get(detect_variant(), _NON_UPDATEABLE_REASONS['unknown']) | |
53 | ||
54 | ||
55 | def run_update(ydl): | |
56 | """ | |
57 | Update the program file with the latest version from the repository | |
58 | Returns whether the program should terminate | |
59 | """ | |
60 | ||
61 | JSON_URL = 'https://api.github.com/repos/yt-dlp/yt-dlp/releases/latest' | |
62 | ||
63 | def report_error(msg, expected=False): | |
64 | ydl.report_error(msg, tb=False if expected else None) | |
65 | ||
66 | def report_unable(action, expected=False): | |
67 | report_error(f'Unable to {action}', expected) | |
68 | ||
69 | def report_permission_error(file): | |
70 | report_unable(f'write to {file}; Try running as administrator', True) | |
71 | ||
72 | def report_network_error(action, delim=';'): | |
73 | report_unable(f'{action}{delim} Visit https://github.com/yt-dlp/yt-dlp/releases/latest', True) | |
74 | ||
75 | def calc_sha256sum(path): | |
76 | h = hashlib.sha256() | |
77 | b = bytearray(128 * 1024) | |
78 | mv = memoryview(b) | |
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 | # Download and check versions info | |
85 | try: | |
86 | version_info = ydl._opener.open(JSON_URL).read().decode() | |
87 | version_info = json.loads(version_info) | |
88 | except Exception: | |
89 | return report_network_error('obtain version info', delim='; Please try again later or') | |
90 | ||
91 | def version_tuple(version_str): | |
92 | return tuple(map(int, version_str.split('.'))) | |
93 | ||
94 | version_id = version_info['tag_name'] | |
95 | ydl.to_screen(f'Latest version: {version_id}, Current version: {__version__}') | |
96 | if version_tuple(__version__) >= version_tuple(version_id): | |
97 | ydl.to_screen(f'yt-dlp is up to date ({__version__})') | |
98 | return | |
99 | ||
100 | err = is_non_updateable() | |
101 | if err: | |
102 | return report_error(err, True) | |
103 | ||
104 | variant, filename = get_variant_and_executable_path() | |
105 | filename = compat_realpath(filename) # Absolute path, following symlinks | |
106 | ||
107 | ydl.to_screen(f'Current Build Hash {calc_sha256sum(filename)}') | |
108 | ydl.to_screen(f'Updating to version {version_id} ...') | |
109 | ||
110 | version_labels = { | |
111 | 'zip_3': '', | |
112 | 'win_exe_64': '.exe', | |
113 | 'py2exe_64': '_min.exe', | |
114 | 'win_exe_32': '_x86.exe', | |
115 | 'mac_exe_64': '_macos', | |
116 | } | |
117 | ||
118 | def get_bin_info(bin_or_exe, version): | |
119 | label = version_labels[f'{bin_or_exe}_{version}'] | |
120 | return next((i for i in version_info['assets'] if i['name'] == 'yt-dlp%s' % label), {}) | |
121 | ||
122 | def get_sha256sum(bin_or_exe, version): | |
123 | filename = 'yt-dlp%s' % version_labels[f'{bin_or_exe}_{version}'] | |
124 | urlh = next( | |
125 | (i for i in version_info['assets'] if i['name'] in ('SHA2-256SUMS')), | |
126 | {}).get('browser_download_url') | |
127 | if not urlh: | |
128 | return None | |
129 | hash_data = ydl._opener.open(urlh).read().decode() | |
130 | return dict(ln.split()[::-1] for ln in hash_data.splitlines()).get(filename) | |
131 | ||
132 | if not os.access(filename, os.W_OK): | |
133 | return report_permission_error(filename) | |
134 | ||
135 | if variant in ('win_exe', 'py2exe'): | |
136 | directory = os.path.dirname(filename) | |
137 | if not os.access(directory, os.W_OK): | |
138 | return report_permission_error(directory) | |
139 | try: | |
140 | if os.path.exists(filename + '.old'): | |
141 | os.remove(filename + '.old') | |
142 | except OSError: | |
143 | return report_unable('remove the old version') | |
144 | ||
145 | try: | |
146 | arch = platform.architecture()[0][:2] | |
147 | url = get_bin_info(variant, arch).get('browser_download_url') | |
148 | if not url: | |
149 | return report_network_error('fetch updates') | |
150 | urlh = ydl._opener.open(url) | |
151 | newcontent = urlh.read() | |
152 | urlh.close() | |
153 | except OSError: | |
154 | return report_network_error('download latest version') | |
155 | ||
156 | try: | |
157 | with open(filename + '.new', 'wb') as outf: | |
158 | outf.write(newcontent) | |
159 | except OSError: | |
160 | return report_permission_error(f'{filename}.new') | |
161 | ||
162 | expected_sum = get_sha256sum(variant, arch) | |
163 | if not expected_sum: | |
164 | ydl.report_warning('no hash information found for the release') | |
165 | elif calc_sha256sum(filename + '.new') != expected_sum: | |
166 | report_network_error('verify the new executable') | |
167 | try: | |
168 | os.remove(filename + '.new') | |
169 | except OSError: | |
170 | return report_unable('remove corrupt download') | |
171 | ||
172 | try: | |
173 | os.rename(filename, filename + '.old') | |
174 | except OSError: | |
175 | return report_unable('move current version') | |
176 | try: | |
177 | os.rename(filename + '.new', filename) | |
178 | except OSError: | |
179 | report_unable('overwrite current version') | |
180 | os.rename(filename + '.old', filename) | |
181 | return | |
182 | try: | |
183 | # Continues to run in the background | |
184 | Popen( | |
185 | 'ping 127.0.0.1 -n 5 -w 1000 & del /F "%s.old"' % filename, | |
186 | shell=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) | |
187 | ydl.to_screen('Updated yt-dlp to version %s' % version_id) | |
188 | return True # Exit app | |
189 | except OSError: | |
190 | report_unable('delete the old version') | |
191 | ||
192 | elif variant in ('zip', 'mac_exe'): | |
193 | pack_type = '3' if variant == 'zip' else '64' | |
194 | try: | |
195 | url = get_bin_info(variant, pack_type).get('browser_download_url') | |
196 | if not url: | |
197 | return report_network_error('fetch updates') | |
198 | urlh = ydl._opener.open(url) | |
199 | newcontent = urlh.read() | |
200 | urlh.close() | |
201 | except OSError: | |
202 | return report_network_error('download the latest version') | |
203 | ||
204 | expected_sum = get_sha256sum(variant, pack_type) | |
205 | if not expected_sum: | |
206 | ydl.report_warning('no hash information found for the release') | |
207 | elif hashlib.sha256(newcontent).hexdigest() != expected_sum: | |
208 | return report_network_error('verify the new package') | |
209 | ||
210 | try: | |
211 | with open(filename, 'wb') as outf: | |
212 | outf.write(newcontent) | |
213 | except OSError: | |
214 | return report_unable('overwrite current version') | |
215 | ||
216 | ydl.to_screen('Updated yt-dlp to version %s; Restart yt-dlp to use the new version' % version_id) | |
217 | return | |
218 | ||
219 | assert False, f'Unhandled variant: {variant}' | |
220 | ||
221 | ||
222 | # Deprecated | |
223 | def update_self(to_screen, verbose, opener): | |
224 | ||
225 | printfn = to_screen | |
226 | ||
227 | write_string( | |
228 | 'DeprecationWarning: "yt_dlp.update.update_self" is deprecated and may be removed in a future version. ' | |
229 | 'Use "yt_dlp.update.run_update(ydl)" instead\n') | |
230 | ||
231 | class FakeYDL(): | |
232 | _opener = opener | |
233 | to_screen = printfn | |
234 | ||
235 | @staticmethod | |
236 | def report_warning(msg, *args, **kwargs): | |
237 | return printfn('WARNING: %s' % msg, *args, **kwargs) | |
238 | ||
239 | @staticmethod | |
240 | def report_error(msg, tb=None): | |
241 | printfn('ERROR: %s' % msg) | |
242 | if not verbose: | |
243 | return | |
244 | if tb is None: | |
245 | # Copied from YoutubeDl.trouble | |
246 | if sys.exc_info()[0]: | |
247 | tb = '' | |
248 | if hasattr(sys.exc_info()[1], 'exc_info') and sys.exc_info()[1].exc_info[0]: | |
249 | tb += ''.join(traceback.format_exception(*sys.exc_info()[1].exc_info)) | |
250 | tb += encode_compat_str(traceback.format_exc()) | |
251 | else: | |
252 | tb_data = traceback.format_list(traceback.extract_stack()) | |
253 | tb = ''.join(tb_data) | |
254 | if tb: | |
255 | printfn(tb) | |
256 | ||
257 | return run_update(FakeYDL()) |