Beispiel #1
0
 def __init__(self):
     self.title = 'Problem Details'
     data = problem.retrieve_problem_data()
     self.settings = settings.load_settings()
     prob = problem.get_problem(data, self.settings['Chapters'])
     self.options = [
         'Problem: {}'.format(prob['Problem']),
         'Page: {}'.format(prob['Page']),
     ]
     self.prompt = '\nPress Enter to return to main menu...'
Beispiel #2
0
def get_submissions():
	submissions_return = []
	tid = session.get("tid")
	submissions = ProgrammingSubmissions.query.filter_by(tid=tid).order_by(ProgrammingSubmissions.psid.desc()).all()
	if submissions is not None:
		for submission in submissions:
			_problem = problem.get_problem(pid=submission.pid).first()
			submissions_return.append({
				"psid": submission.psid,
				"title": _problem.title if _problem else "",
				"message": submission.message,
				"log": submission.log,
				"date": utils.isoformat(submission.date),
				"number": submission.number,
				"duration": submission.duration
			})
	return { "success": 1, "submissions": submissions_return }
Beispiel #3
0
def judge(submission_path, language, pid):

    if not os.path.exists(submission_path):
        raise WebException("Program is missing.")

    _problem = problem.get_problem(pid=pid).first()
    if _problem is None:
        raise WebException("Problem does not exist.")

    submission_root = os.path.dirname(submission_path)
    os.chdir(submission_root)
    log = ""
    message = ""

    log += "Compiling...\n"
    start_time = time.time()
    try:
        if language == "python2":
            subprocess.check_output("python -m py_compile %s" %
                                    submission_path,
                                    shell=True)
        elif language == "python3":
            subprocess.check_output("python3 -m py_compile %s" %
                                    submission_path,
                                    shell=True)
        elif language == "java":
            subprocess.check_output("javac %s" % submission_path, shell=True)
        else:
            message = "Not implemented."
            return message, log, time.time() - start_time
    except subprocess.CalledProcessError as e:
        # TODO: Extract useful error messages from exceptions and add timeout
        #log += "There was a problem with compiling.\n%s\n" % str(e)
        message = "There was a problem with compiling."

        return message, log, time.time() - start_time

    log += "Compiled.\n"

    try:
        judge = imp.load_source("judge", _problem.grader)
    except Exception, e:
        message = "An error occured. Please notify an admin immediately."
        log += "Could not load judge.\n"
        return message, log, time.time() - start_time
Beispiel #4
0
def get_submissions():
    submissions_return = []
    tid = session.get("tid")
    submissions = ProgrammingSubmissions.query.filter_by(tid=tid).order_by(
        ProgrammingSubmissions.psid.desc()).all()
    if submissions is not None:
        for submission in submissions:
            _problem = problem.get_problem(pid=submission.pid).first()
            submissions_return.append({
                "psid": submission.psid,
                "title": _problem.title if _problem else "",
                "message": submission.message,
                "log": submission.log,
                "date": utils.isoformat(submission.date),
                "number": submission.number,
                "duration": submission.duration
            })
    return {"success": 1, "submissions": submissions_return}
Beispiel #5
0
def judge(submission_path, language, pid):

	if not os.path.exists(submission_path):
		raise WebException("Program is missing.")

	_problem = problem.get_problem(pid=pid).first()
	if _problem is None:
		raise WebException("Problem does not exist.")

	submission_root = os.path.dirname(submission_path)
	os.chdir(submission_root)
	log = ""
	message = ""

	log += "Compiling...\n"
	start_time = time.time()
	try:
		if language == "python2":
			subprocess.check_output("python -m py_compile %s" % submission_path, shell=True)
		elif language == "python3":
			subprocess.check_output("python3 -m py_compile %s" % submission_path, shell=True)
		elif language == "java":
			subprocess.check_output("javac %s" % submission_path, shell=True)
		else:
			message = "Not implemented."
			return message, log, time.time() - start_time
	except subprocess.CalledProcessError as e:
		# TODO: Extract useful error messages from exceptions and add timeout
		#log += "There was a problem with compiling.\n%s\n" % str(e)
		message = "There was a problem with compiling."

		return message, log, time.time() - start_time

	log += "Compiled.\n"

	try:
		judge = imp.load_source("judge", _problem.grader)
	except Exception, e:
		message = "An error occured. Please notify an admin immediately."
		log += "Could not load judge.\n"
		return message, log, time.time() - start_time
