17dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch#!/usr/bin/env python
27dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch# Copyright 2013 The Chromium Authors. All rights reserved.
37dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch# Use of this source code is governed by a BSD-style license that can be
47dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch# found in the LICENSE file.
57dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
67dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch"""A script to find a recently-built Chrome, in the likely places.
77dbb3d5cf0c15f500944d211057644d6a2f37371Ben MurdochThis script is used for automated testing, don't trust it for anything more
87dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdochthan that!"""
97dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
107dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
117dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdochimport optparse
127dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdochimport os
137dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdochimport sys
147dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
157dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
167dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdochdef FindChrome(src_dir, configs, verbose=False):
177dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  # List of places that chrome could live.
187dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  # In theory we should be more careful about what platform we're actually
197dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  # building for.
207dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  # As currently constructed, this will also hork people who have debug and
217dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  # release builds sitting side by side who build locally.
227dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  chrome_locations = []
237dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
247dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  for config in configs:
257dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    chrome_locations.extend([
267dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch        'build/%s/chrome.exe' % config,
277dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch        'chrome/%s/chrome.exe' % config,
287dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch        # Windows Chromium ninja builder
297dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch        'out/%s/chrome.exe' % config,
304e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)        # Linux
317dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch        'out/%s/chrome' % config,
324e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)        # Mac Chromium ninja builder
337dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch        'out/%s/Chromium.app/Contents/MacOS/Chromium' % config,
34f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        # Mac release ninja builder
357dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch        'out/%s/Google Chrome.app/Contents/MacOS/Google Chrome' % config,
367dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch        # Mac Chromium xcode builder
377dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch        'xcodebuild/%s/Chromium.app/Contents/MacOS/Chromium' % config,
387dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch        # Mac release xcode builder
397dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch        'xcodebuild/%s/Google Chrome.app/Contents/MacOS/Google Chrome' % config,
407dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    ])
417dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
427dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  # Pick the one with the newest timestamp.
437dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  latest_mtime = 0
447dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  latest_path = None
457dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  for chrome in chrome_locations:
467dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    chrome_filename = os.path.join(src_dir, chrome)
477dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    if verbose:
487dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      print 'Looking for %r...' % chrome_filename,
497dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    if os.path.exists(chrome_filename):
507dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      if verbose:
517dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch        print 'YES.'
527dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      mtime = os.path.getmtime(chrome_filename)
537dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      if mtime > latest_mtime:
547dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch        latest_mtime = mtime
557dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch        latest_path = chrome_filename
567dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    else:
577dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      if verbose:
587dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch        print 'NO.'
597dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  if latest_path is not None:
607dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    if verbose:
617dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      print 'Most recent is %r.' % latest_path
627dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    return latest_path
637dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  return None
647dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
657dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
667dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdochdef main(args):
677dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  usage = 'Usage: %prog [options] <src dir>'
687dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  description = __doc__
697dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  parser = optparse.OptionParser(usage, description=description)
707dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  parser.add_option('-c', '--config',
717dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch                    action='append',
727dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch                    help='Which configuration of Chrome to look for. '
737dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch                         'One of [Debug, Release]. The default is to try both. '
747dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch                         'You can specify this multiple times.')
757dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  parser.add_option('-v', '--verbose', action='store_true',
767dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch                    help='Verbose output')
777dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
787dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  options, args = parser.parse_args(args[1:])
797dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
807dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  if not len(args):
817dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    parser.error('Expected source directory as first argument.')
827dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
837dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  if not options.config:
847dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    options.config = ['Debug', 'Release']
857dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
867dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  invalid_configs = set(options.config) - set(['Debug', 'Release'])
877dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  if invalid_configs:
887dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    parser.error('Expected config to be one of [Debug, Release]. '
897dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch                 'Got the following invalid configs: %s. ' %
907dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch                 ', '.invalid_configs)
917dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
927dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  src_dir = args[0]
937dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  chrome_path = FindChrome(src_dir, options.config, options.verbose)
947dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  if not chrome_path:
957dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    sys.stderr.write('Error: Cannot find Chrome. '
967dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch                     'Run again with -v to see where was searched.\n')
977dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    return 1
987dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
997dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  print chrome_path
1007dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  return 0
1017dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
1027dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
1037dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdochif __name__ == '__main__':
1047dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  sys.exit(main(sys.argv))
105