]>
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 to_screen('Current Build Hash %s' % calc_sha256sum(sys
.executable
))
54 if not isinstance(globals().get('__loader__'), zipimporter
) and not hasattr(sys
, 'frozen'):
55 to_screen('It looks like you installed yt-dlp with a package manager, pip, setup.py or a tarball. Please use that to update.')
58 # Download and check versions info
60 version_info
= opener
.open(JSON_URL
).read().decode('utf-8')
61 version_info
= json
.loads(version_info
)
64 to_screen(encode_compat_str(traceback
.format_exc()))
65 to_screen('ERROR: can\'t obtain versions info. Please try again later.')
66 to_screen('Visit https://github.com/yt-dlp/yt-dlp/releases/latest')
69 def version_tuple(version_str
):
70 return tuple(map(int, version_str
.split('.')))
72 version_id
= version_info
['tag_name']
73 if version_tuple(__version__
) >= version_tuple(version_id
):
74 to_screen('yt-dlp is up to date (%s)' % __version__
)
77 to_screen('Updating to version ' + version_id
+ ' ...')
87 def get_bin_info(bin_or_exe
, version
):
88 label
= version_labels
['%s_%s' % (bin_or_exe
, version
)]
90 (i
for i
in version_info
['assets'] if i
['name'] == 'yt-dlp%s' % label
),
93 def get_sha256sum(bin_or_exe
, version
):
94 label
= version_labels
['%s_%s' % (bin_or_exe
, version
)]
96 (i
for i
in version_info
['assets']
97 if i
['name'] in ('SHA2-256SUMS')), {}).get('browser_download_url')
100 hash_data
= opener
.open(urlh
).read().decode('utf-8')
101 hashes
= list(map(lambda x
: x
.split(':'), hash_data
.splitlines()))
103 (i
[1] for i
in hashes
if i
[0] == 'yt-dlp%s' % label
),
106 # sys.executable is set to the full pathname of the exe-file for py2exe
107 # though symlinks are not followed so that we need to do this manually
108 # with help of realpath
109 filename
= compat_realpath(sys
.executable
if hasattr(sys
, 'frozen') else sys
.argv
[0])
111 if not os
.access(filename
, os
.W_OK
):
112 to_screen('ERROR: no write permissions on %s' % filename
)
116 if hasattr(sys
, 'frozen'):
118 directory
= os
.path
.dirname(exe
)
119 if not os
.access(directory
, os
.W_OK
):
120 to_screen('ERROR: no write permissions on %s' % directory
)
124 arch
= platform
.architecture()[0][:2]
125 url
= get_bin_info('exe', arch
).get('browser_download_url')
127 to_screen('ERROR: unable to fetch updates')
128 to_screen('Visit https://github.com/yt-dlp/yt-dlp/releases/latest')
130 urlh
= opener
.open(url
)
131 newcontent
= urlh
.read()
133 except (IOError, OSError, StopIteration):
135 to_screen(encode_compat_str(traceback
.format_exc()))
136 to_screen('ERROR: unable to download latest version')
137 to_screen('Visit https://github.com/yt-dlp/yt-dlp/releases/latest')
141 with open(exe
+ '.new', 'wb') as outf
:
142 outf
.write(newcontent
)
143 except (IOError, OSError):
145 to_screen(encode_compat_str(traceback
.format_exc()))
146 to_screen('ERROR: unable to write the new version')
149 expected_sum
= get_sha256sum('exe', arch
)
151 to_screen('WARNING: no hash information found for the release')
152 elif calc_sha256sum(exe
+ '.new') != expected_sum
:
153 to_screen('ERROR: unable to verify the new executable')
154 to_screen('Visit https://github.com/yt-dlp/yt-dlp/releases/latest')
156 os
.remove(exe
+ '.new')
158 to_screen('ERROR: unable to remove corrupt download')
162 bat
= os
.path
.join(directory
, 'yt-dlp-updater.cmd')
163 with io
.open(bat
, 'w') as batfile
:
166 echo.Waiting for file handle to be closed ...
167 ping 127.0.0.1 -n 5 -w 1000 > NUL
168 move /Y "%s.new" "%s" > NUL
169 echo.Updated yt-dlp to version %s.
171 @start /b "" cmd /c del "%%~f0"&exit /b
172 ''' % (exe
, exe
, version_id
))
174 subprocess
.Popen([bat
]) # Continues to run in the background
175 return True # Exit app
176 except (IOError, OSError):
178 to_screen(encode_compat_str(traceback
.format_exc()))
179 to_screen('ERROR: unable to overwrite current version')
183 elif isinstance(globals().get('__loader__'), zipimporter
):
185 py_ver
= platform
.python_version()[0]
186 url
= get_bin_info('zip', py_ver
).get('browser_download_url')
188 to_screen('ERROR: unable to fetch updates')
189 to_screen('Visit https://github.com/yt-dlp/yt-dlp/releases/latest')
191 urlh
= opener
.open(url
)
192 newcontent
= urlh
.read()
194 except (IOError, OSError, StopIteration):
196 to_screen(encode_compat_str(traceback
.format_exc()))
197 to_screen('ERROR: unable to download latest version')
198 to_screen('Visit https://github.com/yt-dlp/yt-dlp/releases/latest')
202 with open(filename
+ '.new', 'wb') as outf
:
203 outf
.write(newcontent
)
204 except (IOError, OSError):
206 to_screen(encode_compat_str(traceback
.format_exc()))
207 to_screen('ERROR: unable to write the new version')
210 expected_sum
= get_sha256sum('zip', py_ver
)
211 if expected_sum
and calc_sha256sum(filename
+ '.new') != expected_sum
:
212 to_screen('ERROR: unable to verify the new zip')
213 to_screen('Visit https://github.com/yt-dlp/yt-dlp/releases/latest')
215 os
.remove(filename
+ '.new')
217 to_screen('ERROR: unable to remove corrupt zip')
221 os
.rename(filename
+ '.new', filename
)
223 to_screen('ERROR: unable to overwrite current version')
226 to_screen('Updated yt-dlp. Restart yt-dlp to use the new version.')
230 def get_notes(versions, fromVersion):
232 for v, vdata in sorted(versions.items()):
234 notes.extend(vdata.get('notes', []))
238 def print_notes(to_screen, versions, fromVersion=__version__):
239 notes = get_notes(versions, fromVersion)
241 to_screen('PLEASE NOTE:')