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