| Jamie Gennis | d1270ce | 2012-05-06 13:27:13 -0700 | [diff] [blame] | 1 | #!/usr/bin/python | 
|  | 2 |  | 
| Jeff Brown | 595ae1e | 2012-05-22 14:52:13 -0700 | [diff] [blame] | 3 | import httplib, urllib, subprocess, sys, config | 
| Jamie Gennis | d1270ce | 2012-05-06 13:27:13 -0700 | [diff] [blame] | 4 |  | 
|  | 5 | # Read all the Javascript files. | 
| Jeff Brown | 595ae1e | 2012-05-22 14:52:13 -0700 | [diff] [blame] | 6 | js_code = [('js_code', open(f).read()) for f in config.js_in_files] | 
| Jamie Gennis | d1270ce | 2012-05-06 13:27:13 -0700 | [diff] [blame] | 7 |  | 
|  | 8 | # Read all the CSS files and concatenate them. | 
| Jeff Brown | 595ae1e | 2012-05-22 14:52:13 -0700 | [diff] [blame] | 9 | css_code = ''.join(open(f).read() for f in config.css_in_files) | 
| Jamie Gennis | d1270ce | 2012-05-06 13:27:13 -0700 | [diff] [blame] | 10 |  | 
|  | 11 | # Define the parameters for the POST request and encode them in | 
|  | 12 | # a URL-safe format. | 
|  | 13 | params = urllib.urlencode(js_code + [ | 
|  | 14 | ('language', 'ECMASCRIPT5'), | 
|  | 15 | ('compilation_level', 'SIMPLE_OPTIMIZATIONS'), | 
|  | 16 | ('output_format', 'text'), | 
|  | 17 | ('output_info', 'compiled_code'), | 
|  | 18 | ]) | 
|  | 19 |  | 
|  | 20 | # Always use the following value for the Content-type header. | 
|  | 21 | headers = { "Content-type": "application/x-www-form-urlencoded" } | 
|  | 22 | conn = httplib.HTTPConnection('closure-compiler.appspot.com') | 
|  | 23 | conn.request('POST', '/compile', params, headers) | 
|  | 24 | response = conn.getresponse() | 
|  | 25 | data = response.read() | 
|  | 26 | conn.close | 
|  | 27 |  | 
|  | 28 | if response.status != 200: | 
|  | 29 | print sys.stderr, "error returned from JS compile service: %d" % response.status | 
|  | 30 | sys.exit(1) | 
|  | 31 |  | 
| Jeff Brown | 595ae1e | 2012-05-22 14:52:13 -0700 | [diff] [blame] | 32 | open(config.js_out_file, 'wt').write(data) | 
|  | 33 | print 'Generated %s.  Check the file to see if errors occured!' % config.js_out_file | 
| Jamie Gennis | d1270ce | 2012-05-06 13:27:13 -0700 | [diff] [blame] | 34 |  | 
| Jeff Brown | 595ae1e | 2012-05-22 14:52:13 -0700 | [diff] [blame] | 35 | yuic_args = ['yui-compressor', '--type', 'css', '-o', config.css_out_file] | 
| Jamie Gennis | d1270ce | 2012-05-06 13:27:13 -0700 | [diff] [blame] | 36 | p = subprocess.Popen(yuic_args, stdin=subprocess.PIPE) | 
|  | 37 | p.communicate(input=css_code) | 
|  | 38 | if p.wait() != 0: | 
| Jeff Brown | 595ae1e | 2012-05-22 14:52:13 -0700 | [diff] [blame] | 39 | print 'Failed to generate %s.' % config.css_out_file | 
| Jamie Gennis | d1270ce | 2012-05-06 13:27:13 -0700 | [diff] [blame] | 40 | sys.exit(1) | 
|  | 41 |  | 
| Jeff Brown | 595ae1e | 2012-05-22 14:52:13 -0700 | [diff] [blame] | 42 | print 'Generated %s.' % config.css_out_file |