Show update popup when needed and install when it's asked to

This commit is contained in:
CMDR-Kiel42 committed 2020-07-15 00:55:48 +02:00
1 parent ffb3b91706
commit 77327d4ba1
3 files changed
+69 -77

No files matched your search

-44
View File
@@ -19,10 +19,6 @@ import tkinter.messagebox as confirmDialog
class SpanshRouter():
def __init__(self, plugin_dir):
version_file = os.path.join(plugin_dir, "version.json")
with open(version_file, 'r') as version_fd:
self.plugin_version = version_fd.read()
self.update_available = False
self.next_stop = "No route planned"
self.route = []
@@ -301,11 +297,6 @@ class SpanshRouter():
self.copy_waypoint()
self.save_offset()
def goto_changelog_page(self):
changelog_url = 'https://github.com/CMDR-Kiel42/EDMC_SpanshRouter/blob/master/CHANGELOG.md#'
changelog_url += self.spansh_updater.version.replace('.', '')
webbrowser.open(changelog_url)
def plot_file(self):
ftypes = [
('All supported files', '*.csv *.txt'),
@@ -530,38 +521,3 @@ class SpanshRouter():
except ValueError:
self.show_error("Invalid range")
self.range_entry.set_error_style()
def cleanup_old_version(self):
try:
if (os.path.exists(os.path.join(self.plugin_dir, "AutoCompleter.py"))
and os.path.exists(os.path.join(self.plugin_dir, "SpanshRouter"))):
files_list = os.listdir(self.plugin_dir)
for filename in files_list:
if (filename != "load.py"
and (filename.endswith(".py") or filename.endswith(".pyc") or filename.endswith(".pyo"))):
os.remove(os.path.join(self.plugin_dir, filename))
except:
exc_type, exc_value, exc_traceback = sys.exc_info()
lines = traceback.format_exception(exc_type, exc_value, exc_traceback)
sys.stderr.write(''.join('!! ' + line for line in lines))
def check_for_update(self):
self.cleanup_old_version()
version_url = "https://raw.githubusercontent.com/CMDR-Kiel42/EDMC_SpanshRouter/master/version.json"
try:
response = requests.get(version_url, timeout=2)
if response.status_code == 200:
if self.plugin_version != response.text:
self.update_available = True
self.spansh_updater = SpanshUpdater(response.text, self.plugin_dir)
else:
sys.stderr.write("Could not query latest SpanshRouter version: " + str(response.status_code) + response.text)
except:
exc_type, exc_value, exc_traceback = sys.exc_info()
lines = traceback.format_exception(exc_type, exc_value, exc_traceback)
sys.stderr.write(''.join('!! ' + line for line in lines))
def install_update(self):
self.spansh_updater.install()
+58 -17
View File
@@ -7,15 +7,23 @@ import json
import tkinter as tk
import tkinter.font as tkfont
import tkinter.scrolledtext as ScrolledText
import tkinter.messagebox as confirmDialog
class SpanshUpdater():
def __init__(self, version, plugin_dir):
self.version = version
self.zip_name = "EDMC_SpanshRouter_" + version.replace('.', '') + ".zip"
def __init__(self, plugin_dir):
version_file = os.path.join(plugin_dir, "version.json")
with open(version_file, 'r') as version_fd:
self.plugin_version = version_fd.read()
self.zip_name = "EDMC_SpanshRouter_" + self.plugin_version.replace('.', '') + ".zip"
self.plugin_dir = plugin_dir
self.zip_path = os.path.join(self.plugin_dir, self.zip_name)
self.zip_downloaded = False
self.changelogs = self.get_changelog()
self.update_popup = UpdatePopup(self)
self.update_popup.withdraw()
self.update_available = False
def download_zip(self):
url = 'https://github.com/CMDR-Kiel42/EDMC_SpanshRouter/releases/download/v' + self.version + '/' + self.zip_name
@@ -52,6 +60,21 @@ class SpanshUpdater():
else:
sys.stderr.write("Error when downloading the latest SpanshRouter update")
def cleanup_old_version(self):
try:
if (os.path.exists(os.path.join(self.plugin_dir, "AutoCompleter.py"))
and os.path.exists(os.path.join(self.plugin_dir, "SpanshRouter"))):
files_list = os.listdir(self.plugin_dir)
for filename in files_list:
if (filename != "load.py"
and (filename.endswith(".py") or filename.endswith(".pyc") or filename.endswith(".pyo"))):
os.remove(os.path.join(self.plugin_dir, filename))
except:
exc_type, exc_value, exc_traceback = sys.exc_info()
lines = traceback.format_exception(exc_type, exc_value, exc_traceback)
sys.stderr.write(''.join('!! ' + line for line in lines))
def get_changelog(self):
url = "https://api.github.com/repos/CMDR-Kiel42/EDMC_SpanshRouter/releases/latest"
try:
@@ -68,9 +91,29 @@ class SpanshUpdater():
lines = traceback.format_exception(exc_type, exc_value, exc_traceback)
sys.stderr.write(''.join('!! ' + line for line in lines))
def ask_for_update(self):
self.update_popup.deiconify()
def check_for_update(self):
self.cleanup_old_version()
version_url = "https://raw.githubusercontent.com/CMDR-Kiel42/EDMC_SpanshRouter/master/version.json"
try:
response = requests.get(version_url, timeout=2)
if response.status_code == 200:
if self.plugin_version != response.text:
self.update_available = True
else:
sys.stderr.write("Could not query latest SpanshRouter version: " + str(response.status_code) + response.text)
except:
exc_type, exc_value, exc_traceback = sys.exc_info()
lines = traceback.format_exception(exc_type, exc_value, exc_traceback)
sys.stderr.write(''.join('!! ' + line for line in lines))
class UpdatePopup(tk.Toplevel):
def __init__(self, changelogs):
def __init__(self, sp_updater):
self.sp_updater = sp_updater
super().__init__()
width, height = 600, 280
x = self.winfo_screenwidth()/2 - width/2
@@ -80,7 +123,7 @@ class UpdatePopup(tk.Toplevel):
self.text_field = ScrolledText.ScrolledText(self, wrap="word", height=10)
self.text_field.tag_configure("title", font=tkfont.Font(weight="bold", size=14), justify=tk.CENTER)
for line in changelogs.splitlines():
for line in sp_updater.changelogs.splitlines():
if line.startswith("## "):
line = line.replace("## ", "SpanshRouter - Release ")
self.text_field.insert(tk.END, line + "\n", "title")
@@ -88,22 +131,20 @@ class UpdatePopup(tk.Toplevel):
self.text_field.insert(tk.END, line + "\n")
self.skip_install_checkbox = tk.Checkbutton(self, text="Do not warn me about this version anymore")
self.install_button = tk.Button(self, text="Install")
self.cancel_button = tk.Button(self, text="Cancel")
self.install_button = tk.Button(self, text="Install", command=self.click_install)
self.cancel_button = tk.Button(self, text="Cancel", command=self.click_cancel)
self.text_field.config(state=tk.DISABLED)
self.text_field.grid(row=0, columnspan=2, sticky=tk.W+tk.E+tk.N+tk.S, padx=10, pady=15)
self.skip_install_checkbox.grid(row=1, columnspan=2, sticky=tk.W+tk.E+tk.N+tk.S)
self.install_button.grid(row=2, column=0, pady=15)
self.cancel_button.grid(row=2, column=1)
if __name__ == "__main__":
print("Testing")
sp_updater = SpanshUpdater("3.0.3", ".")
root = tk.Tk()
root.geometry("200x300")
root.title("main")
popup = UpdatePopup(sp_updater.changelogs)
popup.mainloop()
def click_install(self):
self.sp_updater.update_available = True
self.destroy()
confirmDialog.showinfo("SpanshRouter", "The update will be installed as soon as you quit EDMC.")
def click_cancel(self):
self.sp_updater.update_available = False
self.destroy()
+11 -16
View File
@@ -1,24 +1,27 @@
import sys
import SpanshRouter.updater as updater
from SpanshRouter.SpanshRouter import SpanshRouter
import tkinter.messagebox as confirmDialog
spansh_router = None
sp_updater = None
def plugin_start3(plugin_dir):
return plugin_start(plugin_dir)
def plugin_start(plugin_dir):
global spansh_router
global sp_updater
spansh_router = SpanshRouter(plugin_dir)
spansh_router.check_for_update()
sp_updater = updater.SpanshUpdater(plugin_dir)
return 'spansh_router'
def plugin_stop():
global spansh_router
global sp_updater
spansh_router.save_route()
if spansh_router.update_available:
spansh_router.install_update()
if sp_updater.update_available:
sp_updater.install()
def journal_entry(cmdr, is_beta, system, station, entry, state):
global spansh_router
@@ -29,18 +32,10 @@ def journal_entry(cmdr, is_beta, system, station, entry, state):
spansh_router.update_route()
def ask_for_update():
global spansh_router
if spansh_router.update_available:
update_txt = "New Spansh Router update available!\n"
update_txt += "If you choose to install it, you will have to restart EDMC for it to take effect.\n\n"
update_txt += spansh_router.spansh_updater.changelogs
update_txt += "\n\nInstall?"
install_update = confirmDialog.askyesno("SpanshRouter", update_txt)
if install_update:
confirmDialog.showinfo("SpanshRouter", "The update will be installed as soon as you quit EDMC.")
else:
spansh_router.update_available = False
global sp_updater
sp_updater.check_for_update()
if sp_updater.update_available:
sp_updater.ask_for_update()
def plugin_app(parent):
global spansh_router