borenet | dc89ca5 | 2014-10-17 07:37:05 -0700 | [diff] [blame] | 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 | |
| 10 | import os |
| 11 | import subprocess |
| 12 | import sys |
| 13 | |
| 14 | sys.path.insert(0, os.getcwd()) |
| 15 | |
| 16 | from common.py.utils import gs_utils |
| 17 | from common.py.utils import shell_utils |
| 18 | |
| 19 | |
| 20 | SKP_VERSION_FILE = 'SKP_VERSION' |
| 21 | |
| 22 | |
| 23 | def _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 | |
rmistry | 37ed996 | 2016-02-03 07:08:02 -0800 | [diff] [blame] | 39 | def main(chrome_src_path, browser_executable, dry_run): |
borenet | dc89ca5 | 2014-10-17 07:37:05 -0700 | [diff] [blame] | 40 | browser_executable = os.path.realpath(browser_executable) |
rmistry | 37ed996 | 2016-02-03 07:08:02 -0800 | [diff] [blame] | 41 | dry_run = (dry_run == 'True') |
borenet | dc89ca5 | 2014-10-17 07:37:05 -0700 | [diff] [blame] | 42 | skp_version = _get_skp_version() |
| 43 | print 'SKP_VERSION=%d' % skp_version |
| 44 | |
| 45 | if os.environ.get('CHROME_HEADLESS'): |
| 46 | # Start Xvfb if running on a bot. |
| 47 | try: |
| 48 | shell_utils.run('sudo Xvfb :0 -screen 0 1280x1024x24 &', shell=True) |
| 49 | except Exception: |
| 50 | # It is ok if the above command fails, it just means that DISPLAY=:0 |
| 51 | # is already up. |
| 52 | pass |
| 53 | |
| 54 | upload_dir = 'playback_%d' % skp_version |
| 55 | webpages_playback_cmd = [ |
| 56 | 'python', os.path.join(os.path.dirname(os.path.realpath(__file__)), |
| 57 | 'webpages_playback.py'), |
| 58 | '--page_sets', 'all', |
| 59 | '--browser_executable', browser_executable, |
| 60 | '--non-interactive', |
kkinnunen | b4ee7ea | 2015-03-31 00:18:26 -0700 | [diff] [blame] | 61 | '--upload', |
borenet | dc89ca5 | 2014-10-17 07:37:05 -0700 | [diff] [blame] | 62 | '--alternate_upload_dir', upload_dir, |
| 63 | '--chrome_src_path', chrome_src_path, |
| 64 | ] |
rmistry | 37ed996 | 2016-02-03 07:08:02 -0800 | [diff] [blame] | 65 | if not dry_run: |
| 66 | webpages_playback_cmd.append('--upload_to_partner_bucket') |
borenet | dc89ca5 | 2014-10-17 07:37:05 -0700 | [diff] [blame] | 67 | |
| 68 | try: |
| 69 | shell_utils.run(webpages_playback_cmd) |
| 70 | finally: |
| 71 | # Clean up any leftover browser instances. This can happen if there are |
| 72 | # telemetry crashes, processes are not always cleaned up appropriately by |
| 73 | # the webpagereplay and telemetry frameworks. |
| 74 | procs = subprocess.check_output(['ps', 'ax']) |
| 75 | for line in procs.splitlines(): |
| 76 | if browser_executable in line: |
| 77 | pid = line.strip().split(' ')[0] |
borenet | 25956ce | 2014-10-17 14:17:18 -0700 | [diff] [blame] | 78 | if pid != str(os.getpid()) and not 'python' in line: |
borenet | dc89ca5 | 2014-10-17 07:37:05 -0700 | [diff] [blame] | 79 | try: |
| 80 | shell_utils.run(['kill', '-9', pid]) |
| 81 | except shell_utils.CommandFailedException as e: |
| 82 | print e |
| 83 | else: |
| 84 | print 'Refusing to kill self.' |
| 85 | |
| 86 | print 'writing %s: %s' % (SKP_VERSION_FILE, skp_version) |
| 87 | with open(SKP_VERSION_FILE, 'w') as f: |
| 88 | f.write(str(skp_version)) |
| 89 | |
| 90 | |
| 91 | if '__main__' == __name__: |
rmistry | 37ed996 | 2016-02-03 07:08:02 -0800 | [diff] [blame] | 92 | if len(sys.argv) != 4: |
| 93 | print >> sys.stderr, ('USAGE: %s <chrome src path> <browser executable> ' |
| 94 | '<dry run>') |
borenet | dc89ca5 | 2014-10-17 07:37:05 -0700 | [diff] [blame] | 95 | sys.exit(1) |
| 96 | main(*sys.argv[1:]) |