def _check_for_updates(): # grab the server version web_api = WebAPI() version = web_api.get_version() print('done') create_tuple = lambda v: tuple(map(int, v.split('.'))) server_version = create_tuple(version) local_version = create_tuple(VERSION) if server_version > local_version or force_update: print('Updating MyPyTutor...', end='', flush=True) # grab our new zip file mpt_zip_path = web_api.get_mpt_zipfile() # extract over the script path # do NOT delete things; the user could have other stuff here script_dir = get_script_dir() safely_extract_zipfile(mpt_zip_path, script_dir) print('done') # re-exec with the new version # remove update arg, or we get an infinite loop argv = sys.argv if '--force-update-mpt' in argv: argv.remove('--force-update-mpt') execl(sys.executable, sys.executable, *argv)
def update_default_tutorial_package(force_update=False): """ Update the default tutorial package if necessary. Args: force_update (bool, optional): If True, update regardless of whether a newer version of the tutorial package is available on the server. """ from tutorlib.config.configuration import load_config from tutorlib.gui.app.support \ import remove_directory_contents, safely_extract_zipfile from tutorlib.interface.problems \ import TutorialPackage, TutorialPackageError from tutorlib.interface.web_api import WebAPI, WebAPIError print('Checking for tutorial package updates...', end='', flush=True) # grab our config file cfg = load_config() package_name = cfg.tutorials.default package_options = getattr(cfg, package_name) # try to open the tutorial package try: tutorial_package = TutorialPackage(package_name, package_options) except TutorialPackageError: print('failed') return # check if we need to do an update at all web_api = WebAPI() try: timestamp = web_api.get_tutorials_timestamp() except WebAPIError: print('failed') return # we need to be comparing as ints create_tuple = lambda t: tuple(map(int, t.split('.'))) server_timestamp = create_tuple(timestamp) local_timestamp = create_tuple(tutorial_package.timestamp) print('done') # we only want to update if the server's version is more recent # a more recent local version should only arise in development, anyway if server_timestamp <= local_timestamp and not force_update: return print('Updating tutorial package...', end='', flush=True) # grab the zipfile try: zip_path = web_api.get_tutorials_zipfile() except WebAPIError: print('failed') return # extract the zipfile into our empty tutorial directory remove_directory_contents(tutorial_package.options.tut_dir) safely_extract_zipfile(zip_path, tutorial_package.options.tut_dir) print('done')
def bootstrap_tutorials(): """ If the default tutorial path does not exist, download and extract the tutorials zipfile. If no default tutorial package is specified, CSSE1001Tutorials will be assumed (this is the default package name used by this script). """ from tutorlib.config.configuration import load_config, save_config from tutorlib.gui.app.support import safely_extract_zipfile from tutorlib.interface.problems \ import TutorialPackage, TutorialPackageError from tutorlib.interface.web_api import WebAPI, WebAPIError # grab our config file cfg = load_config() options = getattr(cfg, cfg.tutorials.default or 'CSSE1001Tutorials') def tutorials_are_installed(): if not options.tut_dir: # no entry in config at all (default) return False if not os.path.exists(options.tut_dir): # no package directory at all return False if not os.path.exists(options.ans_dir): # no answers dir return False try: _ = TutorialPackage(cfg.tutorials.default, options) except TutorialPackageError: return False return True if not tutorials_are_installed(): print('Downloading default tutorial package...', end='', flush=True) web_api = WebAPI() try: filename = web_api.get_tutorials_zipfile() except WebAPIError: print('failed') sys.exit(1) print('done') # set the default tutorial directory # our default tutorial directory is in the same directory as the script # note that this assumes we used the default config, which created the # CSSE1001Tutorials key print('Installing default tutorial package...', end='', flush=True) script_dir = get_script_dir() options.tut_dir = os.path.join(script_dir, 'CSSE1001Tutorials') options.ans_dir = os.path.join(script_dir, 'CSSE1001Answers') safely_extract_zipfile(filename, options.tut_dir) if not os.path.exists(options.ans_dir): os.mkdir(options.ans_dir) save_config(cfg) print('done')