def run_grading_commands(hw_name, prob_name, main_dir, nlogo_dir, grade_only): '''This function runs all commands for grading a particular problem. Where applicable, commands are run through direct calls to python functions. Commands requiring Java are done using system calls to the terminal. Note that this method also avoids the issue of writing a script and so dealing with bash and bat differences. Args: hw_num (int): The homework number. prob_num (int): The problem number. main_dir (str): The name of the primary directory, which should contain a subdirectory for each student and one for answers. These subdirectories should contain student and answer NetLogo files. Student csv files will be written into each student's subdirectory and all additional files will be written into the same subdirectory as the answer file. nlogo_dir (str): The name of the directory containing the NetLogo.jar file and /lib subdirectory. grade_only (bool): If True, the function does not run java commands to produce the experiment xml file or the data csv files from each nlogo file. Instead, it assumes they already exist, and just grades them. Returns: list: True if successful, False otherwise. ''' # If creating files, create the experiment file. if not grade_only: write_experiment_file( open(main_dir + answer_dir_name + '/' + hw_name + '_answers.nlogo'), open( main_dir + answer_dir_name + '/' + hw_name + '_experiments.xml', 'w')) # If creating files, Change to directory containing NetLogo.jar and call # system to produce csv answer file. Get path of answer file. if not grade_only: os.chdir(nlogo_dir) os.system(nlogo_command.format(answer_dir_name, 'answers')) ans_path = main_dir + answer_dir_name + data_file_name # Grade student nlogo files. for h in os.listdir(main_dir): # Student subdirectories are identified as those directories which have # only digits in their names. if os.path.isdir(main_dir + h) and h.isdigit(): # If creating files, create student csv files from nlogo files. if not grade_only: os.system(nlogo_command.format(h, h)) # Grade csv files against answer file and record grades. stud_path = main_dir + h + data_file_name record_grade( get_problem_grade(grade_problem(ans_path, stud_path)), h, hw_name, prob_name, main_dir + '/grades.csv') os.system('echo "Graded ' + hw_name + '.' + prob_name + '"') return True
def grade(main_dir, netlogo_dir, answer_file, grade_only): '''A grading method *** ''' exp_list = [] model_path = '"' + main_dir + '{0}"' setup_file_path = '"' + main_dir + 'experiments.xml"' experiment = '{1}' table_path = '"' + main_dir + '{2}"' nlogo_command='java -Xmx1024m -Dfile.encoding=UTF-8 -cp NetLogo.jar ' + \ 'org.nlogo.headless.Main' + \ ' --model ' + model_path + \ ' --setup-file ' + setup_file_path + \ ' --experiment ' + experiment + \ ' --table ' + table_path + \ ' --threads 1 \n' # If creating files, create the experiment file. if not grade_only: try: os.chdir(main_dir) os.system('del *.csv') except: print("Could not delete .csv files") exp_list = write_experiment_file( open(main_dir + answer_file, 'r'), open(main_dir + 'experiments.xml', 'w')) print (exp_list) for i in range(2): # If creating files, Change to directory containing NetLogo.jar and call # system to produce csv answer file. Get path of answer file. if not grade_only: os.chdir(netlogo_dir) os.system(nlogo_command.format(answer_file, exp_list[i], 'answers'+exp_list[i]+'.csv')) ans_path = main_dir + 'answers'+exp_list[i]+'.csv' # Grade student nlogo files. for h in os.listdir(main_dir): print (h) # Student subdirectories are identified as those directories which have # only digits in their names. if ((os.path.isfile(main_dir + h) and h.count('_') != 0)) : stud_name = h.split('_')[0] print (stud_name) # If creating files, create student csv files from nlogo files. # if not grade_only: try: cmd = nlogo_command.format(h, exp_list[i], stud_name+exp_list[i]+'.csv') print (cmd) # os.system(cmd) subprocess.check_call(cmd,shell=True,timeout=10) except (OSError, CalledProcessError, TimeoutExpired) as e: print ("Error in running student file") # Appears that when it errors out, this command does not # exit, instead just blocking indefinitely # Furthermore, it apparently spawns orphan processes # which keep .csv data output files open indefinitely, # preventing deletion and further change # Grade csv files against answer file and record grades. stud_path = main_dir + stud_name+exp_list[i]+'.csv' record_grade(get_problem_grade(grade_problem(ans_path, stud_path)), h, '1', exp_list[i], main_dir + 'grades.csv') os.system('echo "Graded ' + '1' + '.' + exp_list[i] + '"') return True