Home | History | Annotate | Download | only in chromium-trace
      1 #!/usr/bin/python2.6
      2 
      3 import httplib, json, optparse, os, urllib, shutil, subprocess, sys
      4 
      5 output_css_file = 'style.css'
      6 output_js_file = 'script.js'
      7 
      8 upstream_svn = 'http://trace-viewer.googlecode.com/svn/trunk/'
      9 
     10 script_dir = os.path.dirname(os.path.abspath(sys.argv[0]))
     11 trace_viewer_dir = os.path.join(script_dir, 'trace-viewer')
     12 
     13 parser = optparse.OptionParser()
     14 parser.add_option('--local', dest='local_dir', metavar='DIR',
     15                   help='use a local trace-viewer')
     16 parser.add_option('--no-min', dest='no_min', default=False, action='store_true',
     17                   help='skip minification')
     18 options, args = parser.parse_args()
     19 
     20 if options.local_dir is None:
     21   # Remove the old source
     22   shutil.rmtree(trace_viewer_dir, True)
     23 
     24   # Pull the latest source from the upstream svn
     25   svn_co_args = ['svn', 'co', upstream_svn, trace_viewer_dir]
     26   p = subprocess.Popen(svn_co_args, stdout=subprocess.PIPE)
     27   svn_output = ''
     28   while p.poll() is None:
     29     svn_output += p.stdout.read()
     30   if p.returncode != 0:
     31     print 'Failed to checkout source from upstream svn.'
     32     sys.exit(1)
     33 
     34   # Update the UPSTREAM_REVISION file
     35   rev_str = svn_output.split('\n')[-2]
     36   if not rev_str.startswith('Checked out revision '):
     37     print 'Unrecognized revision string: %q' % rev_str
     38   open('UPSTREAM_REVISION', 'wt').write(rev_str[21:-1] + '\n')
     39 else:
     40   trace_viewer_dir = options.local_dir
     41 
     42 # Generate the flattened JS and CSS
     43 build_dir = os.path.join(trace_viewer_dir, 'build')
     44 sys.path.append(build_dir)
     45 gen = __import__('generate_standalone_timeline_view', {}, {})
     46 js_code = gen.generate_js()
     47 css_code = gen.generate_css()
     48 
     49 if options.no_min:
     50   open(output_js_file, 'wt').write(js_code)
     51   print 'Generated %s' % output_js_file
     52   open(output_css_file, 'wt').write(css_code)
     53   print 'Generated %s' % output_css_file
     54 else:
     55   # Define the parameters for the POST request and encode them in
     56   # a URL-safe format.
     57   params = urllib.urlencode([
     58     ('js_code', js_code),
     59     ('language', 'ECMASCRIPT5'),
     60     ('compilation_level', 'SIMPLE_OPTIMIZATIONS'),
     61     ('output_format', 'json'),
     62     ('output_info', 'errors'),
     63     ('output_info', 'compiled_code'),
     64   ])
     65 
     66   # Always use the following value for the Content-type header.
     67   headers = { "Content-type": "application/x-www-form-urlencoded" }
     68   conn = httplib.HTTPConnection('closure-compiler.appspot.com')
     69   conn.request('POST', '/compile', params, headers)
     70   response = conn.getresponse()
     71   data = response.read()
     72   conn.close
     73 
     74   if response.status != 200:
     75     print sys.stderr, "error returned from JS compile service: %d" % response.status
     76     sys.exit(1)
     77 
     78   result = json.loads(data)
     79   if 'errors' in result:
     80     print 'Encountered error minifying Javascript.  Writing intermediate code to flat_script.js'
     81     open('flat_script.js', 'wt').write(js_code)
     82     for e in result['errors']:
     83       filenum = int(e['file'][6:])
     84       filename = 'flat_script.js'
     85       lineno = e['lineno']
     86       charno = e['charno']
     87       err = e['error']
     88       print '%s:%d:%d: %s' % (filename, lineno, charno, err)
     89     print 'Failed to generate %s.' % output_js_file
     90     sys.exit(1)
     91 
     92   open(output_js_file, 'wt').write(result['compiledCode'] + '\n')
     93   print 'Generated %s' % output_js_file
     94 
     95   yuic_args = ['yui-compressor', '--type', 'css', '-o', output_css_file]
     96   p = subprocess.Popen(yuic_args, stdin=subprocess.PIPE)
     97   p.communicate(input=css_code)
     98   if p.wait() != 0:
     99     print 'Failed to generate %s.' % output_css_file
    100     sys.exit(1)
    101 
    102   print 'Generated %s' % output_css_file
    103