#64 Implement self updater

Co-authored-by: shirtjs <2660574+shirtjs@users.noreply.github.com> (shirt-dev)
Co-authored-by: pukkandan <pukkandan@gmail.com>
pull/72/head
shirt-dev 4 years ago committed by GitHub
parent efabc16165
commit 3dd264bf42
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -27,7 +27,7 @@ os.chdir(root_dir)
exec(compile(open('youtube_dlc/version.py').read(), 'youtube_dlc/version.py', 'exec')) exec(compile(open('youtube_dlc/version.py').read(), 'youtube_dlc/version.py', 'exec'))
VERSION = locals()['__version__'] VERSION = locals()['__version__']
VERSION_LIST = VERSION.replace('-', '.').split('.') VERSION_LIST = VERSION.split('.')
VERSION_LIST = list(map(int, VERSION_LIST)) + [0] * (4 - len(VERSION_LIST)) VERSION_LIST = list(map(int, VERSION_LIST)) + [0] * (4 - len(VERSION_LIST))
print('Version: %s%s' % (VERSION, _x86)) print('Version: %s%s' % (VERSION, _x86))

@ -8,7 +8,7 @@ from datetime import datetime
exec(compile(open('youtube_dlc/version.py').read(), 'youtube_dlc/version.py', 'exec')) exec(compile(open('youtube_dlc/version.py').read(), 'youtube_dlc/version.py', 'exec'))
old_version = locals()['__version__'] old_version = locals()['__version__']
old_version_list = old_version.replace('-', '.').split(".", 4) old_version_list = old_version.split(".", 4)
old_ver = '.'.join(old_version_list[:3]) old_ver = '.'.join(old_version_list[:3])
old_rev = old_version_list[3] if len(old_version_list) > 3 else '' old_rev = old_version_list[3] if len(old_version_list) > 3 else ''

