]> jfr.im git - yt-dlp.git/blob - yt_dlp/update.py
Revert "[build] Build Windows x86 version with py3.8"
[yt-dlp.git] / yt_dlp / update.py
1 from __future__ import unicode_literals
2
3 import hashlib
4 import json
5 import os
6 import platform
7 import subprocess
8 import sys
9 import traceback
10 from zipimport import zipimporter
11
12 from .compat import compat_realpath
13 from .utils import encode_compat_str
14
15 from .version import __version__
16
17
18 ''' # Not signed
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:
28 return False
29 expected = b'0001' + (byte_size - len(asn1) // 2 - 3) * b'ff' + b'00' + asn1
30 return expected == signature
31 '''
32
33
34 def update_self(to_screen, verbose, opener):
35 ''' Exists for backward compatibility. Use run_update(ydl) instead '''
36
37 printfn = to_screen
38
39 class FakeYDL():
40 _opener = opener
41 to_screen = printfn
42
43 @staticmethod
44 def report_warning(msg, *args, **kwargs):
45 return printfn('WARNING: %s' % msg, *args, **kwargs)
46
47 @staticmethod
48 def report_error(msg, tb=None):
49 printfn('ERROR: %s' % msg)
50 if not verbose:
51 return
52 if tb is None:
53 # Copied from YoutubeDl.trouble
54 if sys.exc_info()[0]:
55 tb = ''
56 if hasattr(sys.exc_info()[1], 'exc_info') and sys.exc_info()[1].exc_info[0]:
57 tb += ''.join(traceback.format_exception(*sys.exc_info()[1].exc_info))
58 tb += encode_compat_str(traceback.format_exc())
59 else:
60 tb_data = traceback.format_list(traceback.extract_stack())
61 tb = ''.join(tb_data)
62 if tb:
63 printfn(tb)
64
65 return run_update(FakeYDL())
66
67
68 def run_update(ydl):
69 """
70 Update the program file with the latest version from the repository
71 Returns whether the program should terminate
72 """
73
74 JSON_URL = 'https://api.github.com/repos/yt-dlp/yt-dlp/releases/latest'
75
76 def report_error(msg, network=False, expected=False, delim=';'):
77 if network:
78 msg += '%s Visit https://github.com/yt-dlp/yt-dlp/releases/latest' % delim
79 ydl.report_error(msg, tb='' if network or expected else None)
80
81 def calc_sha256sum(path):
82 h = hashlib.sha256()
83 b = bytearray(128 * 1024)
84 mv = memoryview(b)
85 with open(os.path.realpath(path), 'rb', buffering=0) as f:
86 for n in iter(lambda: f.readinto(mv), 0):
87 h.update(mv[:n])
88 return h.hexdigest()
89
90 err = None
91 if isinstance(globals().get('__loader__'), zipimporter):
92 # We only support python 3.6 or above
93 if sys.version_info < (3, 6):
94 err = 'This is the last release of yt-dlp for Python version %d.%d! Please update to Python 3.6 or above' % sys.version_info[:2]
95 elif hasattr(sys, 'frozen'):
96 # Python 3.6 supports only vista and above
97 if sys.getwindowsversion()[0] < 6:
98 err = 'This is the last release of yt-dlp for your version of Windows. Please update to Windows Vista or above'
99 else:
100 err = 'It looks like you installed yt-dlp with a package manager, pip, setup.py or a tarball. Please use that to update'
101 if err:
102 return report_error(err, expected=True)
103
104 # sys.executable is set to the full pathname of the exe-file for py2exe
105 # though symlinks are not followed so that we need to do this manually
106 # with help of realpath
107 filename = compat_realpath(sys.executable if hasattr(sys, 'frozen') else sys.argv[0])
108 ydl.to_screen('Current Build Hash %s' % calc_sha256sum(filename))
109
110 # Download and check versions info
111 try:
112 version_info = ydl._opener.open(JSON_URL).read().decode('utf-8')
113 version_info = json.loads(version_info)
114 except Exception:
115 return report_error('can\'t obtain versions info. Please try again later ', True, delim='or')
116
117 def version_tuple(version_str):
118 return tuple(map(int, version_str.split('.')))
119
120 version_id = version_info['tag_name']
121 if version_tuple(__version__) >= version_tuple(version_id):
122 ydl.to_screen('yt-dlp is up to date (%s)' % __version__)
123 return
124
125 ydl.to_screen('Updating to version ' + version_id + ' ...')
126
127 version_labels = {
128 'zip_3': '',
129 'exe_64': '.exe',
130 'exe_32': '_x86.exe',
131 }
132
133 def get_bin_info(bin_or_exe, version):
134 label = version_labels['%s_%s' % (bin_or_exe, version)]
135 return next((i for i in version_info['assets'] if i['name'] == 'yt-dlp%s' % label), {})
136
137 def get_sha256sum(bin_or_exe, version):
138 filename = 'yt-dlp%s' % version_labels['%s_%s' % (bin_or_exe, version)]
139 urlh = next(
140 (i for i in version_info['assets'] if i['name'] in ('SHA2-256SUMS')),
141 {}).get('browser_download_url')
142 if not urlh:
143 return None
144 hash_data = ydl._opener.open(urlh).read().decode('utf-8')
145 if hash_data.startswith('version:'):
146 # Old colon-separated hash file
147 return dict(ln.split(':') for ln in hash_data.splitlines()).get(filename)
148 else:
149 # GNU-style hash file
150 return dict(ln.split()[::-1] for ln in hash_data.splitlines()).get(filename)
151
152 if not os.access(filename, os.W_OK):
153 return report_error('no write permissions on %s' % filename, expected=True)
154
155 # PyInstaller
156 if hasattr(sys, 'frozen'):
157 exe = filename
158 directory = os.path.dirname(exe)
159 if not os.access(directory, os.W_OK):
160 return report_error('no write permissions on %s' % directory, expected=True)
161 try:
162 if os.path.exists(filename + '.old'):
163 os.remove(filename + '.old')
164 except (IOError, OSError):
165 return report_error('unable to remove the old version')
166
167 try:
168 arch = platform.architecture()[0][:2]
169 url = get_bin_info('exe', arch).get('browser_download_url')
170 if not url:
171 return report_error('unable to fetch updates', True)
172 urlh = ydl._opener.open(url)
173 newcontent = urlh.read()
174 urlh.close()
175 except (IOError, OSError, StopIteration):
176 return report_error('unable to download latest version', True)
177
178 try:
179 with open(exe + '.new', 'wb') as outf:
180 outf.write(newcontent)
181 except (IOError, OSError):
182 return report_error('unable to write the new version')
183
184 expected_sum = get_sha256sum('exe', arch)
185 if not expected_sum:
186 ydl.report_warning('no hash information found for the release')
187 elif calc_sha256sum(exe + '.new') != expected_sum:
188 report_error('unable to verify the new executable', True)
189 try:
190 os.remove(exe + '.new')
191 except OSError:
192 return report_error('unable to remove corrupt download')
193
194 try:
195 os.rename(exe, exe + '.old')
196 except (IOError, OSError):
197 return report_error('unable to move current version')
198 try:
199 os.rename(exe + '.new', exe)
200 except (IOError, OSError):
201 report_error('unable to overwrite current version')
202 os.rename(exe + '.old', exe)
203 return
204 try:
205 # Continues to run in the background
206 subprocess.Popen(
207 'ping 127.0.0.1 -n 5 -w 1000 & del /F "%s.old"' % exe,
208 shell=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
209 ydl.to_screen('Updated yt-dlp to version %s' % version_id)
210 return True # Exit app
211 except OSError:
212 report_error('unable to delete old version')
213
214 # Zip unix package
215 elif isinstance(globals().get('__loader__'), zipimporter):
216 try:
217 url = get_bin_info('zip', '3').get('browser_download_url')
218 if not url:
219 return report_error('unable to fetch updates', True)
220 urlh = ydl._opener.open(url)
221 newcontent = urlh.read()
222 urlh.close()
223 except (IOError, OSError, StopIteration):
224 return report_error('unable to download latest version', True)
225
226 expected_sum = get_sha256sum('zip', '3')
227 if not expected_sum:
228 ydl.report_warning('no hash information found for the release')
229 elif hashlib.sha256(newcontent).hexdigest() != expected_sum:
230 return report_error('unable to verify the new zip', True)
231
232 try:
233 with open(filename, 'wb') as outf:
234 outf.write(newcontent)
235 except (IOError, OSError):
236 return report_error('unable to overwrite current version')
237
238 ydl.to_screen('Updated yt-dlp to version %s; Restart yt-dlp to use the new version' % version_id)
239
240
241 ''' # UNUSED
242 def get_notes(versions, fromVersion):
243 notes = []
244 for v, vdata in sorted(versions.items()):
245 if v > fromVersion:
246 notes.extend(vdata.get('notes', []))
247 return notes
248
249
250 def print_notes(to_screen, versions, fromVersion=__version__):
251 notes = get_notes(versions, fromVersion)
252 if notes:
253 to_screen('PLEASE NOTE:')
254 for note in notes:
255 to_screen(note)
256 '''