]>
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
):
36 ''' Exists for backward compatibility. Use run_update(ydl) instead '''
45 def report_warning(msg
, *args
, **kwargs
):
46 return printfn('WARNING: %s' % msg
, *args
, **kwargs
)
49 def report_error(msg
, tb
=None):
50 printfn('ERROR: %s' % msg
)
54 # Copied from YoutubeDl.trouble
57 if hasattr(sys
.exc_info()[1], 'exc_info') and sys
.exc_info()[1].exc_info
[0]:
58 tb
+= ''.join(traceback
.format_exception(*sys
.exc_info()[1].exc_info
))
59 tb
+= encode_compat_str(traceback
.format_exc())
61 tb_data
= traceback
.format_list(traceback
.extract_stack())
66 return run_update(FakeYDL())
71 Update the program file with the latest version from the repository
72 Returns whether the program should terminate
75 JSON_URL
= 'https://api.github.com/repos/yt-dlp/yt-dlp/releases/latest'
77 def report_error(msg
, network
=False, expected
=False, delim
=';'):
79 msg
+= '%s Visit https://github.com/yt-dlp/yt-dlp/releases/latest' % delim
80 ydl
.report_error(msg
, tb
='' if network
or expected
else None)
82 def calc_sha256sum(path
):
84 b
= bytearray(128 * 1024)
86 with open(os
.path
.realpath(path
), 'rb', buffering
=0) as f
:
87 for n
in iter(lambda: f
.readinto(mv
), 0):
91 if not isinstance(globals().get('__loader__'), zipimporter
) and not hasattr(sys
, 'frozen'):
93 'It looks like you installed yt-dlp with a package manager, pip, setup.py or a tarball. '
94 'Please use that to update', expected
=True)
96 # sys.executable is set to the full pathname of the exe-file for py2exe
97 # though symlinks are not followed so that we need to do this manually
98 # with help of realpath
99 filename
= compat_realpath(sys
.executable
if hasattr(sys
, 'frozen') else sys
.argv
[0])
100 ydl
.to_screen('Current Build Hash %s' % calc_sha256sum(filename
))
102 # Download and check versions info
104 version_info
= ydl
._opener
.open(JSON_URL
).read().decode('utf-8')
105 version_info
= json
.loads(version_info
)
107 return report_error('can\'t obtain versions info. Please try again later ', True, delim
='or')
109 def version_tuple(version_str
):
110 return tuple(map(int, version_str
.split('.')))
112 version_id
= version_info
['tag_name']
113 if version_tuple(__version__
) >= version_tuple(version_id
):
114 ydl
.to_screen('yt-dlp is up to date (%s)' % __version__
)
117 ydl
.to_screen('Updating to version ' + version_id
+ ' ...')
123 'exe_32': '_x86.exe',
126 def get_bin_info(bin_or_exe
, version
):
127 label
= version_labels
['%s_%s' % (bin_or_exe
, version
)]
128 return next((i
for i
in version_info
['assets'] if i
['name'] == 'yt-dlp%s' % label
), {})
130 def get_sha256sum(bin_or_exe
, version
):
131 label
= version_labels
['%s_%s' % (bin_or_exe
, version
)]
133 (i
for i
in version_info
['assets'] if i
['name'] in ('SHA2-256SUMS')),
134 {}).get('browser_download_url')
137 hash_data
= ydl
._opener
.open(urlh
).read().decode('utf-8')
138 hashes
= list(map(lambda x
: x
.split(':'), hash_data
.splitlines()))
139 return next((i
[1] for i
in hashes
if i
[0] == 'yt-dlp%s' % label
), None)
141 if not os
.access(filename
, os
.W_OK
):
142 return report_error('no write permissions on %s' % filename
, expected
=True)
145 if hasattr(sys
, 'frozen'):
147 directory
= os
.path
.dirname(exe
)
148 if not os
.access(directory
, os
.W_OK
):
149 return report_error('no write permissions on %s' % directory
, expected
=True)
152 arch
= platform
.architecture()[0][:2]
153 url
= get_bin_info('exe', arch
).get('browser_download_url')
155 return report_error('unable to fetch updates', True)
156 urlh
= ydl
._opener
.open(url
)
157 newcontent
= urlh
.read()
159 except (IOError, OSError, StopIteration):
160 return report_error('unable to download latest version', True)
163 with open(exe
+ '.new', 'wb') as outf
:
164 outf
.write(newcontent
)
165 except (IOError, OSError):
166 return report_error('unable to write the new version')
168 expected_sum
= get_sha256sum('exe', arch
)
170 ydl
.report_warning('no hash information found for the release')
171 elif calc_sha256sum(exe
+ '.new') != expected_sum
:
172 report_error('unable to verify the new executable', True)
174 os
.remove(exe
+ '.new')
176 return report_error('unable to remove corrupt download')
179 bat
= os
.path
.join(directory
, 'yt-dlp-updater.cmd')
180 with io
.open(bat
, 'w') as batfile
:
183 echo.Waiting for file handle to be closed ...
184 ping 127.0.0.1 -n 5 -w 1000 > NUL
185 move /Y "%s.new" "%s" > NUL
186 echo.Updated yt-dlp to version %s
188 @start /b "" cmd /c del "%%~f0"&exit /b
189 ''' % (exe
, exe
, version_id
))
191 subprocess
.Popen([bat
]) # Continues to run in the background
192 except (IOError, OSError):
193 report_error('unable to overwrite current version')
194 return True # Exit app
197 elif isinstance(globals().get('__loader__'), zipimporter
):
199 py_ver
= platform
.python_version()[0]
200 url
= get_bin_info('zip', py_ver
).get('browser_download_url')
202 return report_error('unable to fetch updates', True)
203 urlh
= ydl
._opener
.open(url
)
204 newcontent
= urlh
.read()
206 except (IOError, OSError, StopIteration):
207 return report_error('unable to download latest version', True)
209 expected_sum
= get_sha256sum('zip', py_ver
)
210 if expected_sum
and hashlib
.sha256(newcontent
).hexdigest() != expected_sum
:
211 return report_error('unable to verify the new zip', True)
214 with open(filename
, 'wb') as outf
:
215 outf
.write(newcontent
)
216 except (IOError, OSError):
217 return report_error('unable to overwrite current version')
219 ydl
.to_screen('Updated yt-dlp to version %s; Restart yt-dlp to use the new version' % version_id
)
223 def get_notes(versions, fromVersion):
225 for v, vdata in sorted(versions.items()):
227 notes.extend(vdata.get('notes', []))
231 def print_notes(to_screen, versions, fromVersion=__version__):
232 notes = get_notes(versions, fromVersion)
234 to_screen('PLEASE NOTE:')