Esempio n. 1
0
def load():
    import sys, json
    from Websheet import Websheet
    student = sys.argv[2]
    ispreview = sys.argv[3] == 'True'
    try:
        websheet = Websheet.from_name(sys.argv[1], ispreview, student)
        if websheet == None:
            print("Could not find " + sys.argv[1])
            sys.exit(1)
        dbslug = websheet.dbslug

        import config, json

        authinfo = json.loads("".join(sys.stdin))
        instructor = config.get_instructor(student)
        if "username" in authinfo:  # if not anonymous
            # checks if you can see the solution -- are his instructor (or himself)
            if not authinfo["username"] in [instructor, student]:
                print("You can't see the submission of " + student +
                      ". You are not his instructor (nor himself).")
                return

        if not config.db_enabled: student = "anonymous"
        #  websheet = Websheet.from_filesystem(classname)
        data = {
            "template_code": websheet.get_json_template(),
            "description": websheet.description,
            "user_code": "",  # config.load_submission(student, dbslug),
            "ever_passed": config.ever_passed(student, dbslug),
            "num_submissions": config.num_submissions(student, dbslug),
            "initial_snippets": websheet.get_initial_snippets(),
            "lang": websheet.lang,
            "sharing": websheet.sharing,
            "attempts_until_ref": websheet.attempts_until_ref,
            "authinfo": authinfo
        }
        if websheet.nocode:
            data["nocode"] = websheet.get_nocode_question()
        else:
            data["nocode"] = False
        if (ispreview or data["ever_passed"]
                and websheet.attempts_until_ref != "never" or
            (websheet.attempts_until_ref not in ["never", "infinity"]
             and data["num_submissions"] >= websheet.attempts_until_ref)):
            data["reference_sol"] = websheet.get_reference_snippets()
        print(json.dumps(data, indent=4, separators=(',', ': ')))  # pretty!
    except FileNotFoundError:
        print("No exercise named " + sys.argv[1])
Esempio n. 2
0
def load():
  import sys, json
  from Websheet import Websheet
  student = sys.argv[2]
  ispreview = sys.argv[3]=='True'
  try:
    websheet = Websheet.from_name(sys.argv[1], ispreview, student)
    if websheet == None:
      print("Could not find " + sys.argv[1])
      sys.exit(1)
    dbslug = websheet.dbslug
    
    import config, json
    
    authinfo = json.loads("".join(sys.stdin))
    instructor = config.get_instructor(student)
    if "username" in authinfo: # if not anonymous
        # checks if you can see the solution -- are his instructor (or himself)
        if not authinfo["username"] in [instructor, student]:  
            print("You can't see the submission of " + student
                 + ". You are not his instructor (nor himself).")
            return
          
    if not config.db_enabled: student="anonymous"
    #  websheet = Websheet.from_filesystem(classname)
    data = {"template_code":websheet.get_json_template(),
            "description":websheet.description,
            "user_code": config.load_submission(student, dbslug),
            "ever_passed": config.ever_passed(student, dbslug),
            "num_submissions": config.num_submissions(student, dbslug),
            "initial_snippets": websheet.get_initial_snippets(),            
            "lang": websheet.lang,
            "sharing": websheet.sharing,
            "attempts_until_ref": websheet.attempts_until_ref,
            "authinfo": authinfo
          }
    if websheet.nocode:
      data["nocode"] = websheet.get_nocode_question()
    else:
      data["nocode"] = False
    if (ispreview or 
        data["ever_passed"] and websheet.attempts_until_ref != "never"
        or (websheet.attempts_until_ref not in ["never", "infinity"] and
            data["num_submissions"] >= websheet.attempts_until_ref)) :
      data["reference_sol"] = websheet.get_reference_snippets()
    print(json.dumps(data, indent=4, separators=(',', ': '))) # pretty!
  except FileNotFoundError:
    print("No exercise named " + sys.argv[1])
