showard | 9484c31 | 2009-01-07 21:07:28 +0000 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | |
showard | 2df3c69 | 2009-09-11 18:41:07 +0000 | [diff] [blame] | 3 | import cgi, traceback, urllib2 |
showard | ef6fe02 | 2009-03-27 20:55:16 +0000 | [diff] [blame] | 4 | import common |
| 5 | from autotest_lib.frontend.afe.json_rpc import serviceHandler |
showard | 9484c31 | 2009-01-07 21:07:28 +0000 | [diff] [blame] | 6 | |
| 7 | script = """\ |
| 8 | Content-Type: text/javascript |
| 9 | |
| 10 | %(callback)s(%(result)s); |
| 11 | """ |
| 12 | |
showard | ef6fe02 | 2009-03-27 20:55:16 +0000 | [diff] [blame] | 13 | class LogFileNotFound(Exception): |
| 14 | pass |
| 15 | |
| 16 | form = cgi.FieldStorage(keep_blank_values=True) |
| 17 | encoded_request = form['request'].value |
showard | 9484c31 | 2009-01-07 21:07:28 +0000 | [diff] [blame] | 18 | callback = form['callback'].value |
| 19 | |
showard | ef6fe02 | 2009-03-27 20:55:16 +0000 | [diff] [blame] | 20 | request = serviceHandler.ServiceHandler.translateRequest(encoded_request) |
| 21 | parameters = request['params'][0] |
| 22 | path = parameters['path'] |
| 23 | |
showard | 2df3c69 | 2009-09-11 18:41:07 +0000 | [diff] [blame] | 24 | result_dict = serviceHandler.ServiceHandler.blank_result_dict() |
showard | 9484c31 | 2009-01-07 21:07:28 +0000 | [diff] [blame] | 25 | try: |
| 26 | file_contents = urllib2.urlopen('http://localhost' + path).read() |
showard | 2df3c69 | 2009-09-11 18:41:07 +0000 | [diff] [blame] | 27 | result_dict['result'] = file_contents |
showard | 9484c31 | 2009-01-07 21:07:28 +0000 | [diff] [blame] | 28 | except urllib2.HTTPError: |
showard | 2df3c69 | 2009-09-11 18:41:07 +0000 | [diff] [blame] | 29 | result_dict['err'] = LogFileNotFound('%s not found' % path) |
| 30 | result_dict['err_traceback'] = traceback.format_exc() |
showard | 9484c31 | 2009-01-07 21:07:28 +0000 | [diff] [blame] | 31 | |
showard | 2df3c69 | 2009-09-11 18:41:07 +0000 | [diff] [blame] | 32 | encoded_result = serviceHandler.ServiceHandler.translateResult(result_dict) |
showard | ef6fe02 | 2009-03-27 20:55:16 +0000 | [diff] [blame] | 33 | print script % dict(callback=callback, result=encoded_result) |