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 22 23def create_asset(chrome_src_path, browser_executable, target_dir, 24 upload_to_partner_bucket): 25 """Create the asset.""" 26 browser_executable = os.path.realpath(browser_executable) 27 chrome_src_path = os.path.realpath(chrome_src_path) 28 target_dir = os.path.realpath(target_dir) 29 30 if not os.path.exists(target_dir): 31 os.makedirs(target_dir) 32 33 with utils.tmp_dir(): 34 if os.environ.get('CHROME_HEADLESS'): 35 # Start Xvfb if running on a bot. 36 try: 37 subprocess.Popen(['sudo', 'Xvfb', ':0', '-screen', '0', '1280x1024x24']) 38 except Exception: 39 # It is ok if the above command fails, it just means that DISPLAY=:0 40 # is already up. 41 pass 42 43 webpages_playback_cmd = [ 44 'python', os.path.join(SKIA_TOOLS, 'skp', 'webpages_playback.py'), 45 '--page_sets', 'all', 46 '--browser_executable', browser_executable, 47 '--non-interactive', 48 '--output_dir', os.getcwd(), 49 '--chrome_src_path', chrome_src_path, 50 ] 51 if upload_to_partner_bucket: 52 webpages_playback_cmd.append('--upload_to_partner_bucket') 53 print 'Running webpages_playback command:\n$ %s' % ( 54 ' '.join(webpages_playback_cmd)) 55 try: 56 subprocess.check_call(webpages_playback_cmd) 57 finally: 58 # Clean up any leftover browser instances. This can happen if there are 59 # telemetry crashes, processes are not always cleaned up appropriately by 60 # the webpagereplay and telemetry frameworks. 61 procs = subprocess.check_output(['ps', 'ax']) 62 for line in procs.splitlines(): 63 if browser_executable in line: 64 pid = line.strip().split(' ')[0] 65 if pid != str(os.getpid()) and not 'python' in line: 66 try: 67 subprocess.check_call(['kill', '-9', pid]) 68 except subprocess.CalledProcessError as e: 69 print e 70 else: 71 print 'Refusing to kill self.' 72 src = os.path.join(os.getcwd(), 'playback', 'skps') 73 for f in os.listdir(src): 74 if f.endswith('.skp'): 75 shutil.copyfile(os.path.join(src, f), os.path.join(target_dir, f)) 76 77 78def main(): 79 parser = argparse.ArgumentParser() 80 parser.add_argument('--target_dir', '-t', required=True) 81 parser.add_argument('--chrome_src_path', '-c', required=True) 82 parser.add_argument('--browser_executable', '-e', required=True) 83 parser.add_argument('--upload_to_partner_bucket', action='store_true') 84 args = parser.parse_args() 85 create_asset(args.chrome_src_path, args.browser_executable, args.target_dir, 86 args.upload_to_partner_bucket) 87 88 89if __name__ == '__main__': 90 main() 91