|
| 1 | +# Lightweight OPT server that works on both Python 2 and 3 |
| 2 | + |
| 3 | +# to invoke, run 'python bottle_server.py' |
| 4 | +# and visit http://localhost:8080/index.html |
| 5 | +# |
| 6 | +# external dependencies: bottle |
| 7 | +# |
| 8 | +# easy_install pip |
| 9 | +# pip install bottle |
| 10 | + |
| 11 | +# From an OPT user: A couple notes: to get bottle_server.py running, |
| 12 | +# I had to replace cStringIO with io and urllib2 with urllib, for |
| 13 | +# compatibility from 2.x to 3.x Ii was running from /v3/). |
| 14 | + |
| 15 | +from bottle import route, get, request, run, template, static_file |
| 16 | +import StringIO # NB: don't use cStringIO since it doesn't support unicode!!! |
| 17 | +import json |
| 18 | +import pg_logger |
| 19 | +import urllib |
| 20 | +import urllib2 |
| 21 | + |
| 22 | +# dummy routes for testing only |
| 23 | +@route('/web_exec_<name:re:.+>.py') |
| 24 | +def web_exec(name): |
| 25 | + return 'OK' |
| 26 | + |
| 27 | +@route('/LIVE_exec_<name:re:.+>.py') |
| 28 | +def live_exec(name): |
| 29 | + return 'OK' |
| 30 | + |
| 31 | + |
| 32 | +@route('/<filepath:path>') |
| 33 | +def index(filepath): |
| 34 | + # special-case for testing name_lookup.py ... |
| 35 | + if 'name_lookup.py' in filepath: |
| 36 | + return json.dumps(dict(name='TEST NAME', email='TEST EMAIL')) |
| 37 | + return static_file(filepath, root='.') |
| 38 | + |
| 39 | +@get('/exec') |
| 40 | +def get_exec(): |
| 41 | + out_s = StringIO.StringIO() |
| 42 | + |
| 43 | + def json_finalizer(input_code, output_trace): |
| 44 | + ret = dict(code=input_code, trace=output_trace) |
| 45 | + json_output = json.dumps(ret, indent=None) |
| 46 | + out_s.write(json_output) |
| 47 | + |
| 48 | + options = json.loads(request.query.options_json) |
| 49 | + |
| 50 | + pg_logger.exec_script_str_local(request.query.user_script, |
| 51 | + request.query.raw_input_json, |
| 52 | + options['cumulative_mode'], |
| 53 | + options['heap_primitives'], |
| 54 | + json_finalizer) |
| 55 | + |
| 56 | + return out_s.getvalue() |
| 57 | + |
| 58 | + |
| 59 | +@get('/load_matrix_problem.py') |
| 60 | +def load_matrix_problem(): |
| 61 | + prob_name = request.query.problem_name |
| 62 | + assert type(prob_name) in (str, unicode) |
| 63 | + |
| 64 | + # whitelist |
| 65 | + assert prob_name in ('python_comprehension-1',) |
| 66 | + |
| 67 | + fn = 'matrix-demo/' + prob_name + '.py' |
| 68 | + f = open(fn) |
| 69 | + cod = f.read() |
| 70 | + f.close() |
| 71 | + |
| 72 | + import doctest |
| 73 | + import sys |
| 74 | + p = doctest.DocTestParser() |
| 75 | + examples = p.get_examples(cod) |
| 76 | + if len(examples): |
| 77 | + first_ex = examples[0] |
| 78 | + #print >> sys.stderr, 'Source:', `first_ex.source` |
| 79 | + testCod = 'result = ' + first_ex.source |
| 80 | + |
| 81 | + return json.dumps(dict(code=cod, test=testCod)) |
| 82 | + |
| 83 | + |
| 84 | +@get('/submit_matrix_problem.py') |
| 85 | +def submit_matrix_problem(): |
| 86 | + user_code = request.query.submitted_code |
| 87 | + prob_name = request.query.problem_name |
| 88 | + assert type(prob_name) in (str, unicode) |
| 89 | + |
| 90 | + # whitelist |
| 91 | + assert prob_name in ('python_comprehension-1',) |
| 92 | + |
| 93 | + test_fn = 'matrix-demo/' + prob_name + '.test.py' |
| 94 | + test_cod = open(test_fn).read() |
| 95 | + |
| 96 | + # concatenate! |
| 97 | + script = test_cod + '\n' + user_code + \ |
| 98 | +''' |
| 99 | +import doctest |
| 100 | +(n_fail, n_tests) = doctest.testmod(verbose=False) |
| 101 | +if n_fail == 0: |
| 102 | + print("All %d tests passed!" % n_tests) |
| 103 | +''' |
| 104 | + |
| 105 | + url = 'http://ec2-107-20-94-197.compute-1.amazonaws.com/cgi-bin/run_code.py' |
| 106 | + values = {'user_script' : script} |
| 107 | + |
| 108 | + data = urllib.urlencode(values) |
| 109 | + req = urllib2.Request(url, data) |
| 110 | + response = urllib2.urlopen(req) |
| 111 | + the_page = response.read() |
| 112 | + return the_page |
| 113 | + |
| 114 | + |
| 115 | +if __name__ == "__main__": |
| 116 | + #run(host='localhost', port=8080, reloader=True) |
| 117 | + run(host='0.0.0.0', port=8003, reloader=True) # make it externally visible - DANGER this is very insecure since there's no sandboxing! |
0 commit comments