1#!/usr/bin/env python
2
3# Copyright (c) 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
7import codecs
8import optparse
9import os
10import re
11import subprocess
12import sys
13
14_CATAPULT_PATH = os.path.abspath(
15    os.path.join(os.path.dirname(__file__), os.path.pardir, os.path.pardir))
16sys.path.append(os.path.join(_CATAPULT_PATH, 'tracing'))
17from tracing_build import vulcanize_trace_viewer
18
19SYSTRACE_TRACE_VIEWER_HTML_FILE_ = 'systrace_trace_viewer.html'
20CATAPULT_REV_ = 'CATAPULT_REV'
21NO_AUTO_UPDATE_ = 'NO_AUTO_UPDATE'
22
23
24def create_catapult_rev_str_(revision):
25  return '<!--' + CATAPULT_REV_ + '=' + str(revision) + '-->'
26
27
28def get_catapult_rev_in_file_():
29  assert os.path.exists(SYSTRACE_TRACE_VIEWER_HTML_FILE_)
30  rev = ''
31  with open(SYSTRACE_TRACE_VIEWER_HTML_FILE_, 'r') as f:
32    lines = f.readlines()
33    for line in lines[::-1]:
34      if CATAPULT_REV_ in line:
35        tokens = line.split(CATAPULT_REV_)
36        rev = re.sub(r'[=\->]', '', tokens[1]).strip()
37        break
38  return rev
39
40
41def get_catapult_rev_in_git_():
42  try:
43    return subprocess.check_output(
44        ['git', 'rev-parse', 'HEAD'],
45        cwd=os.path.dirname(os.path.abspath(__file__))).strip()
46  except subprocess.CalledProcessError:
47    return ''
48
49
50def update(no_auto_update=False, no_min=False):
51  """Update the systrace trace viewer html file.
52
53  When the html file exists, do not update the file if
54  1. the revision is NO_AUTO_UPDATE_;
55  2. or the revision is not changed.
56
57  Args:
58    no_auto_update: If true, force updating the file with revision
59                    NO_AUTO_UPDATE_. Future updates will be skipped unless this
60                    argument is true again.
61    no_min:         If true, skip minification when updating the file.
62  """
63  new_rev = None
64  if no_auto_update:
65    new_rev = NO_AUTO_UPDATE_
66  else:
67    new_rev = get_catapult_rev_in_git_()
68    if not new_rev:
69      return
70
71    if os.path.exists(SYSTRACE_TRACE_VIEWER_HTML_FILE_):
72      rev_in_file = get_catapult_rev_in_file_()
73      if rev_in_file == NO_AUTO_UPDATE_ or rev_in_file == new_rev:
74        return
75
76  # Generate the vulcanized result.
77  output_html_file = SYSTRACE_TRACE_VIEWER_HTML_FILE_
78  with codecs.open(output_html_file, encoding='utf-8', mode='w') as f:
79    vulcanize_trace_viewer.WriteTraceViewer(
80        f,
81        config_name='systrace',
82        minify=(not no_min),
83        output_html_head_and_body=False)
84    f.write(create_catapult_rev_str_(new_rev))
85  print 'Generated %s with revision %s.' % (output_html_file, new_rev)
86
87
88def main():
89  parser = optparse.OptionParser()
90  parser.add_option('--no-auto-update', dest='no_auto_update',
91                    default=False, action='store_true', help='force update the '
92                    'systrace trace viewer html file. Future auto updates will '
93                    'be skipped unless this flag is specified again.')
94  parser.add_option('--no-min', dest='no_min', default=False,
95                    action='store_true', help='skip minification')
96  # pylint: disable=unused-variable
97  options, unused_args = parser.parse_args(sys.argv[1:])
98
99  update(options.no_auto_update, options.no_min)
100
101
102if __name__ == '__main__':
103  main()
104