]>
jfr.im git - yt-dlp.git/blob - yt_dlp/update.py
1 from __future__
import unicode_literals
11 from zipimport
import zipimporter
13 from .compat
import compat_realpath
14 from .utils
import encode_compat_str
16 from .version
import __version__
20 def rsa_verify(message, signature, key):
21 from hashlib import sha256
22 assert isinstance(message, bytes)
23 byte_size = (len(bin(key[0])) - 2 + 8 - 1) // 8
24 signature = ('%x' % pow(int(signature, 16), key[1], key[0])).encode()
25 signature = (byte_size * 2 - len(signature)) * b'0' + signature
26 asn1 = b'3031300d060960864801650304020105000420'
27 asn1 += sha256(message).hexdigest().encode()
28 if byte_size < len(asn1) // 2 + 11:
30 expected = b'0001' + (byte_size - len(asn1) // 2 - 3) * b'ff' + b'00' + asn1
31 return expected == signature
35 def update_self(to_screen
, verbose
, opener
):
37 Update the program file with the latest version from the repository
38 Returns whether the program should terminate
41 JSON_URL
= 'https://api.github.com/repos/yt-dlp/yt-dlp/releases/latest'
43 def calc_sha256sum(path
):
45 b
= bytearray(128 * 1024)
47 with open(os
.path
.realpath(path
), 'rb', buffering
=0) as f
:
48 for n
in iter(lambda: f
.readinto(mv
), 0):
52 if not isinstance(globals().get('__loader__'), zipimporter
) and not hasattr(sys
, 'frozen'):
53 to_screen('It looks like you installed yt-dlp with a package manager, pip, setup.py or a tarball. Please use that to update.')
56 # sys.executable is set to the full pathname of the exe-file for py2exe
57 # though symlinks are not followed so that we need to do this manually
58 # with help of realpath
59 filename
= compat_realpath(sys
.executable
if hasattr(sys
, 'frozen') else sys
.argv
[0])
60 to_screen('Current Build Hash %s' % calc_sha256sum(filename
))
62 # Download and check versions info
64 version_info
= opener
.open(JSON_URL
).read().decode('utf-8')
65 version_info
= json
.loads(version_info
)
68 to_screen(encode_compat_str(traceback
.format_exc()))
69 to_screen('ERROR: can\'t obtain versions info. Please try again later.')
70 to_screen('Visit https://github.com/yt-dlp/yt-dlp/releases/latest')
73 def version_tuple(version_str
):
74 return tuple(map(int, version_str
.split('.')))
76 version_id
= version_info
['tag_name']
77 if version_tuple(__version__
) >= version_tuple(version_id
):
78 to_screen('yt-dlp is up to date (%s)' % __version__
)
81 to_screen('Updating to version ' + version_id
+ ' ...')
91 def get_bin_info(bin_or_exe
, version
):
92 label
= version_labels
['%s_%s' % (bin_or_exe
, version
)]
94 (i
for i
in version_info
['assets'] if i
['name'] == 'yt-dlp%s' % label
),
97 def get_sha256sum(bin_or_exe
, version
):
98 label
= version_labels
['%s_%s' % (bin_or_exe
, version
)]
100 (i
for i
in version_info
['assets']
101 if i
['name'] in ('SHA2-256SUMS')), {}).get('browser_download_url')
104 hash_data
= opener
.open(urlh
).read().decode('utf-8')
105 hashes
= list(map(lambda x
: x
.split(':'), hash_data
.splitlines()))
107 (i
[1] for i
in hashes
if i
[0] == 'yt-dlp%s' % label
),
110 if not os
.access(filename
, os
.W_OK
):
111 to_screen('ERROR: no write permissions on %s' % filename
)
115 if hasattr(sys
, 'frozen'):
117 directory
= os
.path
.dirname(exe
)
118 if not os
.access(directory
, os
.W_OK
):
119 to_screen('ERROR: no write permissions on %s' % directory
)
123 arch
= platform
.architecture()[0][:2]
124 url
= get_bin_info('exe', arch
).get('browser_download_url')
126 to_screen('ERROR: unable to fetch updates')
127 to_screen('Visit https://github.com/yt-dlp/yt-dlp/releases/latest')
129 urlh
= opener
.open(url
)
130 newcontent
= urlh
.read()
132 except (IOError, OSError, StopIteration):
134 to_screen(encode_compat_str(traceback
.format_exc()))
135 to_screen('ERROR: unable to download latest version')
136 to_screen('Visit https://github.com/yt-dlp/yt-dlp/releases/latest')
140 with open(exe
+ '.new', 'wb') as outf
:
141 outf
.write(newcontent
)
142 except (IOError, OSError):
144 to_screen(encode_compat_str(traceback
.format_exc()))
145 to_screen('ERROR: unable to write the new version')
148 expected_sum
= get_sha256sum('exe', arch
)
150 to_screen('WARNING: no hash information found for the release')
151 elif calc_sha256sum(exe
+ '.new') != expected_sum
:
152 to_screen('ERROR: unable to verify the new executable')
153 to_screen('Visit https://github.com/yt-dlp/yt-dlp/releases/latest')
155 os
.remove(exe
+ '.new')
157 to_screen('ERROR: unable to remove corrupt download')
161 bat
= os
.path
.join(directory
, 'yt-dlp-updater.cmd')
162 with io
.open(bat
, 'w') as batfile
:
165 echo.Waiting for file handle to be closed ...
166 ping 127.0.0.1 -n 5 -w 1000 > NUL
167 move /Y "%s.new" "%s" > NUL
168 echo.Updated yt-dlp to version %s.
170 @start /b "" cmd /c del "%%~f0"&exit /b
171 ''' % (exe
, exe
, version_id
))
173 subprocess
.Popen([bat
]) # Continues to run in the background
174 return True # Exit app
175 except (IOError, OSError):
177 to_screen(encode_compat_str(traceback
.format_exc()))
178 to_screen('ERROR: unable to overwrite current version')
182 elif isinstance(globals().get('__loader__'), zipimporter
):
184 py_ver
= platform
.python_version()[0]
185 url
= get_bin_info('zip', py_ver
).get('browser_download_url')
187 to_screen('ERROR: unable to fetch updates')
188 to_screen('Visit https://github.com/yt-dlp/yt-dlp/releases/latest')
190 urlh
= opener
.open(url
)
191 newcontent
= urlh
.read()
193 except (IOError, OSError, StopIteration):
195 to_screen(encode_compat_str(traceback
.format_exc()))
196 to_screen('ERROR: unable to download latest version')
197 to_screen('Visit https://github.com/yt-dlp/yt-dlp/releases/latest')
200 expected_sum
= get_sha256sum('zip', py_ver
)
201 if expected_sum
and hashlib
.sha256(newcontent
).hexdigest() != expected_sum
:
202 to_screen('ERROR: unable to verify the new zip')
203 to_screen('Visit https://github.com/yt-dlp/yt-dlp/releases/latest')
207 with open(filename
, 'wb') as outf
:
208 outf
.write(newcontent
)
209 except (IOError, OSError):
211 to_screen(encode_compat_str(traceback
.format_exc()))
212 to_screen('ERROR: unable to overwrite current version')
215 to_screen('Updated yt-dlp. Restart yt-dlp to use the new version.')
219 def get_notes(versions, fromVersion):
221 for v, vdata in sorted(versions.items()):
223 notes.extend(vdata.get('notes', []))
227 def print_notes(to_screen, versions, fromVersion=__version__):
228 notes = get_notes(versions, fromVersion)
230 to_screen('PLEASE NOTE:')