You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

73 regels
1.4 KiB

  1. #!/usr/bin/python
  2. import cgi
  3. import datetime
  4. import os
  5. try:
  6. form = cgi.FieldStorage()
  7. out = ''
  8. out_buf, str_out = '', ''
  9. # Traceback
  10. # ---------
  11. def getTraceback():
  12. import sys, traceback, string
  13. type, value, tb = sys.exc_info()
  14. body = "Traceback (innermost last):\n"
  15. list = traceback.format_tb(tb, None) \
  16. + traceback.format_exception_only(type, value)
  17. body = body + "%-20s %s" % (string.join(list[:-1], ""), list[-1])
  18. return body
  19. def load_js_file():
  20. global out
  21. filename = form.getvalue('filename')
  22. import os
  23. try:
  24. f = open(os.path.join('../js/', filename))
  25. try:
  26. out = f.read()
  27. finally:
  28. f.close()
  29. except IOError,e:
  30. out = "Not Found: %s" % filename
  31. def compress_string(buf):
  32. import gzip, cStringIO
  33. zbuf = cStringIO.StringIO()
  34. zfile = gzip.GzipFile(mode = 'wb', fileobj = zbuf, compresslevel = 5)
  35. zfile.write(buf)
  36. zfile.close()
  37. return zbuf.getvalue()
  38. compress = 0
  39. try:
  40. if string.find(os.environ["HTTP_ACCEPT_ENCODING"], "gzip") != -1:
  41. compress = 1
  42. except:
  43. pass
  44. load_js_file()
  45. if compress and len(out)>512:
  46. out_buf = compress_string(str_out)
  47. print "Content-Encoding: gzip"
  48. print "Content-Length: %d" % (len(out_buf))
  49. print "Content-Type: text/javascript"
  50. # Headers end
  51. print
  52. if out_buf:
  53. sys.stdout.write(out_buf)
  54. elif out:
  55. print out
  56. except Exception, e:
  57. print "Content-Type: text/javascript"
  58. print
  59. print getTraceback().replace('\n','<br>')