]>
jfr.im git - yt-dlp.git/blob - youtube_dlc/update.py
1 from __future__
import unicode_literals
10 from zipimport
import zipimporter
12 from .compat
import compat_realpath
13 from .utils
import encode_compat_str
15 from .version
import __version__
18 def rsa_verify(message
, signature
, key
):
19 from hashlib
import sha256
20 assert isinstance(message
, bytes)
21 byte_size
= (len(bin(key
[0])) - 2 + 8 - 1) // 8
22 signature
= ('%x' % pow(int(signature
, 16), key
[1], key
[0])).encode()
23 signature
= (byte_size
* 2 - len(signature
)) * b
'0' + signature
24 asn1
= b
'3031300d060960864801650304020105000420'
25 asn1
+= sha256(message
).hexdigest().encode()
26 if byte_size
< len(asn1
) // 2 + 11:
28 expected
= b
'0001' + (byte_size
- len(asn1
) // 2 - 3) * b
'ff' + b
'00' + asn1
29 return expected
== signature
32 def update_self(to_screen
, verbose
, opener
):
33 """Update the program file with the latest version from the repository"""
35 UPDATE_URL
= 'https://blackjack4494.github.io//update/'
36 VERSION_URL
= UPDATE_URL
+ 'LATEST_VERSION'
37 JSON_URL
= UPDATE_URL
+ 'versions.json'
38 UPDATES_RSA_KEY
= (0x9d60ee4d8f805312fdb15a62f87b95bd66177b91df176765d13514a0f1754bcd2057295c5b6f1d35daa6742c3ffc9a82d3e118861c207995a8031e151d863c9927e304576bc80692bc8e094896fcf11b66f3e29e04e3a71e9a11558558acea1840aec37fc396fb6b65dc81a1c4144e03bd1c011de62e3f1357b327d08426fe93, 65537)
42 b
= bytearray(128 * 1024)
44 with open(os
.path
.realpath(sys
.executable
), 'rb', buffering
=0) as f
:
45 for n
in iter(lambda: f
.readinto(mv
), 0):
49 to_screen('Current Build Hash %s' % sha256sum())
51 if not isinstance(globals().get('__loader__'), zipimporter
) and not hasattr(sys
, 'frozen'):
52 to_screen('It looks like you installed youtube-dlc with a package manager, pip, setup.py or a tarball. Please use that to update.')
55 # compiled file.exe can find itself by
56 # to_screen(os.path.basename(sys.executable))
57 # and path to py or exe
58 # to_screen(os.path.realpath(sys.executable))
60 # Check if there is a new version
62 newversion
= opener
.open(VERSION_URL
).read().decode('utf-8').strip()
65 to_screen(encode_compat_str(traceback
.format_exc()))
66 to_screen('ERROR: can\'t find the current version. Please try again later.')
67 to_screen('Visit https://github.com/blackjack4494/yt-dlc/releases/latest')
69 if newversion
== __version__
:
70 to_screen('youtube-dlc is up-to-date (' + __version__
+ ')')
73 # Download and check versions info
75 versions_info
= opener
.open(JSON_URL
).read().decode('utf-8')
76 versions_info
= json
.loads(versions_info
)
79 to_screen(encode_compat_str(traceback
.format_exc()))
80 to_screen('ERROR: can\'t obtain versions info. Please try again later.')
81 to_screen('Visit https://github.com/blackjack4494/yt-dlc/releases/latest')
83 if 'signature' not in versions_info
:
84 to_screen('ERROR: the versions file is not signed or corrupted. Aborting.')
86 signature
= versions_info
['signature']
87 del versions_info
['signature']
88 if not rsa_verify(json
.dumps(versions_info
, sort_keys
=True).encode('utf-8'), signature
, UPDATES_RSA_KEY
):
89 to_screen('ERROR: the versions file signature is invalid. Aborting.')
92 version_id
= versions_info
['latest']
94 def version_tuple(version_str
):
95 return tuple(map(int, version_str
.split('.')))
96 if version_tuple(__version__
) >= version_tuple(version_id
):
97 to_screen('youtube-dlc is up to date (%s)' % __version__
)
100 to_screen('Updating to version ' + version_id
+ ' ...')
101 version
= versions_info
['versions'][version_id
]
103 print_notes(to_screen
, versions_info
['versions'])
105 # sys.executable is set to the full pathname of the exe-file for py2exe
106 # though symlinks are not followed so that we need to do this manually
107 # with help of realpath
108 filename
= compat_realpath(sys
.executable
if hasattr(sys
, 'frozen') else sys
.argv
[0])
110 if not os
.access(filename
, os
.W_OK
):
111 to_screen('ERROR: no write permissions on %s' % filename
)
115 if hasattr(sys
, 'frozen'):
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
)
123 urlh
= opener
.open(version
['exe'][0])
124 newcontent
= urlh
.read()
126 except (IOError, OSError):
128 to_screen(encode_compat_str(traceback
.format_exc()))
129 to_screen('ERROR: unable to download latest version')
130 to_screen('Visit https://github.com/blackjack4494/yt-dlc/releases/latest')
133 newcontent_hash
= hashlib
.sha256(newcontent
).hexdigest()
134 if newcontent_hash
!= version
['exe'][1]:
135 to_screen('ERROR: the downloaded file hash does not match. Aborting.')
139 with open(exe
+ '.new', 'wb') as outf
:
140 outf
.write(newcontent
)
141 except (IOError, OSError):
143 to_screen(encode_compat_str(traceback
.format_exc()))
144 to_screen('ERROR: unable to write the new version')
148 bat
= os
.path
.join(directory
, 'youtube-dlc-updater.bat')
149 with io
.open(bat
, 'w') as batfile
:
152 echo Waiting for file handle to be closed ...
153 ping 127.0.0.1 -n 5 -w 1000 > NUL
154 move /Y "%s.new" "%s" > NUL
155 echo Updated youtube-dlc to version %s.
156 start /b "" cmd /c del "%%~f0"&exit /b"
157 \n''' % (exe
, exe
, version_id
))
159 subprocess
.Popen([bat
]) # Continues to run in the background
160 return # Do not show premature success messages
161 except (IOError, OSError):
163 to_screen(encode_compat_str(traceback
.format_exc()))
164 to_screen('ERROR: unable to overwrite current version')
168 elif isinstance(globals().get('__loader__'), zipimporter
):
170 urlh
= opener
.open(version
['bin'][0])
171 newcontent
= urlh
.read()
173 except (IOError, OSError):
175 to_screen(encode_compat_str(traceback
.format_exc()))
176 to_screen('ERROR: unable to download latest version')
177 to_screen('Visit https://github.com/blackjack4494/yt-dlc/releases/latest')
180 newcontent_hash
= hashlib
.sha256(newcontent
).hexdigest()
181 if newcontent_hash
!= version
['bin'][1]:
182 to_screen('ERROR: the downloaded file hash does not match. Aborting.')
186 with open(filename
, 'wb') as outf
:
187 outf
.write(newcontent
)
188 except (IOError, OSError):
190 to_screen(encode_compat_str(traceback
.format_exc()))
191 to_screen('ERROR: unable to overwrite current version')
194 to_screen('Updated youtube-dlc. Restart youtube-dlc to use the new version.')
197 def get_notes(versions
, fromVersion
):
199 for v
, vdata
in sorted(versions
.items()):
201 notes
.extend(vdata
.get('notes', []))
205 def print_notes(to_screen
, versions
, fromVersion
=__version__
):
206 notes
= get_notes(versions
, fromVersion
)
208 to_screen('PLEASE NOTE:')