|
| 1 | +#!/usr/bin/python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | +# Script to find assignments which have not been graded |
| 4 | +# Filename: missing_grade.py |
| 5 | +import json, os |
| 6 | +import argparse, requests |
| 7 | +import sys |
| 8 | +import zlib |
| 9 | +from datetime import datetime |
| 10 | + |
| 11 | +TASK = '' |
| 12 | +WEEK = '' |
| 13 | +DEADLINE = 0 |
| 14 | +CANVAS_TOKEN = '' |
| 15 | +CANVAS_URL = "https://canvas.kth.se" |
| 16 | +CANVAS_COURSE_ID = 48942 # 2024 edition |
| 17 | +CONTRIBUTION_PATH = '../contributions' |
| 18 | + |
| 19 | + |
| 20 | +# Mapping from github task name to canvas group set id |
| 21 | +def task_to_set(task_name, canvas_set): |
| 22 | + mapping = { |
| 23 | + #"course-automation": canvas_set["Course automation"], |
| 24 | + "demo": canvas_set["Demos"], |
| 25 | + "scientific-paper": canvas_set["Scientific Papers"], |
| 26 | + "executable-tutorial": canvas_set["Executable Tutorials"], |
| 27 | + "feedback": canvas_set["Feedback"], |
| 28 | + "opensource": canvas_set["Open-source contributions"], |
| 29 | + "presentation": canvas_set["Presentations"], |
| 30 | + } |
| 31 | + return mapping.get(task_name, Exception("Groupset mapping")) |
| 32 | + |
| 33 | + |
| 34 | +# Get all the group sets -> name : id |
| 35 | +def get_group_categories(): |
| 36 | + url = "{0}/api/v1/courses/{1}/group_categories".format(CANVAS_URL, CANVAS_COURSE_ID) |
| 37 | + r = requests.get(url, headers={'Authorization': 'Bearer ' + CANVAS_TOKEN}) |
| 38 | + if 'next' in r.links: raise Exception("need to implement paging") |
| 39 | + return {group["name"]: group["id"] for group in json.loads(r.content)} |
| 40 | + |
| 41 | + |
| 42 | +# Get assignments -> name : id |
| 43 | +def get_assignments(): |
| 44 | + url = "{0}/api/v1/courses/{1}/assignments".format(CANVAS_URL, CANVAS_COURSE_ID) |
| 45 | + r = requests.get(url, headers={'Authorization': 'Bearer ' + CANVAS_TOKEN}) |
| 46 | + if 'next' in r.links: raise Exception("need to implement paging") |
| 47 | + return {assignment["name"]: assignment["id"] for assignment in json.loads(r.content)} |
| 48 | + |
| 49 | + |
| 50 | +# Get groups in a group set -> name : id |
| 51 | +def list_groups(id_group_category): |
| 52 | + url = "{0}/api/v1/group_categories/{1}/groups?per_page=200".format(CANVAS_URL, id_group_category) |
| 53 | + r = requests.get(url, headers={'Authorization': 'Bearer ' + CANVAS_TOKEN}) |
| 54 | + if 'next' in r.links: raise Exception("need to implement paging") |
| 55 | + return {group["name"]: group["id"] for group in json.loads(r.content)} |
| 56 | + |
| 57 | +# Get groups members -> name : id |
| 58 | +def get_group_members(id_group): |
| 59 | + url = "{0}/api/v1/groups/{1}/users".format(CANVAS_URL, id_group) |
| 60 | + r = requests.get(url, headers={'Authorization': 'Bearer ' + CANVAS_TOKEN}) |
| 61 | + if 'next' in r.links: raise Exception("need to implement paging") |
| 62 | + return {member["name"]: member["id"] for member in json.loads(r.content)} |
| 63 | + |
| 64 | + |
| 65 | +def grader(url): |
| 66 | + url = url.replace("/2024/","/2023/") |
| 67 | + graders = json.load(open("graders.json")) |
| 68 | + n= zlib.adler32(url.encode("utf-8"))%len(graders) |
| 69 | + return graders[n] |
| 70 | + raise Exception() |
| 71 | + |
| 72 | +# Get groups in a group set |
| 73 | +def check_group_grading(groups, id_assignment): |
| 74 | + for group in groups: |
| 75 | + members = get_group_members(groups[group]) |
| 76 | + for member in members: |
| 77 | + # doc: https://canvas.instructure.com/doc/api/assignments.html |
| 78 | + canvas_url = "{0}/api/v1/courses/{1}/assignments/{2}/submissions/{3}".format(CANVAS_URL, CANVAS_COURSE_ID, |
| 79 | + id_assignment, |
| 80 | + members[member]) |
| 81 | + |
| 82 | + # {'include[]': "submission_comments"} is a non-documented flag provided by colleague Chip Maguire |
| 83 | + r = requests.get(canvas_url, headers={'Authorization': 'Bearer ' + CANVAS_TOKEN}, params = {'include[]': "submission_comments"}) |
| 84 | + if 'next' in r.links: raise Exception("need to implement paging") |
| 85 | + |
| 86 | + comments = [x for x in r.json()["submission_comments"]] |
| 87 | + |
| 88 | + last_comment = comments[-1]["comment"] if len(comments)>0 else "" |
| 89 | + |
| 90 | + url = "https://github.com/KTH/devops-course/tree/"+datetime.today().strftime("%Y")+"/contributions/"+TASK+"/"+group |
| 91 | + |
| 92 | + if "FAIL" in last_comment: |
| 93 | + print(url," failed by ",comments[-1]["author_name"]) |
| 94 | + continue |
| 95 | + |
| 96 | + if "FAIL" not in last_comment and json.loads(r.content)["entered_grade"] == "incomplete": |
| 97 | + print(url," REPEAT assigned to grader",grader(url)) |
| 98 | + continue |
| 99 | + |
| 100 | + graded = "graded" == json.loads(r.content)["workflow_state"] |
| 101 | + if not graded: |
| 102 | + #print(url) |
| 103 | + print(url," assigned to grader",grader(url)) |
| 104 | + |
| 105 | + #print("missing grade for", TASK, "of", group) |
| 106 | + break |
| 107 | + |
| 108 | + |
| 109 | + |
| 110 | +# Get sub directories of a given path |
| 111 | +def get_sub_directory(path): |
| 112 | + categories = dict() |
| 113 | + |
| 114 | + for dirpath, dirnames, filenames in os.walk(path, topdown=True): |
| 115 | + for category in dirnames: |
| 116 | + categories[category] = {"path": os.path.join(dirpath, category)} |
| 117 | + break |
| 118 | + |
| 119 | + return categories |
| 120 | + |
| 121 | + |
| 122 | +# Parse arguments of the script |
| 123 | +def parse_args(): |
| 124 | + global TASK |
| 125 | + global WEEK |
| 126 | + global DEADLINE |
| 127 | + global CANVAS_TOKEN |
| 128 | + |
| 129 | + parser = argparse.ArgumentParser() |
| 130 | + parser.add_argument('--task', dest='task', type=str, help='Task name', required=True) |
| 131 | + parser.add_argument('--token', dest='token', type=str, help='Canvas access token', required=True) |
| 132 | + parser.add_argument('--week', dest='week', type=str, help='Week folder (ONLY for presentation/demo/scientific-paper)') |
| 133 | + parser.add_argument('--deadline', dest='deadline', type=int, help='Deadline number (NOT for presentation/demo/scientific-paper)') |
| 134 | + |
| 135 | + args = parser.parse_args() |
| 136 | + TASK = args.task |
| 137 | + WEEK = args.week |
| 138 | + DEADLINE = args.deadline |
| 139 | + CANVAS_TOKEN = args.token |
| 140 | + |
| 141 | + |
| 142 | +# Keep only the groups for a given deadline number |
| 143 | +# For each group, we check the readme a look for "task x" if task is not present we keep the group |
| 144 | +def filter_deadline_groups(groups, deadline): |
| 145 | + sorted_groups = dict() |
| 146 | + for group in groups: |
| 147 | + f = open(groups[group]["path"] + '/README.md', "r") |
| 148 | + file = f.read().lower() |
| 149 | + |
| 150 | + #if 'task ' not in file: |
| 151 | + #sorted_groups[group] = groups[group] |
| 152 | + #print("Not sure if the group " + group + " is in for this deadline, checking it anyway\n") |
| 153 | + if 'task ' + str(deadline) in file: |
| 154 | + sorted_groups[group] = groups[group] |
| 155 | + |
| 156 | + return sorted_groups |
| 157 | + |
| 158 | +def check_all_assigned(): |
| 159 | + l = [] |
| 160 | + #mapping = { |
| 161 | + #"course-automation": canvas_set["Course automation"], |
| 162 | + #"demo": canvas_set["Demos"], |
| 163 | + #"essay": canvas_set["Essays"], |
| 164 | + #"executable-tutorial": canvas_set["Executable Tutorials"], |
| 165 | + #"feedback": canvas_set["Feedback"], |
| 166 | + #"open-source": canvas_set["Open-source contributions"], |
| 167 | + #"presentation": canvas_set["Presentations"], } |
| 168 | + # for i in get_sub_directory(CONTRIBUTION_PATH+"/"+"course-automation").values(): |
| 169 | + # l.append(i['path']) |
| 170 | + # for i in get_sub_directory(CONTRIBUTION_PATH+"/"+"essay").values(): |
| 171 | + # l.append(i['path']) |
| 172 | + for i in get_sub_directory(CONTRIBUTION_PATH+"/"+"executable-tutorial").values(): |
| 173 | + l.append(i['path']) |
| 174 | + for i in get_sub_directory(CONTRIBUTION_PATH+"/"+"feedback").values(): |
| 175 | + l.append(i['path']) |
| 176 | + for i in get_sub_directory(CONTRIBUTION_PATH+"/"+"open-source").values(): |
| 177 | + l.append(i['path']) |
| 178 | + |
| 179 | + # already done |
| 180 | + #for i in get_sub_directory(CONTRIBUTION_PATH+"/"+"presentation").values(): |
| 181 | + #for j in get_sub_directory(i['path']).values(): |
| 182 | + #l.append(j['path']) |
| 183 | + #for i in get_sub_directory(CONTRIBUTION_PATH+"/"+"demo").values(): |
| 184 | + #for j in get_sub_directory(i['path']).values(): |
| 185 | + #l.append(j['path']) |
| 186 | + |
| 187 | + # remove "../" |
| 188 | + l = ["https://github.com/KTH/devops-course/tree/"+datetime.today().strftime("%Y")+"/"+x[3:] for x in l] |
| 189 | + |
| 190 | + |
| 191 | + #print(l) |
| 192 | + |
| 193 | +def main(): |
| 194 | + parse_args() |
| 195 | + |
| 196 | + if TASK == "check_all_assigned": |
| 197 | + check_all_assigned() |
| 198 | + return |
| 199 | + |
| 200 | + canvas_groups_set = get_group_categories() |
| 201 | + canvas_groups_category_id = task_to_set(TASK, canvas_groups_set) |
| 202 | + canvas_groups = list_groups(canvas_groups_category_id) |
| 203 | + canvas_assignment = get_assignments() |
| 204 | + canvas_assignment_id = task_to_set(TASK, canvas_assignment) |
| 205 | + |
| 206 | + task_sub = get_sub_directory(CONTRIBUTION_PATH + '/' + TASK) |
| 207 | + |
| 208 | + # Filter github groups according to args |
| 209 | + if WEEK is not None: |
| 210 | + groups = get_sub_directory(task_sub[WEEK]["path"]) |
| 211 | + print("Found " + str(len(groups)) + " group(s) for " + TASK + " in " + WEEK) |
| 212 | + elif DEADLINE is not None: |
| 213 | + groups = filter_deadline_groups(task_sub, str(DEADLINE)) |
| 214 | + print(TASK + ", deadline " + str(DEADLINE)) |
| 215 | + else: |
| 216 | + groups = canvas_groups |
| 217 | + print("Found " + str(len(groups)) + " group(s) for " + TASK) |
| 218 | + |
| 219 | + # Intersection of canvas groups and github filtered groups |
| 220 | + canvas_groups = {x: canvas_groups[x] for x in canvas_groups if x in groups} |
| 221 | + |
| 222 | + # Check the grading |
| 223 | + check_group_grading(canvas_groups, canvas_assignment_id) |
| 224 | + |
| 225 | + |
| 226 | +main() |
0 commit comments