Example #1
0
File: views.py Project: rcoder/vrfy
def _get_problem_result(solution,request):
  '''
  attempt to get the problem from tango if it's been autograded
  if not, get the normal result
  if the result doesn't exist, return none
  '''
  ps = solution.student_problem_set.problem_set
  #poll the tango server
  if solution.problem.autograde_problem:
    prob_result = ProblemResult.objects.get(sp_sol = solution, job_id=solution.job_id, attempt_num=solution.attempt_num)
    outputFile = slugify(ps.title) + "_" +slugify(solution.problem.title) + "-" + request.user.username
    r = tango.poll(solution.problem, ps, outputFile)
    raw_output = r.text
    line = r.text.split("\n")[-2]#theres a line with an empty string after the last actual output line
    tango_time = r.text.split("\n")[0].split("[")[1].split("]")[0] #the time is on the first line surrounded by brackets
    tango_time = time.strftime("%Y-%m-%d %H:%M:%S", time.strptime(tango_time, '%a %b %d %H:%M:%S %Y'))
    
    if tango_time != str(prob_result.timestamp).split("+")[0]:
      print("hello")
      if "Autodriver: Job timed out after " in line: #thats the text that Tango outputs when a job times out
        prob_result.score = 0
        prob_result.json_log = {'score_sum':'0','external_log':["Program timed out after " + line.split(" ")[-2] + " seconds."]}
        prob_result.timestamp = tango_time
        prob_result.save()
      else:
        #try:
        log_data = json.loads(line)
        #create the result object
        prob_result.score = log_data["score_sum"]
        prob_result.raw_output = raw_output
        prob_result.json_log = log_data
        prob_result.timestamp = tango_time
        prob_result.save()
        #except ValueError: #if the json isn't there, something went wrong when running the job, or the grader file messed up
          #raise Http404("Something went wrong. Make sure your code is bug free and resubmit. \nIf the problem persists, contact your professor or TA")
  
  else:
    #special not-autograded stuff goes here
    prob_result = ProblemResult.objects.get(sp_sol = solution, attempt_num=solution.attempt_num)

  return prob_result
Example #2
0
    def _update_result(self, solution):
        ps = solution.get_problemset()
        reedie = solution.get_user()
        prob_result = models.ProblemResult.objects.get(
            user=reedie,
            sp_sol=solution,
            job_id=solution.job_id,
            attempt_num=solution.attempt_num)
        outputFile = slugify(ps.title) + "_" + \
            slugify(solution.problem.title) + "-" + str(reedie)
        r = tango.poll(solution.problem, ps, outputFile)
        while r.status_code is 404:
            time.sleep(.2)
            r = tango.poll(solution.problem, ps, outputFile)

        if r.status_code is 200:
            raw_output = r.text
            # Autograder [Tue Sep  1 21:39:00 2015]: Received job test-reassess-hw0_hw0reassess-isjoriss:25
            # there will be a bug here eventually ... note try / except block in views.py
            # line = r.text.split("\n")[-2]#theres a line with an empty string
            # after the last actual output line
            job_id = r.text.split("\n")[0].split(":")[4]
            # the time is on the first line surrounded by brackets
            tango_time = r.text.split("\n")[0].split("[")[1].split("]")[0]
            tango_time = time.strftime(
                "%Y-%m-%d %H:%M:%S",
                time.strptime(
                    tango_time,
                    '%a %b %d %H:%M:%S %Y'))
            tango_time = parse_datetime(tango_time)
            tango_time = timezone.make_aware(
                tango_time, timezone=timezone.UTC())
            while int(job_id) != prob_result.job_id:
                time.sleep(.5)
                r = tango.poll(solution.problem, ps, outputFile)
                try:
                    job_id = int(r.text.split("\n")[0].split(":")[4])
                except:
                    pass

            raw_output = r.text
            # TO DO: bug where the autograder returns successfully but the output is nothing so
            # trying to assign line throws an error
            # TO DO: make sure we don't keep requesting when we've spent more
            # time waiting for the job than the timeout of the problem
            # theres a line with an empty string after the last actual output
            # line
            line = r.text.split("\n")[-2]
            # the time is on the first line surrounded by brackets
            tango_time = r.text.split("\n")[0].split("[")[1].split("]")[0]
            tango_time = time.strftime(
                "%Y-%m-%d %H:%M:%S",
                time.strptime(
                    tango_time,
                    '%a %b %d %H:%M:%S %Y'))
            tango_time = parse_datetime(tango_time)
            tango_time = timezone.make_aware(
                tango_time, timezone=timezone.UTC())

            if "Autodriver: Job timed out after " in line:  # thats the text that Tango outputs when a job times out
                prob_result.score = 0
                prob_result.json_log = {'score_sum': '0', 'external_log': [
                    "Program timed out after " + line.split(" ")[-2] + " seconds."]}
                prob_result.timestamp = tango_time
                prob_result.raw_output = raw_output
                prob_result.save()
            else:
                # try:
                log_data = json.loads(line)
                # create the result object
                prob_result.max_score = log_data["max_score"]
                prob_result.score = log_data["score_sum"]
                prob_result.raw_output = raw_output
                prob_result.json_log = log_data
                prob_result.timestamp = tango_time
                prob_result.save()
        else:
            return redirect('500.html')