1#!/usr/bin/env python
2# Copyright 2013 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"""A script to find a recently-built Chrome, in the likely places.
7This script is used for automated testing, don't trust it for anything more
8than that!"""
9
10
11import optparse
12import os
13import sys
14
15
16def FindChrome(src_dir, configs, verbose=False):
17  # List of places that chrome could live.
18  # In theory we should be more careful about what platform we're actually
19  # building for.
20  # As currently constructed, this will also hork people who have debug and
21  # release builds sitting side by side who build locally.
22  chrome_locations = []
23
24  for config in configs:
25    chrome_locations.extend([
26        'build/%s/chrome.exe' % config,
27        'chrome/%s/chrome.exe' % config,
28        # Windows Chromium ninja builder
29        'out/%s/chrome.exe' % config,
30        # Linux
31        'out/%s/chrome' % config,
32        # Mac Chromium ninja builder
33        'out/%s/Chromium.app/Contents/MacOS/Chromium' % config,
34        # Mac release ninja builder
35        'out/%s/Google Chrome.app/Contents/MacOS/Google Chrome' % config,
36        # Mac Chromium xcode builder
37        'xcodebuild/%s/Chromium.app/Contents/MacOS/Chromium' % config,
38        # Mac release xcode builder
39        'xcodebuild/%s/Google Chrome.app/Contents/MacOS/Google Chrome' % config,
40    ])
41
42  # Pick the one with the newest timestamp.
43  latest_mtime = 0
44  latest_path = None
45  for chrome in chrome_locations:
46    chrome_filename = os.path.join(src_dir, chrome)
47    if verbose:
48      print 'Looking for %r...' % chrome_filename,
49    if os.path.exists(chrome_filename):
50      if verbose:
51        print 'YES.'
52      mtime = os.path.getmtime(chrome_filename)
53      if mtime > latest_mtime:
54        latest_mtime = mtime
55        latest_path = chrome_filename
56    else:
57      if verbose:
58        print 'NO.'
59  if latest_path is not None:
60    if verbose:
61      print 'Most recent is %r.' % latest_path
62    return latest_path
63  return None
64
65
66def main(args):
67  usage = 'Usage: %prog [options] <src dir>'
68  description = __doc__
69  parser = optparse.OptionParser(usage, description=description)
70  parser.add_option('-c', '--config',
71                    action='append',
72                    help='Which configuration of Chrome to look for. '
73                         'One of [Debug, Release]. The default is to try both. '
74                         'You can specify this multiple times.')
75  parser.add_option('-v', '--verbose', action='store_true',
76                    help='Verbose output')
77
78  options, args = parser.parse_args(args[1:])
79
80  if not len(args):
81    parser.error('Expected source directory as first argument.')
82
83  if not options.config:
84    options.config = ['Debug', 'Release']
85
86  invalid_configs = set(options.config) - set(['Debug', 'Release'])
87  if invalid_configs:
88    parser.error('Expected config to be one of [Debug, Release]. '
89                 'Got the following invalid configs: %s. ' %
90                 ', '.invalid_configs)
91
92  src_dir = args[0]
93  chrome_path = FindChrome(src_dir, options.config, options.verbose)
94  if not chrome_path:
95    sys.stderr.write('Error: Cannot find Chrome. '
96                     'Run again with -v to see where was searched.\n')
97    return 1
98
99  print chrome_path
100  return 0
101
102
103if __name__ == '__main__':
104  sys.exit(main(sys.argv))
105