autograder.cmd.gradeassignment
Grade an assignment (specified by an assignment JSON file) with the given submission. Non-Python assignments can be graded, but they require an "invocation" field' in the assignment config, and the running machine must be configured to run them' (e.g. have all the required software installed).
1""" 2Grade an assignment (specified by an assignment JSON file) with the given submission. 3Non-Python assignments can be graded, but they require an "invocation" field' 4in the assignment config, and the running machine must be configured to run them' 5(e.g. have all the required software installed). 6""" 7 8import argparse 9import os 10import sys 11 12import edq.util.dirent 13import edq.util.json 14 15import autograder.cli.parser 16import autograder.submission 17 18DEFAULT_ASSIGNMENT: str = 'assignment.json' 19TEST_SUBMISSION_FILENAME: str = 'test-submission.json' 20 21def run(args: argparse.Namespace) -> int: 22 """ 23 Grade an assignment, output the report, and optionally output additional out or test files. 24 """ 25 26 assignment_config_path = os.path.abspath(args.assignment) 27 submission_path = os.path.abspath(args.submission) 28 29 grading_dir = autograder.submission.prep_grading_dir(assignment_config_path, submission_path) 30 31 result = autograder.submission.run_submission(grading_dir, 32 assignment_config_path = assignment_config_path) 33 if (result is None): 34 return 2 35 36 print(result.report()) 37 38 if (args.out_path is not None): 39 out_path = os.path.abspath(args.out_path) 40 edq.util.dirent.mkdir(os.path.dirname(os.path.abspath(out_path))) 41 42 edq.util.json.dump_path(result, out_path, indent = 4) 43 44 if (args.test_submission_path is not None): 45 test_submission_path = os.path.abspath(args.test_submission_path) 46 if (os.path.isdir(test_submission_path)): 47 test_submission_path = os.path.join(test_submission_path, TEST_SUBMISSION_FILENAME) 48 49 edq.util.dirent.mkdir(os.path.dirname(test_submission_path)) 50 51 edq.util.json.dump_path(result.to_test_submission(), test_submission_path, indent = 4) 52 53 return 0 54 55def _get_parser() -> argparse.ArgumentParser: 56 """ Get a parser for this operation. """ 57 58 parser = autograder.cli.parser.get_parser(__doc__.strip()) 59 60 parser.add_argument('-a', '--assignment', 61 action = 'store', type = str, required = False, default = DEFAULT_ASSIGNMENT, 62 help = 'The path to a JSON file describing an assignment (default: %(default)s).') 63 64 parser.add_argument('-s', '--submission', 65 action = 'store', type = str, required = True, 66 help = 'The path to a submission to use for grading.') 67 68 parser.add_argument('-o', '--out-path', dest = 'out_path', 69 action = 'store', type = str, required = False, default = None, 70 help = 'The path to a output the JSON result.') 71 72 parser.add_argument('-t', '--test-submission-path', dest = 'test_submission_path', 73 action = 'store', type = str, required = False, default = None, 74 help = ('Create a test submission file at the specified path.' 75 + ' If an existing dir is provided,' 76 + f" a '{TEST_SUBMISSION_FILENAME}' file will be created inside that dir.")) 77 78 return parser 79 80def main() -> int: 81 """ Get a parser, parse the args, and call run. """ 82 83 args, _ = _get_parser().parse_known_args() 84 return run(args) 85 86if (__name__ == '__main__'): 87 sys.exit(main())
DEFAULT_ASSIGNMENT: str =
'assignment.json'
TEST_SUBMISSION_FILENAME: str =
'test-submission.json'
def
run(args: argparse.Namespace) -> int:
22def run(args: argparse.Namespace) -> int: 23 """ 24 Grade an assignment, output the report, and optionally output additional out or test files. 25 """ 26 27 assignment_config_path = os.path.abspath(args.assignment) 28 submission_path = os.path.abspath(args.submission) 29 30 grading_dir = autograder.submission.prep_grading_dir(assignment_config_path, submission_path) 31 32 result = autograder.submission.run_submission(grading_dir, 33 assignment_config_path = assignment_config_path) 34 if (result is None): 35 return 2 36 37 print(result.report()) 38 39 if (args.out_path is not None): 40 out_path = os.path.abspath(args.out_path) 41 edq.util.dirent.mkdir(os.path.dirname(os.path.abspath(out_path))) 42 43 edq.util.json.dump_path(result, out_path, indent = 4) 44 45 if (args.test_submission_path is not None): 46 test_submission_path = os.path.abspath(args.test_submission_path) 47 if (os.path.isdir(test_submission_path)): 48 test_submission_path = os.path.join(test_submission_path, TEST_SUBMISSION_FILENAME) 49 50 edq.util.dirent.mkdir(os.path.dirname(test_submission_path)) 51 52 edq.util.json.dump_path(result.to_test_submission(), test_submission_path, indent = 4) 53 54 return 0
Grade an assignment, output the report, and optionally output additional out or test files.
def
main() -> int:
81def main() -> int: 82 """ Get a parser, parse the args, and call run. """ 83 84 args, _ = _get_parser().parse_known_args() 85 return run(args)
Get a parser, parse the args, and call run.