1#!/usr/bin/env python
2#
3# Copyright 2016 Google Inc.
4#
5# Use of this source code is governed by a BSD-style license that can be
6# found in the LICENSE file.
7
8
9"""Create the SKP asset."""
10
11
12import argparse
13import common
14import os
15import shutil
16import subprocess
17import utils
18
19
20SKIA_TOOLS = os.path.join(common.INFRA_BOTS_DIR, os.pardir, os.pardir, 'tools')
21
22PRIVATE_SKPS_GS = 'gs://skia-skps/private/skps'
23
24
25def create_asset(chrome_src_path, browser_executable, target_dir,
26                 upload_to_partner_bucket):
27  """Create the asset."""
28  browser_executable = os.path.realpath(browser_executable)
29  chrome_src_path = os.path.realpath(chrome_src_path)
30  target_dir = os.path.realpath(target_dir)
31
32  if not os.path.exists(target_dir):
33    os.makedirs(target_dir)
34
35  with utils.tmp_dir():
36    if os.environ.get('CHROME_HEADLESS'):
37      # Start Xvfb if running on a bot.
38      try:
39        subprocess.Popen(['sudo', 'Xvfb', ':0', '-screen', '0', '1280x1024x24'])
40      except Exception:
41        # It is ok if the above command fails, it just means that DISPLAY=:0
42        # is already up.
43        pass
44
45    webpages_playback_cmd = [
46      'python', os.path.join(SKIA_TOOLS, 'skp', 'webpages_playback.py'),
47      '--page_sets', 'all',
48      '--browser_executable', browser_executable,
49      '--non-interactive',
50      '--output_dir', os.getcwd(),
51      '--chrome_src_path', chrome_src_path,
52    ]
53    if upload_to_partner_bucket:
54      webpages_playback_cmd.append('--upload_to_partner_bucket')
55    print 'Running webpages_playback command:\n$ %s' % (
56        ' '.join(webpages_playback_cmd))
57    try:
58      subprocess.check_call(webpages_playback_cmd)
59    finally:
60      # Clean up any leftover browser instances. This can happen if there are
61      # telemetry crashes, processes are not always cleaned up appropriately by
62      # the webpagereplay and telemetry frameworks.
63      procs = subprocess.check_output(['ps', 'ax'])
64      for line in procs.splitlines():
65        if browser_executable in line:
66          pid = line.strip().split(' ')[0]
67          if pid != str(os.getpid()) and not 'python' in line:
68            try:
69              subprocess.check_call(['kill', '-9', pid])
70            except subprocess.CalledProcessError as e:
71              print e
72          else:
73            print 'Refusing to kill self.'
74    src = os.path.join(os.getcwd(), 'playback', 'skps')
75    for f in os.listdir(src):
76      if f.endswith('.skp'):
77        shutil.copyfile(os.path.join(src, f), os.path.join(target_dir, f))
78
79  # Copy over private SKPs from Google storage into the target_dir.
80  subprocess.check_call([
81        'gsutil', 'cp', os.path.join(PRIVATE_SKPS_GS, '*'), target_dir])
82
83
84def main():
85  parser = argparse.ArgumentParser()
86  parser.add_argument('--target_dir', '-t', required=True)
87  parser.add_argument('--chrome_src_path', '-c', required=True)
88  parser.add_argument('--browser_executable', '-e', required=True)
89  parser.add_argument('--upload_to_partner_bucket', action='store_true')
90  args = parser.parse_args()
91  create_asset(args.chrome_src_path, args.browser_executable, args.target_dir,
92               args.upload_to_partner_bucket)
93
94
95if __name__ == '__main__':
96  main()
97