File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ #!/usr/bin/python
2+
3+ import cgi
4+ import doctest
5+ import json
6+ import sys
7+
8+ form = cgi .FieldStorage ()
9+ prob_name = form ['problem_name' ].value
10+ assert type (prob_name ) is str # prevent subclassing shenanigans
11+ # whitelist
12+ assert prob_name in ('python_comprehension-1' ,)
13+
14+ fn = 'matrix-demo/' + prob_name + '.py'
15+ cod = open (fn ).read ()
16+
17+ p = doctest .DocTestParser ()
18+ examples = p .get_examples (cod )
19+ if len (examples ):
20+ first_ex = examples [0 ]
21+ testCod = 'result = ' + first_ex .source
22+
23+ print ("Content-type: text/plain; charset=iso-8859-1\n " )
24+ print (json .dumps (dict (code = cod , test = testCod )))
Original file line number Diff line number Diff line change 1+ #!/usr/bin/python
2+
3+ import cgi
4+ import doctest
5+ import json
6+ import urllib
7+ import urllib2
8+
9+ form = cgi .FieldStorage ()
10+ user_code = form ['submitted_code' ].value
11+ prob_name = form ['problem_name' ].value
12+ #assert type(prob_name) is str
13+
14+ # whitelist
15+ assert prob_name in ('python_comprehension-1' ,)
16+
17+ test_fn = 'matrix-demo/' + prob_name + '.test.py'
18+ test_cod = open (test_fn ).read ()
19+
20+ # concatenate!
21+ script = test_cod + '\n ' + user_code + \
22+ '''
23+ import doctest
24+ (n_fail, n_tests) = doctest.testmod(verbose=False)
25+ if n_fail == 0:
26+ print("All %d tests passed!" % n_tests)
27+ '''
28+
29+ # run on the EC2 sandbox
30+ url = 'http://ec2-107-20-94-197.compute-1.amazonaws.com/cgi-bin/run_code.py'
31+ values = {'user_script' : script }
32+
33+ data = urllib .urlencode (values )
34+ req = urllib2 .Request (url , data )
35+ response = urllib2 .urlopen (req )
36+ the_page = response .read ()
37+
38+ print ("Content-type: text/plain; charset=iso-8859-1\n " )
39+ print (the_page )
You can’t perform that action at this time.
0 commit comments