-
Notifications
You must be signed in to change notification settings - Fork 5
/
submit.py
executable file
·55 lines (40 loc) · 1.37 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
#!/usr/bin/python
import os
import sys
import base64
import json
import urllib,urllib2
TOKEN_FILE = 'submit.token'
SUBMIT_URL = 'https://6858submit.csail.mit.edu/capi/submit'
def submit(filename):
if not os.path.exists(TOKEN_FILE):
print "Please get a valid token from the submission website and enter it here"
token = raw_input("token: ").strip()
with open(TOKEN_FILE,'w') as f:
f.write(token);
with open(TOKEN_FILE) as f:
token = f.read()
doc = prepare_data(filename,token)
print "using token: {0}".format(token)
try:
urllib2.urlopen(SUBMIT_URL,json.dumps(doc))
print 'File {0} submitted'.format(filename)
print 'please visit the submission website to verify your submission.'
except urllib2.HTTPError as e:
print "Error submitting file: {0}".format(e)
print "Maybe your API token has changed or the file is not a tarball?"
print "Please delete the file 'submit.token' and try again."
def prepare_data(filename,token):
with open(filename) as f:
filedata = f.read()
doc = {
'filename':filename,
'api-token':token,
'data': base64.b64encode(filedata),
}
return doc
if __name__ == '__main__':
if len(sys.argv) != 2:
print "Usage {0} labx-handin.tar.gz".format(sys.argv[0])
sys.exit(1)
submit(sys.argv[1])