blob: 92d0352900d8cc43afb2fe930116fc346823f059 [file] [log] [blame]
import httplib, json, optparse, os, urllib, shutil, subprocess, sys
output_css_file = 'style.css'
output_js_file = 'script.js'
upstream_svn = ''
script_dir = os.path.dirname(os.path.abspath(sys.argv[0]))
trace_viewer_dir = os.path.join(script_dir, 'trace-viewer')
parser = optparse.OptionParser()
parser.add_option('--local', dest='local_dir', metavar='DIR',
help='use a local trace-viewer')
parser.add_option('--no-min', dest='no_min', default=False, action='store_true',
help='skip minification')
options, args = parser.parse_args()
if options.local_dir is None:
# Remove the old source
shutil.rmtree(trace_viewer_dir, True)
# Pull the latest source from the upstream svn
svn_co_args = ['svn', 'co', upstream_svn, trace_viewer_dir]
p = subprocess.Popen(svn_co_args, stdout=subprocess.PIPE)
svn_output = ''
while p.poll() is None:
svn_output +=
if p.returncode != 0:
print 'Failed to checkout source from upstream svn.'
# Update the UPSTREAM_REVISION file
rev_str = svn_output.split('\n')[-2]
if not rev_str.startswith('Checked out revision '):
print 'Unrecognized revision string: %q' % rev_str
open('UPSTREAM_REVISION', 'wt').write(rev_str[21:-1] + '\n')
trace_viewer_dir = options.local_dir
# Generate the flattened JS and CSS
build_dir = os.path.join(trace_viewer_dir, 'build')
gen = __import__('generate_standalone_timeline_view', {}, {})
js_code = gen.generate_js()
css_code = gen.generate_css()
if options.no_min:
open(output_js_file, 'wt').write(js_code)
print 'Generated %s' % output_js_file
open(output_css_file, 'wt').write(css_code)
print 'Generated %s' % output_css_file
# Define the parameters for the POST request and encode them in
# a URL-safe format.
params = urllib.urlencode([
('js_code', js_code),
('language', 'ECMASCRIPT5'),
('compilation_level', 'SIMPLE_OPTIMIZATIONS'),
('output_format', 'json'),
('output_info', 'errors'),
('output_info', 'compiled_code'),
# Always use the following value for the Content-type header.
headers = { "Content-type": "application/x-www-form-urlencoded" }
conn = httplib.HTTPConnection('')
conn.request('POST', '/compile', params, headers)
response = conn.getresponse()
data =
if response.status != 200:
print sys.stderr, "error returned from JS compile service: %d" % response.status
result = json.loads(data)
if 'errors' in result:
print 'Encountered error minifying Javascript. Writing intermediate code to flat_script.js'
open('flat_script.js', 'wt').write(js_code)
for e in result['errors']:
filenum = int(e['file'][6:])
filename = 'flat_script.js'
lineno = e['lineno']
charno = e['charno']
err = e['error']
print '%s:%d:%d: %s' % (filename, lineno, charno, err)
print 'Failed to generate %s.' % output_js_file
open(output_js_file, 'wt').write(result['compiledCode'] + '\n')
print 'Generated %s' % output_js_file
yuic_args = ['yui-compressor', '--type', 'css', '-o', output_css_file]
p = subprocess.Popen(yuic_args, stdin=subprocess.PIPE)
if p.wait() != 0:
print 'Failed to generate %s.' % output_css_file
print 'Generated %s' % output_css_file