-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubmit.py
executable file
·288 lines (241 loc) · 9.92 KB
/
submit.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
import os
import urllib
import urllib2
import hashlib
import random
import email
import email.message
import email.encoders
import StringIO
import sys
import json
import subprocess
import getpass
from tempfile import NamedTemporaryFile
from time import time
from subprocess import Popen
from subprocess import PIPE
import datetime
class NullDevice:
def write(self, s):
pass
def submit(partId):
print '==\n== [inforetrieval] Submitting Solutions | Programming Exercise %s\n=='% homework_id()
if(not partId):
partId = promptPart()
partNames = validParts()
if not isValidPartId(partId):
print '!! Invalid homework part selected.'
print '!! Expected an integer from 0 to %d.' % (len(partNames) + 1)
print '!! Submission Cancelled'
return
(login, password) = loginPrompt()
if not login:
print '!! Submission Cancelled'
return
print '\n== Connecting to coursera ... '
# Setup submit list
if partId == len(partNames):
submitParts = range(0, len(partNames))
else:
submitParts = [partId]
print submitParts
for partId in submitParts:
# Get Challenge
(login, ch, state, ch_aux) = getChallenge(login, partId)
if((not login) or (not ch) or (not state)):
# Some error occured, error string in first return element.
print '\n!! Error: %s\n' % login
return
# Get source files
src = source(partId)
# Attempt Submission with Challenge
ch_resp = challengeResponse(login, password, ch)
(result, string) = submitSolution(login, ch_resp, partId, output(partId, ch_aux), \
src, state, ch_aux)
print '== [inforetrieval] Submitted Homework %s - Part %d - %s' % \
(homework_id(), partId, partNames[partId])
print '== %s' % string.strip()
if (string.strip() == 'Exception: We could not verify your username / password, please try again. (Note that your password is case-sensitive.)'):
print '== The password is not your login, but a 10 character alphanumeric string displayed on the top of the Assignments page'
## This collects the source code (just for logging purposes)
def source(partId):
if partId == 0:
if not os.path.exists("report.pdf"):
print "No report.pdf file found in the directory. Please make sure it exists (and make sure it's all lowercase letters)."
sys.exit(1);
p = open("report.pdf","rb").read().encode("base64")
return p
return ""
def promptPart():
"""Prompt the user for which part to submit."""
print('== Select which part(s) to submit for assignment ' + homework_id())
partNames = validParts()
for i in range(0, len(partNames)):
print '== %d) %s' % (i, partNames[i])
print '== %d) All of the above \n==\n ' % (len(partNames))
selPart = raw_input('Enter your choice [0-{0}]: '.format(len(partNames)))
partId = int(selPart)
if not isValidPartId(partId):
partId = -1
return partId
def isValidPartId(partId):
"""Returns true if partId references a valid part."""
partNames = validParts()
return ((partId >= 0) and (partId <= len(partNames) + 1))
# =========================== LOGIN HELPERS ===========================
def loginPrompt():
"""Prompt the user for login credentials. Returns a tuple (login, password)."""
(login, password) = basicPrompt()
return login, password
def basicPrompt():
"""Prompt the user for login credentials. Returns a tuple (login, password)."""
login = raw_input('Login (Email address): ')
password = raw_input('Password: ')
return login, password
def homework_id():
"""Returns the string homework id."""
return '4'
def getChallenge(email, partId):
"""Gets the challenge salt from the server. Returns (email,ch,state,ch_aux)."""
url = challenge_url()
values = {'email_address' : email, 'assignment_part_sid' : "%s-%s" % (homework_id(), partId), 'response_encoding' : 'delim'}
# values = {'email_address' : email, 'assignment_part_sid' : "%s-%s-dev" % (homework_id(), partId), 'response_encoding' : 'delim'}
data = urllib.urlencode(values)
req = urllib2.Request(url, data)
response = urllib2.urlopen(req)
text = response.read().strip()
# print text
# print values
# sys.exit(1)
# text is of the form email|ch|signature
splits = text.split('|')
if(len(splits) != 9):
print 'Badly formatted challenge response: %s' % text
return None
return (splits[2], splits[4], splits[6], splits[8])
def challengeResponse(email, passwd, challenge):
sha1 = hashlib.sha1()
sha1.update("".join([challenge, passwd])) # hash the first elements
digest = sha1.hexdigest()
strAnswer = ''
for i in range(0, len(digest)):
strAnswer = strAnswer + digest[i]
return strAnswer
def challenge_url():
"""Returns the challenge url."""
return "https://stanford.coursera.org/" + URL + "/assignment/challenge"
#return "https://stanford.coursera.org/inforetrieval/assignment/challenge"
#return "https://stanford.coursera.org/inforetrieval-staging/assignment/challenge"
def submit_url():
"""Returns the submission url."""
return "https://stanford.coursera.org/" + URL + "/assignment/submit"
return "https://stanford.coursera.org/inforetrieval/assignment/submit"
#return "https://stanford.coursera.org/inforetrieval-staging/assignment/submit"
def submitSolution(email_address, ch_resp, part, output, source, state, ch_aux):
"""Submits a solution to the server. Returns (result, string)."""
#source_64_msg = email.message.Message()
#source_64_msg.set_payload(source)
#email.encoders.encode_base64(source_64_msg)
output_64_msg = email.message.Message()
output_64_msg.set_payload(output)
email.encoders.encode_base64(output_64_msg)
values = { 'assignment_part_sid' : ("%s-%s" % (homework_id(), part)), \
# values = { 'assignment_part_sid' : ("%s-%s-dev" % (homework_id(), part)), \
'email_address' : email_address, \
'submission' : output_64_msg.get_payload(), \
'submission_aux' : source, \
'challenge_response' : ch_resp, \
'state' : state \
}
url = submit_url()
inp = raw_input('Do you want to actually submit this (yes|y)?: ') #CHANGELIVE
inp = inp.strip().lower()
if inp != 'y' and inp != 'yes':
print '== Fine, aborting'
sys.exit(0) #CHANGELIVE
data = urllib.urlencode(values)
req = urllib2.Request(url, data)
response = urllib2.urlopen(req)
string = response.read().strip()
# print "STRING: " + string
# TODO parse string for success / failure
result = 0
return result, string
############ BEGIN ASSIGNMENT SPECIFIC CODE ##############
# For example, if your URL is https://class.coursera.org/pgm-2012-001-staging, set it to "pgm-2012-001-staging".
URL = 'cs276-002'
def validParts():
"""Returns a list of valid part names."""
partNames = ['Report', \
'Task 1 (Linear Regression)', \
'Task 2 (Ranking SVM)', \
'Task 3 (More Features)', \
'Task 4 (Extra Credit)']
return partNames
# Thang: factor code into a new function to run different tasks
def run_task(task_id, test_file, test_output_length):
start = time()
child = Popen(['./l2r.sh', 'data/pa4.signal.train', 'data/pa4.rel.train', test_file, task_id], stdout=PIPE, stderr=PIPE, shell=False);
(res, err) = child.communicate("")
# print res
elapsed = time() - start
guesses = res.splitlines()
print err
if (len(guesses) != test_output_length):
print 'Warning. The number of url-document pairs ' + str(len(guesses)) + ' is not correct. Please ensure that the output is formatted properly.'
print guesses
return (res, elapsed)
def output(partId, ch_aux):
"""Uses the student code to compute the output for test cases."""
if not os.path.exists("people.txt"):
print "There is no people.txt file in this directory. Please make people.txt file in this directory with your and your partner's SUNet ID in separate lines (do NOT include @stanford.edu)"
sys.exit(1)
people = open("people.txt").read().splitlines();
if len(people) == 0:
print "people.txt is empty! Write the SUNet ids of you and your partner, if any, in separate lines.."
sys.exit(1);
for x in people:
if len(x) < 3 or len(x) > 8 or ' ' in x.strip() == True:
print "The SUNet IDs don't seem to be correct. Make sure to remove empty lines. They are supposed to be between 3 and 8 characters. Also, make sure to not include @stanford.edu."
sys.exit(1);
peopleStr = "_".join(str(x.strip()) for x in people if x)
elapsed= 0.0
dev_elapsed = 0.0
results = []
dev_results = []
if partId == 0:
print 'Submitting the report'
elif partId <= 4 and partId>0:
print '== Make sure data/pa4.signal.(train|dev) and data/pa4.rel.(train|dev) are in the current working directory'
print '== Running your code ...'
print '== Your code should output results (and nothing else) to stdout'
print '## Calling ./l2r.sh for Task', str(partId), '(this might take a while)'
# dev
dev_file = 'data/pa4.signal.dev'
print '# Running on development test data ...'
dev_output_length = 1066
(dev_results, dev_elapsed) = run_task(str(partId), dev_file, dev_output_length)
# test
print '# Running on test data ...'
test_file = NamedTemporaryFile(delete=False)
test_file.write(ch_aux)
test_file.close()
test_file = test_file.name
test_output_length = 1065
(results, elapsed) = run_task(str(partId), test_file, test_output_length)
else:
print '[WARNING]\t[output]\tunknown partId: %s' % partId
sys.exit(1)
print '== Finished running your code'
# print dev_results
# print results
return json.dumps( { 'dev_result': dev_results, 'dev_time': dev_elapsed, 'result': results, 'time': elapsed, 'timesubmitted': str(datetime.datetime.now()), 'USERIDS': peopleStr } )
def test_python_version():
"""docstring for test_python_version"""
if sys.version_info < (2,6):
print >> sys.stderr, "Your python version is too old, please use >= 2.6"
sys.exit(1)
if __name__ == '__main__':
test_python_version()
submit(0)