]> jfr.im git - yt-dlp.git/blob - yt_dlp/update.py
[update] Fix current build hash for UNIX
[yt-dlp.git] / yt_dlp / update.py
1 from __future__ import unicode_literals
2
3 import io
4 import json
5 import traceback
6 import hashlib
7 import os
8 import platform
9 import subprocess
10 import sys
11 from zipimport import zipimporter
12
13 from .compat import compat_realpath
14 from .utils import encode_compat_str
15
16 from .version import __version__
17
18
19 ''' # Not signed
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:
29 return False
30 expected = b'0001' + (byte_size - len(asn1) // 2 - 3) * b'ff' + b'00' + asn1
31 return expected == signature
32 '''
33
34
35 def update_self(to_screen, verbose, opener):
36 """
37 Update the program file with the latest version from the repository
38 Returns whether the program should terminate
39 """
40
41 JSON_URL = 'https://api.github.com/repos/yt-dlp/yt-dlp/releases/latest'
42
43 def calc_sha256sum(path):
44 h = hashlib.sha256()
45 b = bytearray(128 * 1024)
46 mv = memoryview(b)
47 with open(os.path.realpath(path), 'rb', buffering=0) as f:
48 for n in iter(lambda: f.readinto(mv), 0):
49 h.update(mv[:n])
50 return h.hexdigest()
51
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.')
54 return
55
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))
61
62 # Download and check versions info
63 try:
64 version_info = opener.open(JSON_URL).read().decode('utf-8')
65 version_info = json.loads(version_info)
66 except Exception:
67 if verbose:
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')
71 return
72
73 def version_tuple(version_str):
74 return tuple(map(int, version_str.split('.')))
75
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__)
79 return
80
81 to_screen('Updating to version ' + version_id + ' ...')
82
83 version_labels = {
84 'zip_3': '',
85 'zip_2': '',
86 # 'zip_2': '_py2',
87 'exe_64': '.exe',
88 'exe_32': '_x86.exe',
89 }
90
91 def get_bin_info(bin_or_exe, version):
92 label = version_labels['%s_%s' % (bin_or_exe, version)]
93 return next(
94 (i for i in version_info['assets'] if i['name'] == 'yt-dlp%s' % label),
95 {})
96
97 def get_sha256sum(bin_or_exe, version):
98 label = version_labels['%s_%s' % (bin_or_exe, version)]
99 urlh = next(
100 (i for i in version_info['assets']
101 if i['name'] in ('SHA2-256SUMS')), {}).get('browser_download_url')
102 if not urlh:
103 return None
104 hash_data = opener.open(urlh).read().decode('utf-8')
105 hashes = list(map(lambda x: x.split(':'), hash_data.splitlines()))
106 return next(
107 (i[1] for i in hashes if i[0] == 'yt-dlp%s' % label),
108 None)
109
110 if not os.access(filename, os.W_OK):
111 to_screen('ERROR: no write permissions on %s' % filename)
112 return
113
114 # PyInstaller
115 if hasattr(sys, 'frozen'):
116 exe = filename
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)
120 return
121
122 try:
123 arch = platform.architecture()[0][:2]
124 url = get_bin_info('exe', arch).get('browser_download_url')
125 if not url:
126 to_screen('ERROR: unable to fetch updates')
127 to_screen('Visit https://github.com/yt-dlp/yt-dlp/releases/latest')
128 return
129 urlh = opener.open(url)
130 newcontent = urlh.read()
131 urlh.close()
132 except (IOError, OSError, StopIteration):
133 if verbose:
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')
137 return
138
139 try:
140 with open(exe + '.new', 'wb') as outf:
141 outf.write(newcontent)
142 except (IOError, OSError):
143 if verbose:
144 to_screen(encode_compat_str(traceback.format_exc()))
145 to_screen('ERROR: unable to write the new version')
146 return
147
148 expected_sum = get_sha256sum('exe', arch)
149 if not expected_sum:
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')
154 try:
155 os.remove(exe + '.new')
156 except OSError:
157 to_screen('ERROR: unable to remove corrupt download')
158 return
159
160 try:
161 bat = os.path.join(directory, 'yt-dlp-updater.cmd')
162 with io.open(bat, 'w') as batfile:
163 batfile.write('''
164 @(
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.
169 )
170 @start /b "" cmd /c del "%%~f0"&exit /b
171 ''' % (exe, exe, version_id))
172
173 subprocess.Popen([bat]) # Continues to run in the background
174 return True # Exit app
175 except (IOError, OSError):
176 if verbose:
177 to_screen(encode_compat_str(traceback.format_exc()))
178 to_screen('ERROR: unable to overwrite current version')
179 return
180
181 # Zip unix package
182 elif isinstance(globals().get('__loader__'), zipimporter):
183 try:
184 py_ver = platform.python_version()[0]
185 url = get_bin_info('zip', py_ver).get('browser_download_url')
186 if not url:
187 to_screen('ERROR: unable to fetch updates')
188 to_screen('Visit https://github.com/yt-dlp/yt-dlp/releases/latest')
189 return
190 urlh = opener.open(url)
191 newcontent = urlh.read()
192 urlh.close()
193 except (IOError, OSError, StopIteration):
194 if verbose:
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')
198 return
199
200 try:
201 with open(filename + '.new', 'wb') as outf:
202 outf.write(newcontent)
203 except (IOError, OSError):
204 if verbose:
205 to_screen(encode_compat_str(traceback.format_exc()))
206 to_screen('ERROR: unable to write the new version')
207 return
208
209 expected_sum = get_sha256sum('zip', py_ver)
210 if expected_sum and calc_sha256sum(filename + '.new') != expected_sum:
211 to_screen('ERROR: unable to verify the new zip')
212 to_screen('Visit https://github.com/yt-dlp/yt-dlp/releases/latest')
213 try:
214 os.remove(filename + '.new')
215 except OSError:
216 to_screen('ERROR: unable to remove corrupt zip')
217 return
218
219 try:
220 os.rename(filename + '.new', filename)
221 except OSError:
222 to_screen('ERROR: unable to overwrite current version')
223 return
224
225 to_screen('Updated yt-dlp. Restart yt-dlp to use the new version.')
226
227
228 ''' # UNUSED
229 def get_notes(versions, fromVersion):
230 notes = []
231 for v, vdata in sorted(versions.items()):
232 if v > fromVersion:
233 notes.extend(vdata.get('notes', []))
234 return notes
235
236
237 def print_notes(to_screen, versions, fromVersion=__version__):
238 notes = get_notes(versions, fromVersion)
239 if notes:
240 to_screen('PLEASE NOTE:')
241 for note in notes:
242 to_screen(note)
243 '''