Esempio n. 3
0
try:
  websheet = Websheet.from_name(sys.argv[1], ispreview, student)
  if websheet == None:
    print("Could not find " + sys.argv[1])
    sys.exit(1)
  dbslug = websheet.dbslug
  
  import config, json
  
  if not config.db_enabled: student="anonymous"
  #  websheet = Websheet.from_filesystem(classname)
  data = {"template_code":websheet.get_json_template(),
          "description":websheet.description,
          "user_code": config.load_submission(student, dbslug),
          "ever_passed": config.ever_passed(student, dbslug),
          "num_submissions": config.num_submissions(student, dbslug),
          "initial_snippets": websheet.get_initial_snippets(),            
          "lang": websheet.lang,
          "sharing": websheet.sharing,
          "attempts_until_ref": websheet.attempts_until_ref,
          "authinfo": json.loads("".join(sys.stdin))
        }
  if websheet.nocode:
    data["nocode"] = websheet.get_nocode_question()
  else:
    data["nocode"] = False
  if (ispreview or 
      data["ever_passed"] and websheet.attempts_until_ref != "never"
      or (websheet.attempts_until_ref not in ["never", "infinity"] and
          data["num_submissions"] >= websheet.attempts_until_ref)) :
    data["reference_sol"] = websheet.get_reference_snippets()
Esempio n. 4
0
#!/usr/bin/python3

if __name__ == "__main__":
  import sys
  from Websheet import Websheet
  student = sys.argv[2]
  websheet = Websheet.from_filesystem(sys.argv[1])
  classname = websheet.classname

  import config, json

  if not config.db_enabled: student="anonymous"
#  websheet = Websheet.from_filesystem(classname)
  print(json.dumps({"template_code":websheet.get_json_template(),
                    "description":websheet.description,
                    "user_code": config.load_submission(student, classname),
                    "ever_passed": config.ever_passed(student, classname),
                    "num_submissions": config.num_submissions(student, classname),
                    "reference_sol": websheet.get_reference_snippets(),
                    "initial_snippets": websheet.get_initial_snippets()
                    }))
