build.py 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #!/usr/bin/env python
  2. import sys
  3. sys.path.append("../tools")
  4. import mergejs
  5. import optparse
  6. def build(config_file = None, output_file = None, options = None):
  7. have_compressor = []
  8. try:
  9. import jsmin
  10. have_compressor.append("jsmin")
  11. except ImportError:
  12. print "No jsmin"
  13. try:
  14. import closure
  15. have_compressor.append("closure")
  16. except Exception, E:
  17. print "No closure (%s)" % E
  18. try:
  19. import closure_ws
  20. have_compressor.append("closure_ws")
  21. except ImportError:
  22. print "No closure_ws"
  23. try:
  24. import minimize
  25. have_compressor.append("minimize")
  26. except ImportError:
  27. print "No minimize"
  28. use_compressor = None
  29. if options.compressor and options.compressor in have_compressor:
  30. use_compressor = options.compressor
  31. sourceDirectory = "../lib"
  32. configFilename = "library.cfg"
  33. filename = "proj4js-compressed.js"
  34. outputFilename = "../lib/" + filename
  35. if config_file:
  36. configFilename = config_file
  37. extension = configFilename[-4:]
  38. if extension != ".cfg":
  39. configFilename = config_file + ".cfg"
  40. if output_file:
  41. outputFilename = output_file
  42. print "Merging libraries."
  43. merged = mergejs.run(sourceDirectory, None, configFilename)
  44. print "Setting the filename to "+filename
  45. merged = merged.replace('scriptName: "proj4js.js",','scriptName: "'+filename+'",');
  46. print "Compressing using %s" % use_compressor
  47. if use_compressor == "jsmin":
  48. minimized = jsmin.jsmin(merged)
  49. elif use_compressor == "minimize":
  50. minimized = minimize.minimize(merged)
  51. elif use_compressor == "closure_ws":
  52. if len(merged) > 1000000: # The maximum file size for this web service is 1000 KB.
  53. print "\nPre-compressing using jsmin"
  54. merged = jsmin.jsmin(merged)
  55. print "\nIs being compressed using Closure Compiler Service."
  56. try:
  57. minimized = closure_ws.minimize(merged)
  58. except Exception, E:
  59. print "\nAbnormal termination."
  60. sys.exit("ERROR: Closure Compilation using Web service failed!\n%s" % E)
  61. if len(minimized) <= 2:
  62. print "\nAbnormal termination due to compilation errors."
  63. sys.exit("ERROR: Closure Compilation using Web service failed!")
  64. else:
  65. print '\nClosure Compilation using Web service has completed successfully.'
  66. elif use_compressor == "closure":
  67. minimized = closure.minimize(merged)
  68. else: # fallback
  69. minimized = merged
  70. print "Adding license file."
  71. minimized = file("license.txt").read() + minimized
  72. print "Writing to %s." % outputFilename
  73. file(outputFilename, "w").write(minimized)
  74. print "Done."
  75. if __name__ == '__main__':
  76. opt = optparse.OptionParser(usage="%s [options] [config_file] [output_file]\n Default config_file is 'full.cfg', Default output_file is 'OpenLayers.js'")
  77. opt.add_option("-c", "--compressor", dest="compressor", help="compression method: one of 'jsmin', 'minimize', 'closure_ws', 'closure', or 'none'", default="jsmin")
  78. (options, args) = opt.parse_args()
  79. if not len(args):
  80. build(options=options)
  81. elif len(args) == 1:
  82. build(args[0], options=options)
  83. elif len(args) == 2:
  84. build(args[0], args[1], options=options)
  85. else:
  86. print "Wrong number of arguments"