1 from __future__
import unicode_literals
10 from zipimport
import zipimporter
12 from .compat
import compat_realpath
13 from .utils
import encode_compat_str
, Popen
, write_string
15 from .version
import __version__
19 def rsa_verify(message, signature, key):
20 from hashlib import sha256
21 assert isinstance(message, bytes)
22 byte_size = (len(bin(key[0])) - 2 + 8 - 1) // 8
23 signature = ('%x' % pow(int(signature, 16), key[1], key[0])).encode()
24 signature = (byte_size * 2 - len(signature)) * b'0' + signature
25 asn1 = b'3031300d060960864801650304020105000420'
26 asn1 += sha256(message).hexdigest().encode()
27 if byte_size < len(asn1) // 2 + 11:
29 expected = b'0001' + (byte_size - len(asn1) // 2 - 3) * b'ff' + b'00' + asn1
30 return expected == signature
35 if hasattr(sys
, 'frozen'):
36 prefix
= 'mac' if sys
.platform
== 'darwin' else 'win'
37 if getattr(sys
, '_MEIPASS', None):
38 if sys
._MEIPASS
== os
.path
.dirname(sys
.executable
):
39 return f
'{prefix}_dir'
40 return f
'{prefix}_exe'
42 elif isinstance(globals().get('__loader__'), zipimporter
):
44 elif os
.path
.basename(sys
.argv
[0]) == '__main__.py':
49 _NON_UPDATEABLE_REASONS
= {
54 'win_dir': 'Auto-update is not supported for unpackaged windows executable; Re-download the latest release',
55 'mac_dir': 'Auto-update is not supported for unpackaged MacOS executable; Re-download the latest release',
56 'source': 'You cannot update when running from source code; Use git to pull the latest changes',
57 'unknown': 'It looks like you installed yt-dlp with a package manager, pip or setup.py; Use that to update',
61 def is_non_updateable():
62 return _NON_UPDATEABLE_REASONS
.get(detect_variant(), _NON_UPDATEABLE_REASONS
['unknown'])
67 Update the program file with the latest version from the repository
68 Returns whether the program should terminate
71 JSON_URL
= 'https://api.github.com/repos/yt-dlp/yt-dlp/releases/latest'
73 def report_error(msg
, expected
=False):
74 ydl
.report_error(msg
, tb
='' if expected
else None)
76 def report_unable(action
, expected
=False):
77 report_error(f
'Unable to {action}', expected
)
79 def report_permission_error(file):
80 report_unable(f
'write to {file}; Try running as administrator', True)
82 def report_network_error(action
, delim
=';'):
83 report_unable(f
'{action}{delim} Visit https://github.com/yt-dlp/yt-dlp/releases/latest', True)
85 def calc_sha256sum(path
):
87 b
= bytearray(128 * 1024)
89 with
open(os
.path
.realpath(path
), 'rb', buffering
=0) as f
:
90 for n
in iter(lambda: f
.readinto(mv
), 0):
94 # Download and check versions info
96 version_info
= ydl
._opener
.open(JSON_URL
).read().decode('utf-8')
97 version_info
= json
.loads(version_info
)
99 return report_network_error('obtain version info', delim
='; Please try again later or')
101 def version_tuple(version_str
):
102 return tuple(map(int, version_str
.split('.')))
104 version_id
= version_info
['tag_name']
105 ydl
.to_screen(f
'Latest version: {version_id}, Current version: {__version__}')
106 if version_tuple(__version__
) >= version_tuple(version_id
):
107 ydl
.to_screen(f
'yt-dlp is up to date ({__version__})')
110 err
= is_non_updateable()
112 return report_error(err
, True)
114 # sys.executable is set to the full pathname of the exe-file for py2exe
115 # though symlinks are not followed so that we need to do this manually
116 # with help of realpath
117 filename
= compat_realpath(sys
.executable
if hasattr(sys
, 'frozen') else sys
.argv
[0])
118 ydl
.to_screen(f
'Current Build Hash {calc_sha256sum(filename)}')
119 ydl
.to_screen(f
'Updating to version {version_id} ...')
123 'win_exe_64': '.exe',
124 'py2exe_64': '_min.exe',
125 'win_exe_32': '_x86.exe',
126 'mac_exe_64': '_macos',
129 def get_bin_info(bin_or_exe
, version
):
130 label
= version_labels
['%s_%s' % (bin_or_exe
, version
)]
131 return next((i
for i
in version_info
['assets'] if i
['name'] == 'yt-dlp%s' % label
), {})
133 def get_sha256sum(bin_or_exe
, version
):
134 filename
= 'yt-dlp%s' % version_labels
['%s_%s' % (bin_or_exe
, version
)]
136 (i
for i
in version_info
['assets'] if i
['name'] in ('SHA2-256SUMS')),
137 {}).get('browser_download_url')
140 hash_data
= ydl
._opener
.open(urlh
).read().decode('utf-8')
141 return dict(ln
.split()[::-1] for ln
in hash_data
.splitlines()).get(filename
)
143 if not os
.access(filename
, os
.W_OK
):
144 return report_permission_error(filename
)
147 variant
= detect_variant()
148 if variant
in ('win_exe', 'py2exe'):
149 directory
= os
.path
.dirname(filename
)
150 if not os
.access(directory
, os
.W_OK
):
151 return report_permission_error(directory
)
153 if os
.path
.exists(filename
+ '.old'):
154 os
.remove(filename
+ '.old')
155 except (IOError, OSError):
156 return report_unable('remove the old version')
159 arch
= platform
.architecture()[0][:2]
160 url
= get_bin_info(variant
, arch
).get('browser_download_url')
162 return report_network_error('fetch updates')
163 urlh
= ydl
._opener
.open(url
)
164 newcontent
= urlh
.read()
166 except (IOError, OSError):
167 return report_network_error('download latest version')
170 with
open(filename
+ '.new', 'wb') as outf
:
171 outf
.write(newcontent
)
172 except (IOError, OSError):
173 return report_permission_error(f
'{filename}.new')
175 expected_sum
= get_sha256sum(variant
, arch
)
177 ydl
.report_warning('no hash information found for the release')
178 elif calc_sha256sum(filename
+ '.new') != expected_sum
:
179 report_network_error('verify the new executable')
181 os
.remove(filename
+ '.new')
183 return report_unable('remove corrupt download')
186 os
.rename(filename
, filename
+ '.old')
187 except (IOError, OSError):
188 return report_unable('move current version')
190 os
.rename(filename
+ '.new', filename
)
191 except (IOError, OSError):
192 report_unable('overwrite current version')
193 os
.rename(filename
+ '.old', filename
)
196 # Continues to run in the background
198 'ping 127.0.0.1 -n 5 -w 1000 & del /F "%s.old"' % filename
,
199 shell
=True, stdout
=subprocess
.DEVNULL
, stderr
=subprocess
.DEVNULL
)
200 ydl
.to_screen('Updated yt-dlp to version %s' % version_id
)
201 return True # Exit app
203 report_unable('delete the old version')
205 elif variant
in ('zip', 'mac_exe'):
206 pack_type
= '3' if variant
== 'zip' else '64'
208 url
= get_bin_info(variant
, pack_type
).get('browser_download_url')
210 return report_network_error('fetch updates')
211 urlh
= ydl
._opener
.open(url
)
212 newcontent
= urlh
.read()
214 except (IOError, OSError):
215 return report_network_error('download the latest version')
217 expected_sum
= get_sha256sum(variant
, pack_type
)
219 ydl
.report_warning('no hash information found for the release')
220 elif hashlib
.sha256(newcontent
).hexdigest() != expected_sum
:
221 return report_network_error('verify the new package')
224 with
open(filename
, 'wb') as outf
:
225 outf
.write(newcontent
)
226 except (IOError, OSError):
227 return report_unable('overwrite current version')
229 ydl
.to_screen('Updated yt-dlp to version %s; Restart yt-dlp to use the new version' % version_id
)
232 assert False, f
'Unhandled variant: {variant}'
236 def get_notes(versions, fromVersion):
238 for v, vdata in sorted(versions.items()):
240 notes.extend(vdata.get('notes', []))
244 def print_notes(to_screen, versions, fromVersion=__version__):
245 notes = get_notes(versions, fromVersion)
247 to_screen('PLEASE NOTE:')
254 def update_self(to_screen
, verbose
, opener
):
259 'DeprecationWarning: "yt_dlp.update.update_self" is deprecated and may be removed in a future version. '
260 'Use "yt_dlp.update.run_update(ydl)" instead\n')
267 def report_warning(msg
, *args
, **kwargs
):
268 return printfn('WARNING: %s' % msg
, *args
, **kwargs
)
271 def report_error(msg
, tb
=None):
272 printfn('ERROR: %s' % msg
)
276 # Copied from YoutubeDl.trouble
277 if sys
.exc_info()[0]:
279 if hasattr(sys
.exc_info()[1], 'exc_info') and sys
.exc_info()[1].exc_info
[0]:
280 tb
+= ''.join(traceback
.format_exception(*sys
.exc_info()[1].exc_info
))
281 tb
+= encode_compat_str(traceback
.format_exc())
283 tb_data
= traceback
.format_list(traceback
.extract_stack())
284 tb
= ''.join(tb_data
)
288 return run_update(FakeYDL())