-
Notifications
You must be signed in to change notification settings - Fork 3
/
process_submissions.py
1334 lines (944 loc) · 47.8 KB
/
process_submissions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
##!/usr/bin/env python
# -*- coding: utf-8 -*-
# pylint: disable=too-many-instance-attributes
r"""
For the purposes of this file, we assume that a student is either a student
or a team to make parsing easier, since most of the logic is identical.
See download_submission.process_assignment to see how to utilize this class
correctly.
TODO:
* Many of the methods of process_repos should be combined?
"""
__all__ = ["Submissions", ] # Controls what can be imported
__author__ = "Travis Janssen, David Tran"
__credits__ = ["Travis Janssen", "David Tran"]
__status__ = "Production"
__version__ = "1.0.0"
from datetime import datetime, timedelta
import inspect
import json
import itertools
import os
import platform
import re
import subprocess
import logging
logger = logging.getLogger(__name__)
class Submissions(object):
r"""
The purpose of this class is to download and process students' submissions.
This will not grade the submission, rather we automate the process of
acquiring the student's repos.
"""
def __init__(self, is_team, should_pull_repo_flag, folder_prefix="6300Fall18", git_context="gt-omscs-se-2018fall", edtech_platform="CANVAS", git_domain='github.gatech.edu'):
r"""
Defines the variables for the current class.
We could define static variables but they are not private and
are publicly accessible in Python.
Arguments:
self.is_team: (boolean) Sets if this submission is a team project
or not.
should_pull_repo_flag: (boolean) Sets if we should git pull,
if needed.
"""
# Pattern Matching
self.DATETIME_PATTERN = '%Y-%m-%d %H:%M:%S'
self.REGEX_PATTERN = '^[0-9]{4}(-[0-9]{2}){2} [0-9]{2}(:[0-9]{2}){2}$'
self.T_SQUARE_DATETIME_PATTERN = '%Y%m%d%H%M%S'
# Constants for the class
self.FOLDER_PREFIX = folder_prefix
self.GIT_DOMAIN = git_domain
self.GIT_CONTEXT = git_context
self.STUDENT_RECORDS_FILENAME = 'student_records.json'
self.STUDENT_ALIAS_FILENAME = 'student_aliases.json'
self.TEAM_RECORDS_FILENAME = 'student_records_teams.json'
self.TEAM_MEMBERS_FILENAME = 'student_records_team_members.json'
self.TIMESTAMP_FILENAME = 'timestamp.txt'
self.MAIN_REPO_DIR = 'student_repo'
self.PLATFORM = edtech_platform
self.PLATFORMS_VALID = ["CANVAS", "TSQUARE"]
self.ENCODING = "utf-8"
# Stored to be used in later logic, so typos between copies don't exist
self.STR_INVALID = "Invalid"
self.STR_LATE = "Late"
self.STR_MISSING = "Missing"
self.STR_NA = "N/A"
self.STR_OK = "Ok"
self.BAD_STR_LIST = [self.STR_INVALID, self.STR_MISSING]
# Actual non-constant attributes
# Cache results
self.cached_file_dicts = {} # Cache dictionary pulls
self.cached_teams_pulled = set() # Cache pulled teams
self.OS_TYPE = platform.system()
self.is_team = is_team
self.should_pull_repo_flag = should_pull_repo_flag
def process_repos(self, submission_folder_name,
assignment_code, deadline, student_whitelist=None, should_pull=True):
"""
This is the core function that will automate the download of
student submissions.
There is a sister function called _process_team_repos that focuses
on teams that is executed after this code. The logic contained
here mostly applies to both set of submissions.
Arguments:
submission_folder_name: (str) This is the directory for all
submissions that we will download. This must exist and will throw
an IOError if it does not exist.
assignment_code: (str) This is the two letter name for the
assignment.
deadline: (str) This is the deadline of the assignment if it is
late. The input must be in strict ISO 8601 format
'YYYY-MM-DDTHH:MM:SS'. As python 2 does NOT natively support
different timezones, this must be in UTC timezone to be correctly
comparable.
student_whitelist: (list of str) This is the list of student
username IDs that we will whitelist. That is to say all students
in the list will not be ignored. If set to None or empty list,
we will grab all students.
"""
result = re.match(self.REGEX_PATTERN, deadline)
if result is None:
str_buffer = (
"%s: input deadline is not a properly formatted ISO 8601 date\n"
"Please enter it as 'YYYY-MM-DDTHH:MM:SS'\n"
"Don't forget to convert that to UTC time has Python 2.X does "
" not natively support it."
)
print(str_buffer % inspect.currentframe().f_code.co_name)
return
assignment_alias = submission_folder_name.split('/')[-1]
if not os.path.isdir(self.MAIN_REPO_DIR):
os.makedirs(self.MAIN_REPO_DIR)
if not os.path.isdir(submission_folder_name):
raise IOError(
("%s: Submission folder name '%s' not found. "
"Please download this from %s before continuing. "
"Exiting.") %
(inspect.currentframe().f_code.co_name, submission_folder_name, self.PLATFORM.capitalize()))
# Guarantee that we will process something if we have an empty list
if not student_whitelist:
if self.is_team:
student_aliases = self._get_file_dict(filename=self.TEAM_MEMBERS_FILENAME, caller_name=inspect.currentframe().f_code.co_name)
else:
student_aliases = self._get_file_dict(
filename=self.STUDENT_ALIAS_FILENAME,
caller_name=inspect.currentframe().f_code.co_name)
student_whitelist = student_aliases.keys() # Get all students
if self.is_team:
team_records = self._get_file_dict(
filename=self.TEAM_RECORDS_FILENAME,
caller_name=inspect.currentframe().f_code.co_name)
team_members = self._get_file_dict(
filename = self.TEAM_MEMBERS_FILENAME,
caller_name=inspect.currentframe().f_code.co_name)
student_aliases = self._get_file_dict(
filename=self.STUDENT_ALIAS_FILENAME,
caller_name=inspect.currentframe().f_code.co_name)
student_records = self._get_file_dict(
filename=self.STUDENT_RECORDS_FILENAME,
caller_name=inspect.currentframe().f_code.co_name,
epilog="Run create_student_json first.")
# TSQUARE VERSION
directory_listing = self._get_student_folders(
submission_folder_name=submission_folder_name,
student_whitelist=student_whitelist,
assignment_code=assignment_code)
for folder in directory_listing:
"""
need 4 bits of information before processing the submission:
1) platform_id: unique numeric identifier per platform - this is unique per student, even if there are duplicate names
2) current_student: dictionary object for a student. This should contain personal information like GT_ID and name. Records will be stored from here to JSON files.
3) gt_username: login name for student - this is what the student will log into GitHub with
4) student_name: student's first and last name, usually in the form of "Last, First" and may include middle name(s)
"""
if self._should_process_team_submissions(assignment_code): # special handling required for group submissions (one file per group)
platform_id = self._get_group_submission_platform_id(submission_folder_name, folder, team_members, student_aliases)
student_name = folder
current_student = team_records.get(student_name, {})
gt_username = folder
else:
platform_id = folder.split('(')[1].strip(')')
current_student = student_records.get(platform_id, {})
if not current_student:
continue
gt_username = current_student['gt_id']
student_name = current_student['name']
if ((not self.is_team and
gt_username not in student_whitelist) or
(self.is_team and
team_records[gt_username] not in student_whitelist)
):
continue
# Checking repeated results on calls to simplify them
base_directory = self._get_submission_folder(submission_folder_name, folder)
current_assignment = current_student[assignment_alias] = {}
current_submission_file = self._get_submission_file_name(student_name, platform_id)
# TODO: These methods below should be combined together?
# Update submission text
self._check_submission_file(
current_assignment=current_assignment,
base_directory=base_directory,
submission_file=current_submission_file,
student_name=student_name,
platform_id=platform_id)
# Update t-square timestamp
self._set_timestamp_t_square(
current_assignment=current_assignment,
base_directory=base_directory)
# Clone repo if needed
# NOTE: You'll need to authenticate with Github here and
# debuggers may not work properly
self._setup_student_repo(gt_username=gt_username, should_pull=should_pull)
# Only check commit ID validity with GitHub timestamp
if self._is_commit_present(
commit_status=current_assignment['commitID']):
# Try to check out commit ID
self._check_commitID(
current_assignment=current_assignment,
assignment_code=assignment_code,
gt_username=gt_username)
self._compare_timestamp_github(
current_assignment=current_assignment,
gt_username=gt_username, deadline=deadline)
# Check T-Square timestamp against deadline
self._compare_timestamp_t_square(
current_assignment=current_assignment,
deadline=deadline)
# Reset the repo ptr to master if needed
#repo_suffix = self._get_correct_reference_id(
# graded_id=gt_username)
#self._execute_command(
# 'cd %s; git checkout master &> /dev/null' %
# self._gen_prefixed_dir(prefix_str=repo_suffix))
# Save Result
if self._should_process_team_submissions(assignment_code) and platform_id != '-1':
# save records for team on the submitters record; we need to pull this from existing records since team submissions don't need it
current_student['gt_id'] = student_records[platform_id]['gt_id']
current_student['name'] = student_records[platform_id]['name']
student_records[platform_id] = current_student
if student_records is not None:
# Save info
with open(self.STUDENT_RECORDS_FILENAME, 'w') as output_file:
json.dump(student_records, output_file)
if self.is_team and student_whitelist:
self._process_team_repos(
assignment_alias=assignment_alias,
assignment_code=assignment_code,
student_whitelist=student_whitelist)
print("\n\n>>>>>%s: complete for '%s'<<<<<\n\n" %
(inspect.currentframe().f_code.co_name, assignment_code))
def _process_team_repos(self, assignment_alias, assignment_code,
student_whitelist):
"""
This is the extension process_repos that focuses only on teams.
As such this should only be called on team repos.
Arguments:
assignment_alias: (str) This is the name of the assignment, i.e.
the submission name.
assignment_code: (str) This is the two letter name for the
assignment.
student_whitelist: (list of str) This is the list of student
username IDs that we will whitelist. That is to say all students
in the list will not be ignored. If set to None or empty list,
we will grab all students.
"""
student_aliases = self._get_file_dict(
filename=self.STUDENT_ALIAS_FILENAME,
caller_name=inspect.currentframe().f_code.co_name)
student_records = self._get_file_dict(
filename=self.STUDENT_RECORDS_FILENAME,
caller_name=inspect.currentframe().f_code.co_name,
epilog="Run _create_student_json first.")
team_records = self._get_file_dict(
filename=self.TEAM_MEMBERS_FILENAME,
caller_name=inspect.currentframe().f_code.co_name)
for team in student_whitelist:
member_list, commit_list = team_records[team], []
for student in member_list:
try:
platform_id = student_aliases[student]
team_assignment = (
student_records[platform_id][assignment_alias])
commit_time = team_assignment['Timestamp GitHub']
commitID = team_assignment['commitID']
except KeyError:
continue
if (self._is_commit_present(commit_status=commitID) and
commit_time != self.STR_NA):
commit_list.append((commit_time, commitID))
# checkout most recent commit here
if len(commit_list) > 0:
# Most recent should be first
commit_list.sort(reverse=True)
_, most_recent_commit = commit_list[0]
command = (
'cd %s; git checkout %s &> /dev/null; git tag -f %s &> /dev/null' % (
self._gen_prefixed_dir(team), most_recent_commit,
assignment_code))
_ = self._execute_command(command=command)
else:
print("%s: No valid commit for team '%s'!" % (
inspect.currentframe().f_code.co_name, team))
def generate_report(self, assignment, student_list=None,
report_filename=None):
r"""
This generates the final report that can be used by a grader.
The result is outputted to a file (report_filename) and to stdout.
Arguments:
assignment: (str) This is the name of the assignment we are
comparing against.
student_list: (list of str) This is a list of students that we
will analyze and prints the results.
report_filename: (str) This is the filename of the report will
generate, in addition to stdout. To disable this feature, pass in
None.
Returns:
A file, if set, with the results and the output to stdout.
"""
student_aliases = self._get_file_dict(
filename=self.STUDENT_ALIAS_FILENAME,
caller_name=inspect.currentframe().f_code.co_name)
student_records = self._get_file_dict(
filename=self.STUDENT_RECORDS_FILENAME,
caller_name=inspect.currentframe().f_code.co_name,
epilog="Run process_repos first.")
bad_commit, late_github, late_submission, missing, not_in_json = [], [], [], [], []
_init_log(log_filename=report_filename)
logger.info("Report: %s\n", assignment)
if self.is_team:
team_records = self._get_file_dict(
filename=self.TEAM_MEMBERS_FILENAME,
caller_name=inspect.currentframe().f_code.co_name)
new_student_list = []
if student_list == None:
student_list = self._get_file_dict(filename=self.TEAM_MEMBERS_FILENAME, caller_name=inspect.currentframe().f_code.co_name).keys()
for team in student_list:
members_list = team_records[team]
new_student_list.append(team)
new_student_list.extend(members_list)
student_list = new_student_list
elif not student_list:
student_list = student_aliases.keys() # Get all students
#else:
# We are passed a fixed set of students and this is not a team.
# Parse the student list for bad elements
stripped_list = map(str.strip, map(str, student_list))
final_list = list(filter(bool, stripped_list))
# This is a filter to get bad students in different spots
bad_student_dict = {
'Submission GitHub': (self.STR_LATE, late_github),
'Submission Time': (self.STR_LATE, late_submission),
'commitID': (self.STR_MISSING, missing),
'commitID valid': (False, bad_commit)
}
not_in_json = [] # bad_student_dict assumes a student was found; this will track students not in JSON files
for student in final_list:
if self.is_team and 'Team' in student:
logger.info("\n========== %s ==========", student)
continue
else:
logger.info(student)
try:
student_info = student_records[student_aliases[student]]
except KeyError:
if self.is_team:
continue
else:
not_in_json.append(student)
logger.info("\tMissing in JSON files")
continue
if assignment not in student_info:
logger.info('\tNo records found')
missing.append(student)
continue
student_info_assignment = student_info[assignment]
for key in sorted(student_info_assignment.keys(), reverse=True):
student_info_assignment_value = student_info_assignment[key]
logger.info('\t%s: %s', key, student_info_assignment_value)
try:
target_value, target_list = bad_student_dict[key]
if target_value == student_info_assignment_value:
target_list.append(student)
except KeyError:
pass
logger.info("\n========== RESULTS ==========")
str_buffer = ["\nLATE SUBMISSIONS:"]
for fmt_str, data in [("\tSubmission (%d): %s", late_submission),
("\tGitHub (%d): %s", late_github),
("\nMISSING SUBMISSIONS (%s): %s", missing),
("\nBAD COMMITS (%s):\n\t%s", bad_commit),
("MISSING FROM JSON (%s):\n\t%s", not_in_json)]:
str_buffer.append(fmt_str % (len(data), ", ".join(data)))
logger.info("\n".join(str_buffer))
def _setup_student_repo(self, gt_username, should_pull=True):
r"""
Checks if the student Git repo is downloaded and cleans it up for the
grader.
Assignment:
gt_username: (str) The student ID we will use download the repo.
"""
just_cloned_repo = None
repo_suffix = self._get_correct_reference_id(graded_id=gt_username)
if repo_suffix == None:
return # bad suffix - don't process
if not os.path.isdir(self._gen_prefixed_dir(prefix_str=repo_suffix)):
__ = self._execute_command("pwd")
command = ('cd %s && '
'git clone https://%s/%s/%s%s.git && '
'cd ..') % (
self.MAIN_REPO_DIR,
self.GIT_DOMAIN, self.GIT_CONTEXT, self.FOLDER_PREFIX, repo_suffix)
_ = self._execute_command(command=command)
self.cached_teams_pulled.add(repo_suffix)
just_cloned_repo = True
else:
just_cloned_repo = False
# Revert any local changes and pull from remote
try:
pull_flag = ''
if self._should_pull_repo(repo_suffix, should_pull) or just_cloned_repo:
pull_flag = 'git pull origin master -a && '
command = (
('cd %s && %s'
'git reset --hard && cd - &> /dev/null') % (
self._gen_prefixed_dir(prefix_str=repo_suffix), pull_flag))
_ = self._execute_command(command=command)
# TODO: Unneeded?
except subprocess.CalledProcessError as error:
try:
print("%s: student '%s' subprocess.CalledProcessError: %s\n" % (
inspect.currentframe().f_code.co_name,
gt_username, str(error.output)))
except UnicodeDecodeError:
print(("%s: student '%s' subprocess.CalledProcessError: "
"UnicodeDecodeError\n") % (
inspect.currentframe().f_code.co_name, gt_username))
def _execute_command(self, command):
r"""
Parses the command, if it is executed on Windows and returns the output.
Arguments:
command: (str) The command we will execute and return the result.
Return:
The command's output.
"""
if self.OS_TYPE == 'Windows':
# Windows chains commands with &, *nix with ;
command = command.replace('&> /dev/null', '')
command = command.replace(';', '&')
# Windows doesn't support 'go back to last directory'
command = command.replace('& cd -', '')
try:
raw_info = subprocess.check_output(command, shell=True).strip()
info = raw_info.decode(self.ENCODING)
except subprocess.CalledProcessError:
info = "failed"
return info
def create_student_json(self, input_filename, should_create_json_files=False):
r"""
Converts the input file to two useful JSON files specifically for student grading.
Arguments:
input_filename: (str) The input filename we will parse into JSON files.
Returns: None
"""
try:
with open(input_filename, 'r') as input_file:
gt_id_dict, student_records = {}, {}
for line in input_file:
parsed_line = line.strip().split('\t')
try:
if self.PLATFORM == "TSQUARE":
name, gt_id, platform_id = parsed_line[0:3]
elif self.PLATFORM == "CANVAS":
name, platform_id, gt_id = parsed_line[0:3]
else:
raise TypeError("create_student_json error! Currently selected platform %s isn't supported yet! Valid platforms are %s" % (self.PLATFORM, self.PLATFORMS_VALID))
except ValueError:
print("Malformed input not added: %s" % str(parsed_line))
continue # just skip malformed input
student_records[platform_id] = {
'name': name, 'gt_id': gt_id}
gt_id_dict[gt_id] = platform_id
except IOError:
raise IOError(
"%s: Missing file '%s'. Exiting." % (
inspect.currentframe().f_code.co_name, input_filename))
with open(self.STUDENT_RECORDS_FILENAME, 'w') as output_file:
json.dump(student_records, output_file)
with open(self.STUDENT_ALIAS_FILENAME, 'w') as alias_file:
json.dump(gt_id_dict, alias_file)
def create_team_json(self, input_filename):
r"""
Create the JSON files required for processing team submissions.
:param input_filename: filename that will have all required information parsed out of it. Requires format "<GTID>\t<Grader>\t\<Team#>"; the <Grader> section is unused, but left in so this information can be copied directly from the gradebook.
:return: None
"""
try:
with open(input_filename, 'r') as input_file:
students = {} # what team is a student in?
teams = {} # what students are in a team?
for line in input_file:
line = line.strip()
parsed = line.split('\t')
GT_username = parsed[0]
try:
team = parsed[2]
except IndexError:
team = "None"
students[GT_username] = team # store student's team
if team not in teams:
teams[team] = []
teams[team].append(GT_username) # put student on list of team members
# save here
with open(self.TEAM_RECORDS_FILENAME, 'w') as student_teams_file:
json.dump(students, student_teams_file)
with open(self.TEAM_MEMBERS_FILENAME, 'w') as team_members_file:
json.dump(teams, team_members_file)
except IOError:
raise IOError("create_team_json couldn\'t find file with name %s" % input_filename)
def _get_group_submission_platform_id(self, submission_folder_name, group, team_members, student_aliases):
r"""
Finds the platform ID for a valid group submission, if one exists. If none exists, returns -1.
:param submission_folder_name: name of the folder where submissions are kept, like ./submissions/T_DO
:param group: group name - like TeamXX
:param team_members: dictionary containing the names of the members of a group
:param student_aliases: dictionary indexed by gt_username that stores platform IDs
:return: valid platform ID for group submission
"""
base_directory = self._get_submission_folder(submission_folder_name, group)
platform_id = "-1"
for temp_student_name in team_members[group]:
try:
student_platform_id = student_aliases[temp_student_name]
except KeyError:
continue # student dropped
filename_candidate = os.path.join(base_directory, self._get_submission_file_name(group.lower(), student_platform_id))
try:
with open(filename_candidate, 'r'):
platform_id = student_platform_id # found it!
break
except IOError:
try:
# try late submission
filename_candidate = os.path.join(base_directory, self._get_submission_file_name(group.lower(), student_platform_id, True))
with open(filename_candidate, 'r'):
platform_id = student_platform_id
except IOError:
pass
if platform_id == "-1": # not fatal so other submissions can be processed.
print("No valid filename found for %s. Tried students %s: " % (group, team_members[group]))
return platform_id
def _get_correct_reference_id(self, graded_id):
r"""
Depending on which submission type, converts it to the correct ID
instance so we can access the appropriate repo.
For non-team projects, the ID is the correct student ID.
For team projects, we convert said student into the correct team ID>
Arguments:
graded_id: (str) The ID we will convert depending on the mode.
Return:
The corrected ID.
"""
if self.is_team and graded_id.find("Team") != 0:
team_records = self._get_file_dict(
filename=self.TEAM_RECORDS_FILENAME,
caller_name=inspect.currentframe().f_code.co_name)
try:
team_id = team_records[graded_id]
except IndexError:
raise IndexError(
"%s: Couldn't find team for student with GTID '%s'. Exiting."
% (inspect.currentframe().f_code.co_name, graded_id))
except KeyError:
logger.error("%s: Couldn't find team for student with GTID '%s'. Exiting.\n", inspect.currentframe().f_code.co_name, graded_id)
team_id = None
return team_id
else:
# This is the student ID
return graded_id
def _get_file_dict(self, filename, caller_name='', epilog=''):
r"""
Attempts to access the file and retrieve the JSON within it.
Arguments:
filename: (str) The name of the file we will open.
caller_name: (str) This is the caller's function name when
printing errors.
epilog: (str) This is the epilogue error message if one is needed.
NOTE:
For Python, JSON and the native Python dictionary are one and the
same as they have matching calls and very similar syntax.
Returns:
The associated JSON (Python Dict) at the file or an IOError.
"""
file_dict = self.cached_file_dicts.get(filename, None)
if file_dict is None:
try:
with open(filename, 'r') as my_file:
file_dict = self.cached_file_dicts[filename] = json.load(my_file)
except IOError:
raise IOError(
"%s: Missing file '%s'%s Exiting." % (
caller_name, filename, epilog))
return file_dict
def _get_student_folders(self, submission_folder_name, student_whitelist, assignment_code):
r"""
Get a list of student repos that we will grade.
If student_whitelist is not set, we will grab all student repos.
Arguments:
submission_folder_name: (str) This is the directory for all
submissions that we will download. This must exist and will throw
an IOError if it does not exist.
student_whitelist: (list of str) This is the list of student
username IDs that we will whitelist. That is to say all students
in the list will not be ignored. If set to None or empty list,
we will grab all students.
Return:
A list of all student submissions we will process.
"""
if not student_whitelist:
return list(filter(
os.path.isdir, os.listdir(submission_folder_name)))
if self.is_team:
team_records = self._get_file_dict(
filename=self.TEAM_MEMBERS_FILENAME,
caller_name=inspect.currentframe().f_code.co_name)
if not self._should_process_team_submissions(assignment_code):
# T-Square doesn't process groups - it requires individual student submissions. Convert Team list to Student list here.
# Read data in student_whitelist
student_whitelist_multi_list = [team_records[team] for team in student_whitelist]
# Flatten multi list to be a single list and store it back
student_whitelist = list(
itertools.chain.from_iterable(student_whitelist_multi_list))
# student_whitelist now contains student GTIDs instead of team names
student_aliases = self._get_file_dict(
filename=self.STUDENT_ALIAS_FILENAME,
caller_name=inspect.currentframe().f_code.co_name)
student_records = self._get_file_dict(
filename=self.STUDENT_RECORDS_FILENAME,
caller_name=inspect.currentframe().f_code.co_name,
epilog="Run _create_student_json first.")
folders = []
if self._should_process_team_submissions(assignment_code):
folders = student_whitelist
else:
for student in student_whitelist:
try:
platform_id = student_aliases[student]
name = student_records[platform_id]['name']
except KeyError:
error_message = "%s not found in %s - check the gradebook to see if they dropped or added late. If they dropped, remove from your grading list. If they added late, you may need to update %s - raise this issue with the TA group." % (
student, self.STUDENT_ALIAS_FILENAME, self.STUDENT_ALIAS_FILENAME)
if self.is_team:
print(error_message) # dropped students shouldn't be fatal in team grading
continue # don't append student to folders
else:
continue
except IndexError:
logger.error(
"Couldn't get folder name for student with GTID %s\n",
student)
folders.append('%s(%s)' % (name, platform_id))
return folders
def _gen_prefixed_dir(self, prefix_str):
r"""
Combines a directory prefix into the valid directory, to target a
student's directory.
Arguments:
prefix_str: (str) A valid student's prefix (be it a team number
or a student name) so we can access it.
Returns:
A valid directory that can be accessed.
"""
return os.path.join(self.MAIN_REPO_DIR, "%s%s" %
(self.FOLDER_PREFIX, prefix_str))
def _check_commitID(self, current_assignment,
assignment_code, gt_username):
r"""
Checks if the current commit is a valid comment in the Repo.
Some students may submit commits that are invalid.
The result is stored in current_assignment.
Arguments:
current_assignment: (dict) This is the current assignment we are
checking the commit of.
assignment_code: (str) This is the two letter name for the
assignment.
gt_username: (str) student GT username - this is what he or she would log into GitHub with
the info of.
"""
repo_suffix = self._get_correct_reference_id(graded_id=gt_username)
command = (
'cd %s; git checkout %s; git tag -f %s &> /dev/null;'
'git show --pretty=format:\'%%H\' --no-patch; '
'cd - &> /dev/null' % (
self._gen_prefixed_dir(prefix_str=repo_suffix),
current_assignment['commitID'], assignment_code))
output_checkout = self._execute_command(command=command)
if self.OS_TYPE == 'Windows':
# Windows returns \\ prefix and suffix so strip it
commit = output_checkout[1:-1]
else:
commit = str(output_checkout).split('/')[0] # may have suffix /<path>
valid_commit = commit.find(current_assignment['commitID']) != -1 # -1 means no substring found, anything else means find found it
current_assignment['commitID valid'] = valid_commit
def _get_submission_folder(self, submission_folder_name, folder):
r"""
Gets the folder student submissions where student submission info can be found
:param submission_folder_name: root folder for all submissions for this assignment
:param folder: student folder
:return:
"""
if self.PLATFORM == "TSQUARE":
folder_name = os.path.join(submission_folder_name, folder)
elif self.PLATFORM == "CANVAS":
folder_name = submission_folder_name
else:
raise ValueError("_get_submission_folder does not handle platform %!" % self.PLATFORM)
return folder_name
# TODO: current_student gets replaced with student_name (str, rather than object)
def _get_submission_file_name(self, student_name, platform_id, late=False):
r"""
Get the name of the file to pull submission text from.
:param student_name: GT ID of student (string)
:param platform_id: student identifier (differs per platform)
:return:
"""
if self.PLATFORM == "TSQUARE":
current_submission_file = (
'%s(%s)_submissionText.html' % (
student_name, platform_id))
elif self.PLATFORM == "CANVAS":
name = student_name.replace(",", "").replace(" ", "").replace("-", "").replace(".", "").replace("'", "").lower()
label = ""
if late:
label = "late_"
current_submission_file = "%s_%s%s_text.html" % (name, label, platform_id)
else:
raise ValueError("_get_submission_file_name does not handle platform %!" % self.PLATFORM)
return current_submission_file
def _check_submission_file(self, current_assignment,