Skip to content
This repository has been archived by the owner on Aug 6, 2021. It is now read-only.

Commit

Permalink
Updater: Add a utility to auto-update
Browse files Browse the repository at this point in the history
In the process of updating zygrader to a package, it will be nice to automatically
update if new versions exists. This provides the ability to do so.
  • Loading branch information
natecraddock committed Jun 23, 2020
1 parent 17346bb commit 4028a52
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 1 deletion.
9 changes: 9 additions & 0 deletions __main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from zygrader import config
from zygrader.data import lock
from zygrader import logger
from zygrader import updater

def lock_cleanup():
lock.unlock_all_labs_by_grader(getpass.getuser())
Expand Down Expand Up @@ -47,6 +48,14 @@ def sigtstp_handler(signum, frame):
# Set a short ESC key delay (curses environment variable)
os.environ.setdefault('ESCDELAY', '25')

# Check for updates
latest_version = updater.get_latest_version()
if latest_version != config.g_data.VERSION:
updater.update_zygrader(latest_version)

# update_zygrader will update then fork a new zygrader process
sys.exit()

zygrader.start()

logger.log("zygrader exited normally")
2 changes: 1 addition & 1 deletion zygrader/config/g_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from .. import data

# Only change these if you know what you are doing!
# Zygrader version
VERSION = 3.5

# TODO: save this in configuration
Expand Down
39 changes: 39 additions & 0 deletions zygrader/updater.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
"""Updater: To auto-update zygrader during runtime"""
import requests
import subprocess
import sys

from . import config

REPO_NAME = "natecraddock/zygrader"
API_URL = f"https://api.github.com/repos/{REPO_NAME}/tags"
INSTALL_URL = f"https://github.com/{REPO_NAME}/tarball/master"

def get_latest_version():
"""Check the zygrader git repo tags for a new version"""
r = requests.get(API_URL)
if not r.ok:
return config.g_data.VERSION

latest = float(r.json()[0]["name"])
if latest > config.g_data.VERSION:
return latest

return config.g_data.VERSION

def update_zygrader(latest_version):
"""Download zygrader from the git repository and update it"""
print()
print(f"Updating zygrader [{config.g_data.VERSION} -> {latest_version}]")
print()

# Install the new version of zygrader
try:
subprocess.check_call([sys.executable, "-m", "pip", "install", "--upgrade", INSTALL_URL])
except subprocess.CalledProcessError:
print("Failed to update zygrader. Exiting")
print()
return

# zygrader updated properly, fork and run zygrader again
# TODO: Update here once zygrader is a package

0 comments on commit 4028a52

Please sign in to comment.