@ -15,6 +15,7 @@ from .utils import encode_compat_str
from .version import __version__ from .version import __version__
''' # Not signed
def rsa_verify(message, signature, key): def rsa_verify(message, signature, key):
from hashlib import sha256 from hashlib import sha256
assert isinstance(message, bytes) assert isinstance(message, bytes)
@ -27,17 +28,13 @@ def rsa_verify(message, signature, key):
return False return False
expected = b'0001' + (byte_size - len(asn1) // 2 - 3) * b'ff' + b'00' + asn1 expected = b'0001' + (byte_size - len(asn1) // 2 - 3) * b'ff' + b'00' + asn1
return expected == signature return expected == signature
'''
def update_self(to_screen, verbose, opener): def update_self(to_screen, verbose, opener):
"""Update the program file with the latest version from the repository""" """Update the program file with the latest version from the repository"""
return to_screen('Update is currently broken.\nVisit https://github.com/pukkandan/yt-dlp/releases/latest to get the latest version') JSON_URL = 'https://api.github.com/repos/pukkandan/yt-dlp/releases/latest'
UPDATE_URL = 'https://blackjack4494.github.io//update/'
VERSION_URL = UPDATE_URL + 'LATEST_VERSION'
JSON_URL = UPDATE_URL + 'versions.json'
UPDATES_RSA_KEY = (0x9d60ee4d8f805312fdb15a62f87b95bd66177b91df176765d13514a0f1754bcd2057295c5b6f1d35daa6742c3ffc9a82d3e118861c207995a8031e151d863c9927e304576bc80692bc8e094896fcf11b66f3e29e04e3a71e9a11558558acea1840aec37fc396fb6b65dc81a1c4144e03bd1c011de62e3f1357b327d08426fe93, 65537)
def sha256sum(): def sha256sum():
h = hashlib.sha256() h = hashlib.sha256()
@ -54,55 +51,36 @@ def update_self(to_screen, verbose, opener):
to_screen('It looks like you installed youtube-dlc with a package manager, pip, setup.py or a tarball. Please use that to update.') to_screen('It looks like you installed youtube-dlc with a package manager, pip, setup.py or a tarball. Please use that to update.')
return return
# compiled file.exe can find itself by
# to_screen(os.path.basename(sys.executable))
# and path to py or exe
# to_screen(os.path.realpath(sys.executable))
# Check if there is a new version
try:
newversion = opener.open(VERSION_URL).read().decode('utf-8').strip()
except Exception:
if verbose:
to_screen(encode_compat_str(traceback.format_exc()))
to_screen('ERROR: can\'t find the current version. Please try again later.')
to_screen('Visit https://github.com/blackjack4494/yt-dlc/releases/latest')
return
if newversion == __version__:
to_screen('youtube-dlc is up-to-date (' + __version__ + ')')
return
# Download and check versions info # Download and check versions info
try: try:
versions_info = opener.open(JSON_URL).read().decode('utf-8') version_info = opener.open(JSON_URL).read().decode('utf-8')
versions_info = json.loads(versions_info) version_info = json.loads(version_info)
except Exception: except Exception:
if verbose: if verbose:
to_screen(encode_compat_str(traceback.format_exc())) to_screen(encode_compat_str(traceback.format_exc()))
to_screen('ERROR: can\'t obtain versions info. Please try again later.') to_screen('ERROR: can\'t obtain versions info. Please try again later.')
to_screen('Visit https://github.com/blackjack4494/yt-dlc/releases/latest') to_screen('Visit https://github.com/pukkandan/yt-dlp/releases/lastest')
return
if 'signature' not in versions_info:
to_screen('ERROR: the versions file is not signed or corrupted. Aborting.')
return
signature = versions_info['signature']
del versions_info['signature']
if not rsa_verify(json.dumps(versions_info, sort_keys=True).encode('utf-8'), signature, UPDATES_RSA_KEY):
to_screen('ERROR: the versions file signature is invalid. Aborting.')
return return
version_id = versions_info['latest'] version_id = version_info['tag_name']
if version_id == __version__:
to_screen('youtube-dlc is up-to-date (' + __version__ + ')')
return
def version_tuple(version_str): def version_tuple(version_str):
return tuple(map(int, version_str.split('.'))) return tuple(map(int, version_str.split('.')))
if version_tuple(__version__) >= version_tuple(version_id): if version_tuple(__version__) >= version_tuple(version_id):
to_screen('youtube-dlc is up to date (%s)' % __version__) to_screen('youtube-dlc is up to date (%s)' % __version__)
return return
to_screen('Updating to version ' + version_id + ' ...') to_screen('Updating to version ' + version_id + ' ...')
version = versions_info['versions'][version_id]
print_notes(to_screen, versions_info['versions']) version = {
'bin': next(i for i in version_info['assets'] if i['name'] == 'youtube-dlc'),
'exe': next(i for i in version_info['assets'] if i['name'] == 'youtube-dlc.exe'),
'exe_x86': next(i for i in version_info['assets'] if i['name'] == 'youtube-dlc_x86.exe'),
}
# sys.executable is set to the full pathname of the exe-file for py2exe # sys.executable is set to the full pathname of the exe-file for py2exe
# though symlinks are not followed so that we need to do this manually # though symlinks are not followed so that we need to do this manually
@ -113,7 +91,7 @@ def update_self(to_screen, verbose, opener):
to_screen('ERROR: no write permissions on %s' % filename) to_screen('ERROR: no write permissions on %s' % filename)
return return
# Py2EXE # PyInstaller
if hasattr(sys, 'frozen'): if hasattr(sys, 'frozen'):
exe = filename exe = filename
directory = os.path.dirname(exe) directory = os.path.dirname(exe)
@ -122,19 +100,14 @@ def update_self(to_screen, verbose, opener):
return return
try: try:
urlh = opener.open(version['exe'][0]) urlh = opener.open(version['exe']['browser_download_url'])
newcontent = urlh.read() newcontent = urlh.read()
urlh.close() urlh.close()
except (IOError, OSError): except (IOError, OSError):
if verbose: if verbose:
to_screen(encode_compat_str(traceback.format_exc())) to_screen(encode_compat_str(traceback.format_exc()))
to_screen('ERROR: unable to download latest version') to_screen('ERROR: unable to download latest version')
to_screen('Visit https://github.com/blackjack4494/yt-dlc/releases/latest') to_screen('Visit https://github.com/pukkandan/yt-dlp/releases/lastest')
return
newcontent_hash = hashlib.sha256(newcontent).hexdigest()
if newcontent_hash != version['exe'][1]:
to_screen('ERROR: the downloaded file hash does not match. Aborting.')
return return
try: try:
@ -147,16 +120,17 @@ def update_self(to_screen, verbose, opener):
return return
try: try:
bat = os.path.join(directory, 'youtube-dlc-updater.bat') bat = os.path.join(directory, 'yt-dlp-updater.cmd')
with io.open(bat, 'w') as batfile: with io.open(bat, 'w') as batfile:
batfile.write(''' batfile.write('''
@echo off @(
echo Waiting for file handle to be closed ... echo.Waiting for file handle to be closed ...
ping 127.0.0.1 -n 5 -w 1000 > NUL ping 127.0.0.1 -n 5 -w 1000 > NUL
move /Y "%s.new" "%s" > NUL move /Y "%s.new" "%s" > NUL
echo Updated youtube-dlc to version %s. echo.Updated youtube-dlc to version %s.
start /b "" cmd /c del "%%~f0"&exit /b" )
\n''' % (exe, exe, version_id)) @start /b "" cmd /c del "%%~f0"&exit /b
''' % (exe, exe, version_id))
subprocess.Popen([bat]) # Continues to run in the background subprocess.Popen([bat]) # Continues to run in the background
return # Do not show premature success messages return # Do not show premature success messages
@ -169,19 +143,14 @@ start /b "" cmd /c del "%%~f0"&exit /b"
# Zip unix package # Zip unix package
elif isinstance(globals().get('__loader__'), zipimporter): elif isinstance(globals().get('__loader__'), zipimporter):
try: try:
urlh = opener.open(version['bin'][0]) urlh = opener.open(version['bin']['browser_download_url'])
newcontent = urlh.read() newcontent = urlh.read()
urlh.close() urlh.close()
except (IOError, OSError): except (IOError, OSError):
if verbose: if verbose:
to_screen(encode_compat_str(traceback.format_exc())) to_screen(encode_compat_str(traceback.format_exc()))
to_screen('ERROR: unable to download latest version') to_screen('ERROR: unable to download latest version')
to_screen('Visit https://github.com/blackjack4494/yt-dlc/releases/latest') to_screen('Visit https://github.com/pukkandan/yt-dlp/releases/lastest')
return
newcontent_hash = hashlib.sha256(newcontent).hexdigest()
if newcontent_hash != version['bin'][1]:
to_screen('ERROR: the downloaded file hash does not match. Aborting.')
return return
try: try:
@ -196,6 +165,7 @@ start /b "" cmd /c del "%%~f0"&exit /b"
to_screen('Updated youtube-dlc. Restart youtube-dlc to use the new version.') to_screen('Updated youtube-dlc. Restart youtube-dlc to use the new version.')
''' # UNUSED
def get_notes(versions, fromVersion): def get_notes(versions, fromVersion):
notes = [] notes = []
for v, vdata in sorted(versions.items()): for v, vdata in sorted(versions.items()):
@ -210,3 +180,4 @@ def print_notes(to_screen, versions, fromVersion=__version__):
to_screen('PLEASE NOTE:') to_screen('PLEASE NOTE:')
for note in notes: for note in notes:
to_screen(note) to_screen(note)
'''

Loading…
Cancel
Save