-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcpUploadFilesInDirectory.py
More file actions
executable file
·96 lines (78 loc) · 3.73 KB
/
cpUploadFilesInDirectory.py
File metadata and controls
executable file
·96 lines (78 loc) · 3.73 KB
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
#!/usr/bin/env python3
# ----------------------------------------------------------------------
# cpUploadFilesInDirectory.py
# Dave Reed
# 02/14/2020
# ----------------------------------------------------------------------
from argparse import ArgumentParser
from CPAPI import *
from FileUtils import *
# ----------------------------------------------------------------------
def main():
parser = ArgumentParser(description='upload specified files to codepost.io')
parser.add_argument('--course-prefix', dest='coursePrefix', default='CS',
help='''directory prefix for course names (i.e., if all your codepost.io course names and
local directories start with CS such as CS160 then use the default
''')
parser.add_argument('-c', '--course-name', dest='course', default=None,
help='''name of course, if no name supplied, will try to find directory with coursePrefix in
the current working directory's parent directories
''')
parser.add_argument('-a', '--assignment-name', dest='assignment', default=None,
help='''name of assignment, if no name supplied will try to find directory with coursePrefix
and use directory after it as the assignment name
''')
parser.add_argument('--rename', dest='rename', action='store_true',
help='''rename files so arguments are: file1 renamedFile1 file2 renamedFile2''')
parser.add_argument('--overwrite', dest='overwrite', action='store_true',
help='''overwrite files if already exist''')
parser.add_argument("files", nargs='+', default=None)
options = parser.parse_args()
if options.course is None:
course, _, _ = FileInfo.infoForFilePath(os.getcwd())
else:
course = options.course
if options.assignment is None:
_, assignment, _ = FileInfo.infoForFilePath(os.getcwd())
else:
assignment = options.assignment
files = options.files
CP.init()
cpCourse = CP.course(course)
cpAssignment = cpCourse.assignment(assignment)
cwd = os.getcwd()
_, _, studentEmail = FileInfo.infoForFilePath(cwd)
if "@" not in studentEmail:
print(f"{studentEmail} does not appear to be a student directory as no @ sign")
return
print(course, assignment, studentEmail)
# get files in the student directory
studentDirectory = DirectoryInfo(cwd)
studentFiles = studentDirectory.files()
# if we have some files
if len(studentFiles) != 0:
# get the submission
submission = cpAssignment.submissionForStudent(studentEmail)
if submission is None:
submission = cpAssignment.makeSubmissionForStudent(studentEmail)
if options.rename:
files = tuple(zip(*(iter(files),) * 2))
for f, renamedF in files:
info = FileInfo(cwd, f)
if info.filePath() in studentFiles:
text = info.contentsOf()
if text != "":
print(f"upload {info.filePath()} as {renamedF}")
submission.uploadFile(renamedF, text, overwrite=options.overwrite)
else:
for f in files:
info = FileInfo(cwd, f)
if info.filePath() in studentFiles:
text = info.contentsOf()
if text != "":
print(f"upload {info.filePath()}")
submission.uploadFile(f, text, overwrite=options.overwrite)
print()
# ----------------------------------------------------------------------
if __name__ == '__main__':
main()