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.

get_module_js.cgi 2.2 KiB

14 年之前
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. jsdir='../js'
  10. jsonout= {}
  11. # Traceback
  12. # ---------
  13. def getTraceback():
  14. import sys, traceback, string
  15. type, value, tb = sys.exc_info()
  16. body = "Traceback (innermost last):\n"
  17. list = traceback.format_tb(tb, None) \
  18. + traceback.format_exception_only(type, value)
  19. body = body + "%-20s %s" % (string.join(list[:-1], ""), list[-1])
  20. return body
  21. def load_js_from_file(module_name):
  22. global out
  23. global jsonout
  24. import webnotes.utils.jsnamespace as jsn
  25. filename = jsn.jsNamespace.modname_to_filename(module_name,jsdir)
  26. import os
  27. try:
  28. f = open(os.path.join(filename))
  29. try:
  30. out = f.read()
  31. finally:
  32. f.close()
  33. except IOError,e:
  34. out = "Not Found: %s" % filename
  35. jsonout[module_name]=out
  36. def load_js_module(module_name):
  37. global jsonout
  38. from webnotes import defs
  39. devmode = getattr(defs,'developer_mode')
  40. if devmode:
  41. import compilejs
  42. compilejs.wnJSCompiler.compilejs(jsdir)
  43. if module_name not in jsonout:
  44. dependent_mods = get_dependencies(module_name)
  45. for module in dependent_mods:
  46. load_js_from_file(module)
  47. load_js_from_file(module_name)
  48. def get_dependencies(module_name):
  49. import webnotes.utils.jsdependency as jsd
  50. ret = jsd.jsDependencyBuilder.build_dependency(jsdir,module_name)
  51. return ret
  52. def compress_string(buf):
  53. import gzip, cStringIO
  54. zbuf = cStringIO.StringIO()
  55. zfile = gzip.GzipFile(mode = 'wb', fileobj = zbuf, compresslevel = 5)
  56. zfile.write(buf)
  57. zfile.close()
  58. return zbuf.getvalue()
  59. compress = 0
  60. try:
  61. if string.find(os.environ["HTTP_ACCEPT_ENCODING"], "gzip") != -1:
  62. compress = 1
  63. except:
  64. pass
  65. load_js_module(form.getvalue('module'))
  66. #load_js_module('wn.modules')
  67. if compress and len(out)>512:
  68. out_buf = compress_string(str_out)
  69. print "Content-Encoding: gzip"
  70. print "Content-Length: %d" % (len(out_buf))
  71. print "Content-Type: text/javascript"
  72. # Headers end
  73. print
  74. if out_buf:
  75. sys.stdout.write(out_buf)
  76. elif out:
  77. import json
  78. print json.dumps(jsonout)
  79. except Exception, e:
  80. print "Content-Type: text/javascript"
  81. print
  82. print getTraceback()#.replace('\n','<br>')