Beispiel #6
0
def submit_program():
	params = utils.flat_multi(request.form)

	pid = params.get("pid")
	tid = session.get("tid")
	_user = user.get_user().first()

	language = params.get("language")
	submission_contents = params.get("submission")

	_problem = problem.get_problem(pid=pid).first()
	if _problem is None:
		raise WebException("Problem does not exist.")

	if _problem.category != "Programming":
		raise WebException("Can't judge this problem.")

	if language not in extensions:
		raise WebException("Language not supported.")

	solved = Solves.query.filter_by(pid=pid, tid=tid, correct=1).first()
	if solved:
		raise WebException("You already solved this problem.")

	judge_folder = os.path.join(app.config["GRADER_FOLDER"], pid)
	if not os.path.exists(judge_folder):
		os.makedirs(judge_folder)

	submission_folder = os.path.join(judge_folder, utils.generate_string())
	while os.path.exists(submission_folder):
		submission_folder = os.path.join(judge_folder, utils.generate_string())

	os.makedirs(submission_folder)

	submission_path = os.path.join(submission_folder, "program.%s" % extensions[language])

	open(submission_path, "w").write(submission_contents)
	message, log, duration = judge(submission_path, language, pid)

	number = ProgrammingSubmissions.query.filter_by(tid=tid).with_entities(ProgrammingSubmissions.number).order_by(ProgrammingSubmissions.number.desc()).first()

	if number is None:
		number = 1
	else:
		number = number[0] + 1

	submission = ProgrammingSubmissions(pid, tid, submission_path, message, log, number, duration)

	correct = message == "Correct!"

	with app.app_context():
		solve = Solves(pid, _user.uid, tid, submission_path, correct)
		db.session.add(solve)
		db.session.add(submission)
		db.session.commit()

		if correct:
			# Wait until after the solve has been added to the database before adding bonus
			solves = problem.get_solves(pid=pid)
			solve.bonus = [-1, solves][solves < 3]
			db.session.add(solve)
			cache.invalidate_memoization(problem.get_solves, pid)

			if _user:
				activity = UserActivity(_user.uid, 3, tid=tid, pid=pid)
				db.session.add(activity)

			db.session.commit()
		new = {
			"psid": submission.psid,
			"title": _problem.title,
			"message": submission.message,
			"log": submission.log,
			"date": utils.isoformat(submission.date),
			"number": submission.number
		}

	shutil.rmtree(submission_folder)

	return { "success": message == "Correct!", "message": message , "new_submission": new }
Beispiel #7
0
def submit_program():
    params = utils.flat_multi(request.form)

    pid = params.get("pid")
    tid = session.get("tid")
    _user = user.get_user().first()

    language = params.get("language")
    submission_contents = params.get("submission")

    _problem = problem.get_problem(pid=pid).first()
    if _problem is None:
        raise WebException("Problem does not exist.")

    if _problem.category != "Programming":
        raise WebException("Can't judge this problem.")

    if language not in extensions:
        raise WebException("Language not supported.")

    solved = Solves.query.filter_by(pid=pid, tid=tid, correct=1).first()
    if solved:
        raise WebException("You already solved this problem.")

    judge_folder = os.path.join(app.config["GRADER_FOLDER"], pid)
    if not os.path.exists(judge_folder):
        os.makedirs(judge_folder)

    submission_folder = os.path.join(judge_folder, utils.generate_string())
    while os.path.exists(submission_folder):
        submission_folder = os.path.join(judge_folder, utils.generate_string())

    os.makedirs(submission_folder)

    submission_path = os.path.join(submission_folder,
                                   "program.%s" % extensions[language])

    open(submission_path, "w").write(submission_contents)
    message, log, duration = judge(submission_path, language, pid)

    number = ProgrammingSubmissions.query.filter_by(tid=tid).with_entities(
        ProgrammingSubmissions.number).order_by(
            ProgrammingSubmissions.number.desc()).first()

    if number is None:
        number = 1
    else:
        number = number[0] + 1

    submission = ProgrammingSubmissions(pid, tid, submission_path, message,
                                        log, number, duration)

    correct = message == "Correct!"

    with app.app_context():
        solve = Solves(pid, _user.uid, tid, submission_path, correct)
        db.session.add(solve)
        db.session.add(submission)
        db.session.commit()

        if correct:
            # Wait until after the solve has been added to the database before adding bonus
            solves = problem.get_solves(pid=pid)
            solve.bonus = [-1, solves][solves < 3]
            db.session.add(solve)
            cache.invalidate_memoization(problem.get_solves, pid)

            if _user:
                activity = UserActivity(_user.uid, 3, tid=tid, pid=pid)
                db.session.add(activity)

            db.session.commit()
        new = {
            "psid": submission.psid,
            "title": _problem.title,
            "message": submission.message,
            "log": submission.log,
            "date": utils.isoformat(submission.date),
            "number": submission.number
        }

    shutil.rmtree(submission_folder)

    return {
        "success": message == "Correct!",
        "message": message,
        "new_submission": new
    }