1#!/usr/bin/env python
2# Copyright (c) 2014 The Chromium Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6
7"""Run the webpages_playback automation script."""
8
9
10import os
11import subprocess
12import sys
13
14sys.path.insert(0, os.getcwd())
15
16from common.py.utils import gs_utils
17from common.py.utils import shell_utils
18
19
20SKP_VERSION_FILE = 'SKP_VERSION'
21
22
23def _get_skp_version():
24  """Find an unused SKP version."""
25  current_skp_version = None
26  with open(SKP_VERSION_FILE) as f:
27    current_skp_version = int(f.read().rstrip())
28
29  # Find the first SKP version which has no uploaded SKPs.
30  new_version = current_skp_version + 1
31  while True:
32    gs_path = 'playback_%d/skps' % new_version
33    if not gs_utils.GSUtils().does_storage_object_exist('chromium-skia-gm',
34                                                        gs_path):
35      return new_version
36    new_version += 1
37
38
39def main(chrome_src_path, browser_executable):
40  browser_executable = os.path.realpath(browser_executable)
41  skp_version = _get_skp_version()
42  print 'SKP_VERSION=%d' % skp_version
43
44  if os.environ.get('CHROME_HEADLESS'):
45    # Start Xvfb if running on a bot.
46    try:
47      shell_utils.run('sudo Xvfb :0 -screen 0 1280x1024x24 &', shell=True)
48    except Exception:
49      # It is ok if the above command fails, it just means that DISPLAY=:0
50      # is already up.
51      pass
52
53  upload_dir = 'playback_%d' % skp_version
54  webpages_playback_cmd = [
55    'python', os.path.join(os.path.dirname(os.path.realpath(__file__)),
56                           'webpages_playback.py'),
57    '--page_sets', 'all',
58    '--browser_executable', browser_executable,
59    '--non-interactive',
60    '--upload',
61    '--alternate_upload_dir', upload_dir,
62    '--chrome_src_path', chrome_src_path,
63  ]
64
65  try:
66    shell_utils.run(webpages_playback_cmd)
67
68    # Temporary change to enable Slimming Paint runs. See skia:3763.
69    skia_page_sets_path = os.path.join(
70        os.path.dirname(os.path.realpath(__file__)), 'page_sets')
71    sp_skia_page_sets = [
72        os.path.join(skia_page_sets_path, 'skia_carsvg_desktop.py'),
73        os.path.join(skia_page_sets_path, 'skia_chalkboard_desktop.py'),
74        os.path.join(skia_page_sets_path, 'skia_css3gradients_desktop.py'),
75        os.path.join(skia_page_sets_path, 'skia_espn_desktop.py'),
76        os.path.join(skia_page_sets_path, 'skia_gmailthread_desktop.py'),
77        os.path.join(skia_page_sets_path, 'skia_googlehome_desktop.py'),
78        os.path.join(skia_page_sets_path, 'skia_googlespreadsheet_desktop.py'),
79        os.path.join(skia_page_sets_path, 'skia_jsfiddlebigcar_desktop.py'),
80        os.path.join(skia_page_sets_path, 'skia_mapsvg_desktop.py'),
81        os.path.join(skia_page_sets_path, 'skia_nytimes_desktop.py'),
82        os.path.join(skia_page_sets_path, 'skia_samoasvg_desktop.py'),
83        os.path.join(skia_page_sets_path, 'skia_tigersvg_desktop.py'),
84        os.path.join(skia_page_sets_path, 'skia_ugamsolutions_desktop.py'),
85        os.path.join(skia_page_sets_path, 'skia_digg_nexus10.py'),
86        os.path.join(skia_page_sets_path, 'skia_gmail_nexus10.py'),
87        os.path.join(skia_page_sets_path, 'skia_pravda_nexus10.py'),
88    ]
89    webpages_playback_cmd.extend([
90        '--skp_prefix', 'sp_',
91        '--browser_extra_args', '--enable-slimming-paint',
92        '--page_sets', '%s' % ','.join(sp_skia_page_sets),
93    ])
94    shell_utils.run(webpages_playback_cmd)
95  finally:
96    # Clean up any leftover browser instances. This can happen if there are
97    # telemetry crashes, processes are not always cleaned up appropriately by
98    # the webpagereplay and telemetry frameworks.
99    procs = subprocess.check_output(['ps', 'ax'])
100    for line in procs.splitlines():
101      if browser_executable in line:
102        pid = line.strip().split(' ')[0]
103        if pid != str(os.getpid()) and not 'python' in line:
104          try:
105            shell_utils.run(['kill', '-9', pid])
106          except shell_utils.CommandFailedException as e:
107            print e
108        else:
109          print 'Refusing to kill self.'
110
111  print 'writing %s: %s' % (SKP_VERSION_FILE, skp_version)
112  with open(SKP_VERSION_FILE, 'w') as f:
113    f.write(str(skp_version))
114
115
116if '__main__' == __name__:
117  if len(sys.argv) != 3:
118    print >> sys.stderr, 'USAGE: %s <chrome src path> <browser executable>'
119    sys.exit(1)
120  main(*sys.argv[1:])
121