compile.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. #!/usr/bin/env python
  2. # Copyright (c) 2011 The Chromium Authors. All rights reserved.
  3. # Use of this source code is governed by a BSD-style license that can be
  4. # found in the LICENSE file.
  5. """Combines the javascript files needed by jstemplate into a single file."""
  6. import httplib
  7. import sys
  8. import urllib
  9. def main():
  10. srcs = ['util.js', 'jsevalcontext.js', 'jstemplate.js', 'exports.js']
  11. out = 'jstemplate_compiled.js'
  12. # Wrap the output in an anonymous function to prevent poluting the global
  13. # namespace.
  14. output_wrapper = '(function(){%s})()'
  15. # Define the parameters for the POST request and encode them in a URL-safe
  16. # format. See http://code.google.com/closure/compiler/docs/api-ref.html for
  17. # API reference.
  18. params = urllib.urlencode(
  19. map(lambda src: ('js_code', file(src).read()), srcs) +
  20. [
  21. ('compilation_level', 'ADVANCED_OPTIMIZATIONS'),
  22. ('output_format', 'text'),
  23. ('output_info', 'compiled_code'),
  24. ])
  25. # Always use the following value for the Content-type header.
  26. headers = {'Content-type': 'application/x-www-form-urlencoded'}
  27. conn = httplib.HTTPSConnection('closure-compiler.appspot.com')
  28. conn.request('POST', '/compile', params, headers)
  29. response = conn.getresponse()
  30. out_file = file(out, 'w')
  31. out_file.write(output_wrapper % response.read())
  32. out_file.close()
  33. conn.close()
  34. return 0
  35. if __name__ == '__main__':
  36. sys.exit(main())