Skip to content

Commit

Permalink
Added autoupdater
Browse files Browse the repository at this point in the history
  • Loading branch information
AcidWeb committed Mar 11, 2019
1 parent bf7e808 commit bde96e9
Showing 1 changed file with 28 additions and 6 deletions.
34 changes: 28 additions & 6 deletions CurseBreaker.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@
import sys
import time
import msvcrt
import shutil
import requests
import traceback
from tqdm import tqdm
from colorama import init, Fore
from terminaltables import SingleTable
from prompt_toolkit import PromptSession, HTML, print_formatted_text as printft
from prompt_toolkit.completion import WordCompleter
from distutils.version import StrictVersion
from CB import __version__
from CB.Core import Core

Expand Down Expand Up @@ -40,6 +43,7 @@ def start(self):
'Try starting it with administrative privileges.</ansibrightred>\n'))
os.system('pause')
sys.exit(1)
self.auto_update()
self.core.init_config()
self.setup_completer()
# Curse URI Support
Expand Down Expand Up @@ -80,8 +84,7 @@ def start(self):
# Prompt session
while True:
try:
command = self.session.prompt(HTML('<ansibrightgreen>CB></ansibrightgreen> '),
completer=self.completer, bottom_toolbar=self.version_check())
command = self.session.prompt(HTML('<ansibrightgreen>CB></ansibrightgreen> '), completer=self.completer)
except KeyboardInterrupt:
continue
except EOFError:
Expand All @@ -97,6 +100,29 @@ def start(self):
else:
printft('Command not found.')

def auto_update(self):
if getattr(sys, 'frozen', False):
try:
payload = requests.get('https://api.github.com/repos/AcidWeb/CurseBreaker/releases/latest').json()
remoteversion = payload['name']
url = payload['assets'][0]['browser_download_url']
if StrictVersion(remoteversion[1:]) > StrictVersion(__version__):
printft(HTML('<ansigreen>Updating CurseBreaker...</ansigreen>'))
if os.path.isfile(sys.executable + '.old'):
os.remove(sys.executable + '.old')
shutil.move(sys.executable, sys.executable + '.old')
payload = requests.get(url)
with open(sys.executable, 'wb') as f:
f.write(payload.content)
printft(HTML('\n<ansibrightgreen>Update complete! Please restart the application.'
'</ansibrightgreen>\n'))
os.system('pause')
sys.exit(0)
except Exception as e:
printft(HTML(f'<ansibrightred>Update failed!\n\nReason: {str(e)}</ansibrightred>\n'))
os.system('pause')
sys.exit(1)

def handle_exception(self, e):
if getattr(sys, 'frozen', False):
printft(HTML(f'\n<ansibrightred>{str(e)}</ansibrightred>'))
Expand All @@ -109,10 +135,6 @@ def print_header(self):
printft(HTML(f'<ansibrightblack>~~~ <ansibrightgreen>CurseBreaker</ansibrightgreen> <ansibrightred>v'
f'{__version__}</ansibrightred> ~~~</ansibrightblack>\n'))

def version_check(self):
# TODO Version check
return HTML(f'Installed version: <style bg="ansired">v{__version__}</style>')

def setup_completer(self):
commands = ['install', 'uninstall', 'update', 'status', 'orphans', 'toggle_backup', 'uri_integration']
addons = sorted(self.core.config['Addons'], key=lambda k: k['Name'].lower())
Expand Down

0 comments on commit bde96e9

Please sign in to comment.