Esempio n. 5
0
def submit_and_log(websheet_name, student, client_request, meta):

  config.meta = meta

  websheet = Websheet.Websheet.from_name(websheet_name, client_request['preview']=='True', student)

  errmsg = None
  epilogue = None
  
  def compile_and_run():
    nonlocal errmsg, epilogue

    if websheet.nocode:
      if websheet.evaluate_nocode_submission(client_request["nocode_state"]):
        return ("Passed", "<div>Correct!</div>")
      else:
        return ("Failed", "<div>Not correct, please try again.</div>")

    user_poschunks = client_request["snippets"]

    # this is the pre-syntax check
    student_solution = websheet.combine_with_template(user_poschunks, "student")
    if student_solution[0] == False:
        if student_solution[1] == "Internal error! Wrong number of inputs":
          return("Internal Error (Wrong number of snippets)", "Error: wrong number of snippets")

        errmsg = student_solution[1].split('\n')
        if len(errmsg) > 1:
          errmsg = errmsg[1]
        else:
          errmsg = errmsg[0]
        
        return("Pre-syntax Error",
               "<div class='pre-syntax-error'>Syntax error:" + 
               "<pre>\n"+cgi.escape(student_solution[1])+"</pre></div>") # error text

    ss_to_ui_linemap = student_solution[2]

    def translate_line(ss_lineno):
        ss_lineno = int(ss_lineno)
        if ss_lineno in ss_to_ui_linemap:
          return str(ss_to_ui_linemap[ss_lineno])
        else:
          return "???("+str(ss_lineno)+")" + "<!--" + json.dumps(ss_to_ui_linemap) + "-->"
        
    reference_solution = websheet.get_reference_solution("reference")

    ss = student_solution[1]
    for i in range(len(ss)):
      if badchar(ss[i]):
        return("Pre-syntax Error",
               "<div class='pre-syntax-error'>Syntax error:" + 
               " Your code contains character number " + str(ord(ss[i])) + ":<br>" + 
               "<pre>"
               +("&hellip;" if i>5 else "")
               +cgi.escape(ss[max(0,i-5):i])
               +"<b style='background:orange'>"
               +cgi.escape(ss[i])+"</b>"
               +cgi.escape(ss[i+1:i+6])
               +("&hellip;" if i+5<len(ss) else "")
               +"</pre></div>")

    for verboten in websheet.verboten:
        for chunk in user_poschunks:
          if verboten in chunk['code']:
            return("Pre-syntax Error",
                   "<div class='pre-syntax-error'>" + 
                   "You can't use " + tt(verboten) + 
                   " in this exercise.</div>")

    #print(cgi.escape(student_solution[1]))
    #print(cgi.escape(reference_solution))

    if websheet.lang.startswith("C++"):
      student_solution = student_solution[1]
      if websheet.lang == "C++func":
        student_solution = re.sub(r"\bmain\b", "__student_main__", student_solution)

    if websheet.lang.startswith("C++"):
      import grade_cpp
      return grade_cpp.grade(reference_solution, student_solution, translate_line, websheet)
    elif websheet.lang == "Java":
      import grade_java
      # java needs to know student name to look up their solutions for dependencies
      return grade_java.grade(reference_solution, student_solution, translate_line, websheet, student)
    

  try:
    category, results = compile_and_run()
    config.uncreate_tempdirs()
  except Exception:
    category = "Internal Error (Script)"
    import traceback
    results = '<pre>' + cgi.escape(traceback.format_exc()) + '</pre>'

  if category.startswith("Internal Error"):
    results = "<b><p>"+category+"; please report to course staff!</p></b>" + results

  import copy
    
  print_output = {'category': category, 'results' : results}
  save_this = {'category': category}
  if errmsg is not None:
    print_output['errmsg'] = errmsg
    save_this['errmsg'] = errmsg
  if category=='Passed' and websheet.epilogue is not None: 
    print_output['epilogue'] = websheet.epilogue

  passed = (category == "Passed")

  global authinfo
  #print(authinfo)
  if authinfo["error_span"] != "":
    print_output["results"] = authinfo["error_span"]
    print_output["category"] = "Auth Error" 
    meta["logout_bug"] = True

  if (not client_request["viewing_ref"]):
    if websheet.nocode:
      user_state = client_request["nocode_state"]
    else:
      # remove positional information
      user_state = [blank["code"] for blank in client_request["snippets"]]
    config.save_submission(student, websheet.dbslug, user_state, save_this, passed)

  if websheet.attempts_until_ref == 'never':
    pass
  elif websheet.attempts_until_ref == 'infinity':
    if passed:
      print_output["reference_sol"] = websheet.get_reference_snippets()
  else:
    if passed or config.num_submissions(student, websheet.dbslug) == websheet.attempts_until_ref:
      print_output["reference_sol"] = websheet.get_reference_snippets()

  # we can't reliably track # submissions of anonymous users so put trust in the UI
  if ('reference_sol' not in print_output and authinfo['username'] == 'anonymous'
      and websheet.attempts_until_ref not in ['never', 'infinity']):
    print_output["anon_reference_sol"] = websheet.get_reference_snippets()

  return print_output
Esempio n. 6
0
        websheet = Websheet.from_name(sys.argv[1], ispreview, student)
        if websheet == None:
            print("Could not find " + sys.argv[1])
            sys.exit(1)
        dbslug = websheet.dbslug

        import config, json

        if not config.db_enabled: student = "anonymous"
        #  websheet = Websheet.from_filesystem(classname)
        data = {
            "template_code": websheet.get_json_template(),
            "description": websheet.description,
            "user_code": config.load_submission(student, dbslug),
            "ever_passed": config.ever_passed(student, dbslug),
            "num_submissions": config.num_submissions(student, dbslug),
            "initial_snippets": websheet.get_initial_snippets(),
            "lang": websheet.lang,
            "sharing": websheet.sharing,
            "attempts_until_ref": websheet.attempts_until_ref,
            "authinfo": json.loads("".join(sys.stdin))
        }
        if websheet.nocode:
            data["nocode"] = websheet.get_nocode_question()
        else:
            data["nocode"] = False
        if (ispreview or data["ever_passed"]
                and websheet.attempts_until_ref != "never" or
            (websheet.attempts_until_ref not in ["never", "infinity"]
             and data["num_submissions"] >= websheet.attempts_until_ref)):
            data["reference_sol"] = websheet.get_reference_snippets()