]>
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 |
c19bc311 | 7 | import traceback |
d5ed35b6 FV |
8 | from zipimport import zipimporter |
9 | ||
0b9c08b4 | 10 | from .compat import compat_realpath, functools |
f8271158 | 11 | from .utils import Popen, encode_compat_str, write_string |
d5ed35b6 FV |
12 | from .version import __version__ |
13 | ||
5f6a1245 | 14 | |
0b9c08b4 | 15 | @functools.cache |
c487cf00 | 16 | def get_variant_and_executable_path(): |
17 | """@returns (variant, executable_path)""" | |
5d535b4a | 18 | if hasattr(sys, 'frozen'): |
c487cf00 | 19 | path = sys.executable |
0e5927ee | 20 | prefix = 'mac' if sys.platform == 'darwin' else 'win' |
5d535b4a | 21 | if getattr(sys, '_MEIPASS', None): |
22 | if sys._MEIPASS == os.path.dirname(sys.executable): | |
c487cf00 | 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, '..') | |
4c88ff87 | 30 | elif os.path.basename(sys.argv[0]) == '__main__.py': |
c487cf00 | 31 | return 'source', path |
32 | return 'unknown', path | |
33 | ||
34 | ||
35 | def detect_variant(): | |
36 | return get_variant_and_executable_path()[0] | |
4c88ff87 | 37 | |
38 | ||
5d535b4a | 39 | _NON_UPDATEABLE_REASONS = { |
0e5927ee | 40 | 'win_exe': None, |
5d535b4a | 41 | 'zip': None, |
0e5927ee | 42 | 'mac_exe': None, |
386cdfdb | 43 | 'py2exe': None, |
0e5927ee R |
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', | |
e6faf2be | 46 | 'source': 'You cannot update when running from source code; Use git to pull the latest changes', |
455a15e2 | 47 | 'unknown': 'It looks like you installed yt-dlp with a package manager, pip or setup.py; Use that to update', |
5d535b4a | 48 | } |
49 | ||
50 | ||
51 | def is_non_updateable(): | |
52 | return _NON_UPDATEABLE_REASONS.get(detect_variant(), _NON_UPDATEABLE_REASONS['unknown']) | |
53 | ||
54 | ||
c19bc311 | 55 | def run_update(ydl): |
e5813e53 | 56 | """ |
57 | Update the program file with the latest version from the repository | |
58 | Returns whether the program should terminate | |
59 | """ | |
d5ed35b6 | 60 | |
7a5c1cfe | 61 | JSON_URL = 'https://api.github.com/repos/yt-dlp/yt-dlp/releases/latest' |
d5ed35b6 | 62 | |
e6faf2be | 63 | def report_error(msg, expected=False): |
c487cf00 | 64 | ydl.report_error(msg, tb=False if expected else None) |
e6faf2be | 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) | |
c19bc311 | 74 | |
44f705d0 | 75 | def calc_sha256sum(path): |
fa57af1e U |
76 | h = hashlib.sha256() |
77 | b = bytearray(128 * 1024) | |
78 | mv = memoryview(b) | |
44f705d0 | 79 | with open(os.path.realpath(path), 'rb', buffering=0) as f: |
fa57af1e U |
80 | for n in iter(lambda: f.readinto(mv), 0): |
81 | h.update(mv[:n]) | |
82 | return h.hexdigest() | |
83 | ||
28234287 | 84 | # Download and check versions info |
85 | try: | |
0f06bcd7 | 86 | version_info = ydl._opener.open(JSON_URL).read().decode() |
28234287 | 87 | version_info = json.loads(version_info) |
88 | except Exception: | |
e6faf2be | 89 | return report_network_error('obtain version info', delim='; Please try again later or') |
28234287 | 90 | |
91 | def version_tuple(version_str): | |
92 | return tuple(map(int, version_str.split('.'))) | |
93 | ||
94 | version_id = version_info['tag_name'] | |
75b725a7 | 95 | ydl.to_screen(f'Latest version: {version_id}, Current version: {__version__}') |
28234287 | 96 | if version_tuple(__version__) >= version_tuple(version_id): |
97 | ydl.to_screen(f'yt-dlp is up to date ({__version__})') | |
98 | return | |
99 | ||
5d535b4a | 100 | err = is_non_updateable() |
4040428e | 101 | if err: |
e6faf2be | 102 | return report_error(err, True) |
d5ed35b6 | 103 | |
c487cf00 | 104 | variant, filename = get_variant_and_executable_path() |
105 | filename = compat_realpath(filename) # Absolute path, following symlinks | |
106 | ||
91f071af | 107 | ydl.to_screen(f'Current Build Hash {calc_sha256sum(filename)}') |
28234287 | 108 | ydl.to_screen(f'Updating to version {version_id} ...') |
3bf79c75 | 109 | |
44f705d0 | 110 | version_labels = { |
111 | 'zip_3': '', | |
386cdfdb | 112 | 'win_exe_64': '.exe', |
113 | 'py2exe_64': '_min.exe', | |
114 | 'win_exe_32': '_x86.exe', | |
115 | 'mac_exe_64': '_macos', | |
44f705d0 | 116 | } |
117 | ||
e5813e53 | 118 | def get_bin_info(bin_or_exe, version): |
86e5f3ed | 119 | label = version_labels[f'{bin_or_exe}_{version}'] |
c19bc311 | 120 | return next((i for i in version_info['assets'] if i['name'] == 'yt-dlp%s' % label), {}) |
44f705d0 | 121 | |
122 | def get_sha256sum(bin_or_exe, version): | |
86e5f3ed | 123 | filename = 'yt-dlp%s' % version_labels[f'{bin_or_exe}_{version}'] |
44f705d0 | 124 | urlh = next( |
c19bc311 | 125 | (i for i in version_info['assets'] if i['name'] in ('SHA2-256SUMS')), |
126 | {}).get('browser_download_url') | |
44f705d0 | 127 | if not urlh: |
128 | return None | |
0f06bcd7 | 129 | hash_data = ydl._opener.open(urlh).read().decode() |
4c88ff87 | 130 | return dict(ln.split()[::-1] for ln in hash_data.splitlines()).get(filename) |
d5ed35b6 FV |
131 | |
132 | if not os.access(filename, os.W_OK): | |
e6faf2be | 133 | return report_permission_error(filename) |
d5ed35b6 | 134 | |
386cdfdb | 135 | if variant in ('win_exe', 'py2exe'): |
136 | directory = os.path.dirname(filename) | |
d5ed35b6 | 137 | if not os.access(directory, os.W_OK): |
e6faf2be | 138 | return report_permission_error(directory) |
b25522ba | 139 | try: |
140 | if os.path.exists(filename + '.old'): | |
141 | os.remove(filename + '.old') | |
86e5f3ed | 142 | except OSError: |
e6faf2be | 143 | return report_unable('remove the old version') |
d5ed35b6 FV |
144 | |
145 | try: | |
e5813e53 | 146 | arch = platform.architecture()[0][:2] |
386cdfdb | 147 | url = get_bin_info(variant, arch).get('browser_download_url') |
44f705d0 | 148 | if not url: |
e6faf2be | 149 | return report_network_error('fetch updates') |
c19bc311 | 150 | urlh = ydl._opener.open(url) |
d5ed35b6 FV |
151 | newcontent = urlh.read() |
152 | urlh.close() | |
86e5f3ed | 153 | except OSError: |
e6faf2be | 154 | return report_network_error('download latest version') |
d5ed35b6 FV |
155 | |
156 | try: | |
733d8e8f | 157 | with open(filename + '.new', 'wb') as outf: |
d5ed35b6 | 158 | outf.write(newcontent) |
86e5f3ed | 159 | except OSError: |
733d8e8f | 160 | return report_permission_error(f'{filename}.new') |
d5ed35b6 | 161 | |
733d8e8f | 162 | expected_sum = get_sha256sum(variant, arch) |
44f705d0 | 163 | if not expected_sum: |
c19bc311 | 164 | ydl.report_warning('no hash information found for the release') |
733d8e8f | 165 | elif calc_sha256sum(filename + '.new') != expected_sum: |
e6faf2be | 166 | report_network_error('verify the new executable') |
44f705d0 | 167 | try: |
733d8e8f | 168 | os.remove(filename + '.new') |
44f705d0 | 169 | except OSError: |
e6faf2be | 170 | return report_unable('remove corrupt download') |
44f705d0 | 171 | |
d5ed35b6 | 172 | try: |
733d8e8f | 173 | os.rename(filename, filename + '.old') |
86e5f3ed | 174 | except OSError: |
e6faf2be | 175 | return report_unable('move current version') |
b25522ba | 176 | try: |
733d8e8f | 177 | os.rename(filename + '.new', filename) |
86e5f3ed | 178 | except OSError: |
e6faf2be | 179 | report_unable('overwrite current version') |
733d8e8f | 180 | os.rename(filename + '.old', filename) |
b25522ba | 181 | return |
182 | try: | |
183 | # Continues to run in the background | |
d3c93ec2 | 184 | Popen( |
733d8e8f | 185 | 'ping 127.0.0.1 -n 5 -w 1000 & del /F "%s.old"' % filename, |
b25522ba | 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: | |
e6faf2be | 190 | report_unable('delete the old version') |
d5ed35b6 | 191 | |
0e5927ee | 192 | elif variant in ('zip', 'mac_exe'): |
386cdfdb | 193 | pack_type = '3' if variant == 'zip' else '64' |
d5ed35b6 | 194 | try: |
386cdfdb | 195 | url = get_bin_info(variant, pack_type).get('browser_download_url') |
44f705d0 | 196 | if not url: |
e6faf2be | 197 | return report_network_error('fetch updates') |
c19bc311 | 198 | urlh = ydl._opener.open(url) |
d5ed35b6 FV |
199 | newcontent = urlh.read() |
200 | urlh.close() | |
86e5f3ed | 201 | except OSError: |
e6faf2be | 202 | return report_network_error('download the latest version') |
d5ed35b6 | 203 | |
386cdfdb | 204 | expected_sum = get_sha256sum(variant, pack_type) |
beb982be NA |
205 | if not expected_sum: |
206 | ydl.report_warning('no hash information found for the release') | |
207 | elif hashlib.sha256(newcontent).hexdigest() != expected_sum: | |
0e5927ee | 208 | return report_network_error('verify the new package') |
44f705d0 | 209 | |
210 | try: | |
c4a508ab | 211 | with open(filename, 'wb') as outf: |
212 | outf.write(newcontent) | |
86e5f3ed | 213 | except OSError: |
e6faf2be | 214 | return report_unable('overwrite current version') |
d5ed35b6 | 215 | |
0e5927ee R |
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}' | |
3bf79c75 | 220 | |
5f6a1245 | 221 | |
ee8dd27a | 222 | # Deprecated |
e6faf2be | 223 | def update_self(to_screen, verbose, opener): |
e6faf2be | 224 | |
225 | printfn = to_screen | |
226 | ||
ee8dd27a | 227 | write_string( |
228 | 'DeprecationWarning: "yt_dlp.update.update_self" is deprecated and may be removed in a future version. ' | |
b69fd25c | 229 | 'Use "yt_dlp.update.run_update(ydl)" instead\n') |
e6faf2be | 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()) |