def update(course=False): """ Update the data of courses and or exercises from server. """ if course: with Spinner.context(msg="Updated course metadata.", waitmsg="Updating course metadata."): for course in api.get_courses(): old = None try: old = Course.get(Course.tid == course["id"]) except peewee.DoesNotExist: old = None if old: old.details_url = course["details_url"] old.save() continue Course.create(tid=course["id"], name=course["name"], details_url=course["details_url"]) else: selected = Course.get_selected() # with Spinner.context(msg="Updated exercise metadata.", # waitmsg="Updating exercise metadata."): print("Updating exercise data.") for exercise in api.get_exercises(selected): old = None try: old = Exercise.byid(exercise["id"]) except peewee.DoesNotExist: old = None if old is not None: old.name = exercise["name"] old.course = selected.id old.is_attempted = exercise["attempted"] old.is_completed = exercise["completed"] old.deadline = exercise.get("deadline") old.is_downloaded = os.path.isdir(old.path()) old.return_url = exercise["return_url"] old.zip_url = exercise["zip_url"] old.submissions_url = exercise["exercise_submissions_url"] old.save() download_exercise(old, update=True) else: ex = Exercise.create(tid=exercise["id"], name=exercise["name"], course=selected.id, is_attempted=exercise["attempted"], is_completed=exercise["completed"], deadline=exercise.get("deadline"), return_url=exercise["return_url"], zip_url=exercise["zip_url"], submissions_url=exercise[("exercise_" "submissions_" "url")]) ex.is_downloaded = os.path.isdir(ex.path()) ex.save()
def download_exercise(exercise, force=False, update_java=False, update=False): course = exercise.get_course() outpath = os.path.join(course.path) realoutpath = exercise.path() needs_update = update and exercise.is_downloaded print("{} -> {}".format(exercise.menuname(), realoutpath)) if not force and os.path.isdir(realoutpath) and not update: print("Already downloaded, skipping.") exercise.is_downloaded = True exercise.save() if update_java: try: modify_java_target(exercise) except TMCError: pass return with Spinner.context(msg="Updated." if needs_update else "Downloaded.", waitmsg="Downloading."): tmpfile = BytesIO() api.get_zip_stream(exercise, tmpfile) zipfp = zipfile.ZipFile(tmpfile) if needs_update: for i in zipfp.infolist(): if "/src/" not in i.filename: zipfp.extract(i, outpath) else: zipfp.extractall(outpath) exercise.is_downloaded = True exercise.save() if update_java: try: modify_java_target(exercise) except WrongExerciseType: pass
def download_exercise(exercise, force=False, update_java=False, update=False): course = exercise.get_course() outpath = os.path.join(course.path) realoutpath = exercise.path() needs_update = update and exercise.is_downloaded print("{} -> {}".format(exercise.menuname(), realoutpath)) if not force and os.path.isdir(realoutpath) and not update: print("Already downloaded, skipping.") exercise.is_downloaded = True exercise.save() if update_java: try: modify_java_target(exercise) except TMCError: pass return with Spinner.context(msg="Updated." if needs_update else "Downloaded.", waitmsg="Downloading."): tmpfile = BytesIO() api.get_zip_stream(exercise.tid, tmpfile) zipfp = zipfile.ZipFile(tmpfile) if needs_update: for i in zipfp.infolist(): if "/src/" not in i.filename: zipfp.extract(i, outpath) else: zipfp.extractall(outpath) exercise.is_downloaded = True exercise.save() if update_java: try: modify_java_target(exercise) except WrongExerciseType: pass
def submit_exercise(exercise, request_review=False, pastebin=False): outpath = exercise.path() infomsg("Submitting from:", outpath) print("{} -> {}".format(exercise.menuname(), "TMC Server")) outpath = os.path.join(outpath, "src") if not os.path.isdir(outpath): raise NotDownloaded() exercise.is_downloaded = True exercise.save() params = {} if request_review: params["request_review"] = "wolololo" if pastebin: params["paste"] = "wolololo" resp = None with Spinner.context(msg="Submission has been sent.", waitmsg="Sending submission."): tmpfile = BytesIO() with zipfile.ZipFile(tmpfile, "w") as zipfp: for root, _, files in os.walk(outpath): for file in files: filename = os.path.join(root, file) archname = os.path.relpath(os.path.join(root, file), os.path.join(outpath, '..')) compress_type = zipfile.ZIP_DEFLATED zipfp.write(filename, archname, compress_type) resp = api.send_zip(exercise, tmpfile.getvalue(), params) if "submission_url" not in resp: return url = resp["submission_url"] @Spinner.decorate(msg="Results:", waitmsg="Waiting for results.") def inner(): while True: data = api.get_submission(url) if data: return data time.sleep(1) data = inner() success = True status = data["status"] if status == "fail": warningmsg("Some tests failed:") warningmsg("------------------") for test in data["test_cases"]: if test["successful"]: if conf.tests_show_successful: successmsg("{name}: {message}".format(**test)) else: errormsg("{name}:\n {message}".format(**test)) helper = "For better details run 'tmc test --id {0}'\n" warningmsg(helper.format(repr(exercise.tid))) success = False elif status == "ok": successmsg("All tests successful!") successmsg("---------------------") points = ", ".join(data["points"]) successmsg("Points [{0}]".format(points)) exercise.is_completed = exercise.is_attempted = True exercise.save() elif status == "error": warningmsg("Something went wrong :(") warningmsg("-----------------------") errormsg(data["error"]) else: raise TMCError("Submission status unknown: {0}".format(status)) if data.get("paste_url"): infomsg("Pastebin URL: " + data["paste_url"]) if data.get("requests_review", False): if data.get("reviewed", False): infomsg("This submission has been reviewed") else: infomsg("Requested a review") infomsg("Submission URL: " + url.split(".json")[0]) if not success: return False
def submit_exercise(exercise, request_review=False, pastebin=False): outpath = exercise.path() infomsg("Submitting from:", outpath) print("{} -> {}".format(exercise.menuname(), "TMC Server")) outpath = os.path.join(outpath, "src") if not os.path.isdir(outpath): raise NotDownloaded() exercise.is_downloaded = True exercise.save() params = {} if request_review: params["request_review"] = "wolololo" if pastebin: params["paste"] = "wolololo" resp = None with Spinner.context(msg="Submission has been sent.", waitmsg="Sending submission."): tmpfile = BytesIO() with zipfile.ZipFile(tmpfile, "w") as zipfp: for root, _, files in os.walk(outpath): for file in files: filename = os.path.join(root, file) archname = os.path.relpath(os.path.join(root, file), os.path.join(outpath, '..')) compress_type = zipfile.ZIP_DEFLATED zipfp.write(filename, archname, compress_type) resp = api.send_zip(exercise.tid, tmpfile.getvalue(), params) if "submission_url" not in resp: return url = resp["submission_url"] submission_id = int(url.split(".json")[0].split("submissions/")[1]) @Spinner.decorate(msg="Results:", waitmsg="Waiting for results.") def inner(): while True: data = api.get_submission(submission_id) if data: return data time.sleep(1) data = inner() success = True status = data["status"] if status == "fail": warningmsg("Some tests failed:") warningmsg("------------------") for test in data["test_cases"]: if test["successful"]: if conf.tests_show_successful: successmsg("{name}: {message}".format(**test)) else: errormsg("{name}:\n {message}".format(**test)) helper = "For better details run 'tmc test --id {0}'\n" warningmsg(helper.format(repr(exercise.tid))) success = False elif status == "ok": successmsg("All tests successful!") successmsg("---------------------") points = ", ".join(data["points"]) successmsg("Points [{0}]".format(points)) exercise.is_completed = exercise.is_attempted = True exercise.save() elif status == "error": warningmsg("Something went wrong :(") warningmsg("-----------------------") errormsg(data["error"]) else: raise TMCError("Submission status unknown: {0}".format(status)) if "paste_url" in data: infomsg("Pastebin URL: " + data["paste_url"]) if data.get("requests_review", False): if data.get("reviewed", False): infomsg("This submission has been reviewed") else: infomsg("Requested a review") infomsg("Submission URL: " + url.split(".json")[0]) if not success: return False