Home | History | Annotate | Download | only in systrace
      1 #!/usr/bin/env python
      2 
      3 # Copyright 2015 The Chromium Authors. All rights reserved.
      4 # Use of this source code is governed by a BSD-style license that can be
      5 # found in the LICENSE file.
      6 
      7 import codecs
      8 import optparse
      9 import os
     10 import re
     11 import subprocess
     12 import sys
     13 
     14 _CATAPULT_PATH = os.path.abspath(
     15     os.path.join(os.path.dirname(__file__), os.path.pardir, os.path.pardir))
     16 sys.path.append(os.path.join(_CATAPULT_PATH, 'tracing'))
     17 
     18 from tracing_build import vulcanize_trace_viewer
     19 
     20 
     21 SYSTRACE_TRACE_VIEWER_HTML_FILE = os.path.join(
     22     os.path.abspath(os.path.dirname(__file__)),
     23     'systrace_trace_viewer.html')
     24 CATAPULT_REV_ = 'CATAPULT_REV'
     25 NO_AUTO_UPDATE_ = 'NO_AUTO_UPDATE'
     26 UNKNOWN_REVISION_ = 'UNKNOWN'
     27 
     28 
     29 def create_catapult_rev_str_(revision):
     30   return '<!--' + CATAPULT_REV_ + '=' + str(revision) + '-->'
     31 
     32 
     33 def get_catapult_rev_in_file_(html_file):
     34   assert os.path.exists(html_file)
     35   rev = ''
     36   with open(html_file, 'r') as f:
     37     lines = f.readlines()
     38     for line in lines[::-1]:
     39       if CATAPULT_REV_ in line:
     40         tokens = line.split(CATAPULT_REV_)
     41         rev = re.sub(r'[=\->]', '', tokens[1]).strip()
     42         break
     43   return rev
     44 
     45 
     46 def get_catapult_rev_in_git_():
     47   try:
     48     catapult_rev = subprocess.check_output(
     49         'git rev-parse HEAD',
     50         shell=True, # Needed by Windows
     51         cwd=os.path.dirname(os.path.abspath(__file__))).strip()
     52   except (subprocess.CalledProcessError, OSError):
     53     return None
     54   if not catapult_rev:
     55     return None
     56   return catapult_rev
     57 
     58 
     59 def update(no_auto_update=False, no_min=False, force_update=False):
     60   """Update the systrace trace viewer html file.
     61 
     62   When the html file exists, do not update the file if
     63   1. the revision is NO_AUTO_UPDATE_;
     64   2. or the revision is not changed.
     65 
     66   Args:
     67     no_auto_update: If true, force updating the file with revision
     68                     NO_AUTO_UPDATE_. Future auto-updates will be skipped.
     69     no_min:         If true, skip minification when updating the file.
     70     force_update:   If true, update the systrace trace viewer file no matter
     71                     what.
     72   """
     73   if no_auto_update:
     74     new_rev = NO_AUTO_UPDATE_
     75   else:
     76     new_rev = get_catapult_rev_in_git_()
     77     if not new_rev:
     78       # Source tree could be missing git metadata.
     79       print >> sys.stderr, 'Warning: Couldn\'t determine current git revision.'
     80       new_rev = UNKNOWN_REVISION_
     81 
     82   need_update = False
     83   if force_update:
     84     need_update = True
     85   elif no_auto_update:
     86     need_update = True
     87   elif not os.path.exists(SYSTRACE_TRACE_VIEWER_HTML_FILE):
     88     need_update = True
     89   else:
     90     old_rev = get_catapult_rev_in_file_(SYSTRACE_TRACE_VIEWER_HTML_FILE)
     91     if not old_rev or old_rev == UNKNOWN_REVISION_:
     92       need_update = True
     93     # If old_rev was set to NO_AUTO_UPDATE_ it should be skipped, since forced
     94     # update cases have been already handled above.
     95     if old_rev != new_rev and old_rev != NO_AUTO_UPDATE_:
     96       need_update = True
     97 
     98   if not need_update:
     99     print 'Update skipped.'
    100     return
    101 
    102   print 'Generating viewer file %s with revision %s.' % (
    103             SYSTRACE_TRACE_VIEWER_HTML_FILE, new_rev)
    104 
    105   # Generate the vulcanized result.
    106   with codecs.open(SYSTRACE_TRACE_VIEWER_HTML_FILE,
    107                    encoding='utf-8', mode='w') as f:
    108     vulcanize_trace_viewer.WriteTraceViewer(
    109         f,
    110         config_name='full',
    111         minify=(not no_min),
    112         output_html_head_and_body=False)
    113     if not force_update:
    114       f.write(create_catapult_rev_str_(new_rev))
    115 
    116 def main():
    117   parser = optparse.OptionParser()
    118   parser.add_option('--force-update', dest='force_update',
    119                     default=False, action='store_true', help='force update the '
    120                     'systrace trace viewer html file')
    121   parser.add_option('--no-auto-update', dest='no_auto_update',
    122                     default=False, action='store_true', help='force update the '
    123                     'systrace trace viewer html file and disable auto-updates, '
    124                     'delete \'systrace_trace_viewer.html\' to re-enable '
    125                     'auto-updates')
    126   parser.add_option('--no-min', dest='no_min', default=False,
    127                     action='store_true', help='skip minification')
    128   # pylint: disable=unused-variable
    129   options, unused_args = parser.parse_args(sys.argv[1:])
    130 
    131   update(no_auto_update=options.no_auto_update,
    132          no_min=options.no_min,
    133          force_update=options.force_update)
    134 
    135 if __name__ == '__main__':
    136   main()
    137