blob: ca911b7e95a1c8fb3cc5584fc6fdf86882a20ff6 [file] [log] [blame]
Jamie Gennis50dfd062012-05-22 16:29:02 -07001#!/usr/bin/python2.6
Jamie Gennisd1270ce2012-05-06 13:27:13 -07002
Jamie Gennis2da489c2012-09-19 18:06:29 -07003import httplib, json, os, urllib, shutil, subprocess, sys
Jamie Gennisd1270ce2012-05-06 13:27:13 -07004
Jamie Gennis2da489c2012-09-19 18:06:29 -07005minified_css_file = 'style.css'
6minified_js_file = 'script.js'
Jamie Gennisd1270ce2012-05-06 13:27:13 -07007
Jamie Gennis2da489c2012-09-19 18:06:29 -07008upstream_svn = 'http://trace-viewer.googlecode.com/svn/trunk/'
9
10script_dir = os.path.dirname(os.path.abspath(sys.argv[0]))
11trace_viewer_dir = os.path.join(script_dir, 'trace-viewer')
12
13# Remove the old source
14shutil.rmtree(trace_viewer_dir, True)
15
16# Pull the latest source from the upstream svn
17svn_co_args = ['svn', 'co', upstream_svn, trace_viewer_dir]
18p = subprocess.Popen(svn_co_args, stdout=subprocess.PIPE)
19svn_output = ''
20while p.poll() is None:
21 svn_output += p.stdout.read()
22if p.returncode != 0:
23 print 'Failed to checkout source from upstream svn.'
24 sys.exit(1)
25
26# Update the UPSTREAM_REVISION file
27rev_str = svn_output.split('\n')[-2]
28if not rev_str.startswith('Checked out revision '):
29 print 'Unrecognized revision string: %q' % rev_str
30open('UPSTREAM_REVISION', 'wt').write(rev_str[21:-1] + '\n')
31
32# Generate the flattened JS and CSS
33build_dir = os.path.join(trace_viewer_dir, 'build')
34sys.path.append(build_dir)
35gen = __import__('generate_standalone_timeline_view', {}, {})
36js_code = gen.generate_js()
37css_code = gen.generate_css()
Jamie Gennisd1270ce2012-05-06 13:27:13 -070038
39# Define the parameters for the POST request and encode them in
40# a URL-safe format.
Jamie Gennis2da489c2012-09-19 18:06:29 -070041params = urllib.urlencode([
42 ('js_code', js_code),
Jamie Gennisd1270ce2012-05-06 13:27:13 -070043 ('language', 'ECMASCRIPT5'),
44 ('compilation_level', 'SIMPLE_OPTIMIZATIONS'),
Jamie Gennis50dfd062012-05-22 16:29:02 -070045 ('output_format', 'json'),
46 ('output_info', 'errors'),
Jamie Gennisd1270ce2012-05-06 13:27:13 -070047 ('output_info', 'compiled_code'),
48])
49
50# Always use the following value for the Content-type header.
51headers = { "Content-type": "application/x-www-form-urlencoded" }
52conn = httplib.HTTPConnection('closure-compiler.appspot.com')
53conn.request('POST', '/compile', params, headers)
54response = conn.getresponse()
55data = response.read()
56conn.close
57
58if response.status != 200:
59 print sys.stderr, "error returned from JS compile service: %d" % response.status
60 sys.exit(1)
61
Jamie Gennis50dfd062012-05-22 16:29:02 -070062result = json.loads(data)
63if 'errors' in result:
Jamie Gennis2da489c2012-09-19 18:06:29 -070064 print 'Encountered error minifying Javascript. Writing intermediate code to flat_script.js'
65 open('flat_script.js', 'wt').write(js_code)
Jamie Gennis50dfd062012-05-22 16:29:02 -070066 for e in result['errors']:
67 filenum = int(e['file'][6:])
Jamie Gennis2da489c2012-09-19 18:06:29 -070068 filename = 'flat_script.js'
Jamie Gennis50dfd062012-05-22 16:29:02 -070069 lineno = e['lineno']
70 charno = e['charno']
71 err = e['error']
72 print '%s:%d:%d: %s' % (filename, lineno, charno, err)
Jamie Gennis2da489c2012-09-19 18:06:29 -070073 print 'Failed to generate %s.' % minified_js_file
Jamie Gennis50dfd062012-05-22 16:29:02 -070074 sys.exit(1)
75
Jamie Gennis2da489c2012-09-19 18:06:29 -070076open(minified_js_file, 'wt').write(result['compiledCode'] + '\n')
77print 'Generated %s' % minified_js_file
Jamie Gennisd1270ce2012-05-06 13:27:13 -070078
Jamie Gennis2da489c2012-09-19 18:06:29 -070079yuic_args = ['yui-compressor', '--type', 'css', '-o', minified_css_file]
Jamie Gennisd1270ce2012-05-06 13:27:13 -070080p = subprocess.Popen(yuic_args, stdin=subprocess.PIPE)
81p.communicate(input=css_code)
82if p.wait() != 0:
Jamie Gennis2da489c2012-09-19 18:06:29 -070083 print 'Failed to generate %s.' % minified_css_file
Jamie Gennisd1270ce2012-05-06 13:27:13 -070084 sys.exit(1)
85
Jamie Gennis2da489c2012-09-19 18:06:29 -070086print 'Generated %s' % minified_css_file