1164052e9cef7b1b6d64860f52176a4413b23fc5ccommit-bot@chromium.org#!/usr/bin/python
2164052e9cef7b1b6d64860f52176a4413b23fc5ccommit-bot@chromium.org# Copyright (c) 2014 The Chromium Authors. All rights reserved.
3164052e9cef7b1b6d64860f52176a4413b23fc5ccommit-bot@chromium.org# Use of this source code is governed by a BSD-style license that can be
4164052e9cef7b1b6d64860f52176a4413b23fc5ccommit-bot@chromium.org# found in the LICENSE file.
5164052e9cef7b1b6d64860f52176a4413b23fc5ccommit-bot@chromium.org
6164052e9cef7b1b6d64860f52176a4413b23fc5ccommit-bot@chromium.org"""Module that finds and runs a binary by looking in the likely locations."""
7164052e9cef7b1b6d64860f52176a4413b23fc5ccommit-bot@chromium.org
8164052e9cef7b1b6d64860f52176a4413b23fc5ccommit-bot@chromium.org
9164052e9cef7b1b6d64860f52176a4413b23fc5ccommit-bot@chromium.orgimport os
10164052e9cef7b1b6d64860f52176a4413b23fc5ccommit-bot@chromium.orgimport subprocess
11164052e9cef7b1b6d64860f52176a4413b23fc5ccommit-bot@chromium.orgimport sys
12164052e9cef7b1b6d64860f52176a4413b23fc5ccommit-bot@chromium.org
13164052e9cef7b1b6d64860f52176a4413b23fc5ccommit-bot@chromium.org
14164052e9cef7b1b6d64860f52176a4413b23fc5ccommit-bot@chromium.orgdef run_command(args):
15164052e9cef7b1b6d64860f52176a4413b23fc5ccommit-bot@chromium.org  """Runs a program from the command line and returns stdout.
16164052e9cef7b1b6d64860f52176a4413b23fc5ccommit-bot@chromium.org
17164052e9cef7b1b6d64860f52176a4413b23fc5ccommit-bot@chromium.org  Args:
18164052e9cef7b1b6d64860f52176a4413b23fc5ccommit-bot@chromium.org    args: Command line to run, as a list of string parameters. args[0] is the
19164052e9cef7b1b6d64860f52176a4413b23fc5ccommit-bot@chromium.org          binary to run.
20164052e9cef7b1b6d64860f52176a4413b23fc5ccommit-bot@chromium.org
21164052e9cef7b1b6d64860f52176a4413b23fc5ccommit-bot@chromium.org  Returns:
22164052e9cef7b1b6d64860f52176a4413b23fc5ccommit-bot@chromium.org    stdout from the program, as a single string.
23164052e9cef7b1b6d64860f52176a4413b23fc5ccommit-bot@chromium.org
24164052e9cef7b1b6d64860f52176a4413b23fc5ccommit-bot@chromium.org  Raises:
25164052e9cef7b1b6d64860f52176a4413b23fc5ccommit-bot@chromium.org    Exception: the program exited with a nonzero return code.
26164052e9cef7b1b6d64860f52176a4413b23fc5ccommit-bot@chromium.org  """
27164052e9cef7b1b6d64860f52176a4413b23fc5ccommit-bot@chromium.org  proc = subprocess.Popen(args,
28164052e9cef7b1b6d64860f52176a4413b23fc5ccommit-bot@chromium.org                          stdout=subprocess.PIPE,
29164052e9cef7b1b6d64860f52176a4413b23fc5ccommit-bot@chromium.org                          stderr=subprocess.PIPE)
30164052e9cef7b1b6d64860f52176a4413b23fc5ccommit-bot@chromium.org  (stdout, stderr) = proc.communicate()
31164052e9cef7b1b6d64860f52176a4413b23fc5ccommit-bot@chromium.org  if proc.returncode is not 0:
32164052e9cef7b1b6d64860f52176a4413b23fc5ccommit-bot@chromium.org    raise Exception('command "%s" failed: %s' % (args, stderr))
33164052e9cef7b1b6d64860f52176a4413b23fc5ccommit-bot@chromium.org  return stdout
34164052e9cef7b1b6d64860f52176a4413b23fc5ccommit-bot@chromium.org
35164052e9cef7b1b6d64860f52176a4413b23fc5ccommit-bot@chromium.org
36164052e9cef7b1b6d64860f52176a4413b23fc5ccommit-bot@chromium.orgdef find_path_to_program(program):
37164052e9cef7b1b6d64860f52176a4413b23fc5ccommit-bot@chromium.org  """Returns path to an existing program binary.
38164052e9cef7b1b6d64860f52176a4413b23fc5ccommit-bot@chromium.org
39164052e9cef7b1b6d64860f52176a4413b23fc5ccommit-bot@chromium.org  Args:
40164052e9cef7b1b6d64860f52176a4413b23fc5ccommit-bot@chromium.org    program: Basename of the program to find (e.g., 'render_pictures').
41164052e9cef7b1b6d64860f52176a4413b23fc5ccommit-bot@chromium.org
42164052e9cef7b1b6d64860f52176a4413b23fc5ccommit-bot@chromium.org  Returns:
43164052e9cef7b1b6d64860f52176a4413b23fc5ccommit-bot@chromium.org    Absolute path to the program binary, as a string.
44164052e9cef7b1b6d64860f52176a4413b23fc5ccommit-bot@chromium.org
45164052e9cef7b1b6d64860f52176a4413b23fc5ccommit-bot@chromium.org  Raises:
46164052e9cef7b1b6d64860f52176a4413b23fc5ccommit-bot@chromium.org    Exception: unable to find the program binary.
47164052e9cef7b1b6d64860f52176a4413b23fc5ccommit-bot@chromium.org  """
48164052e9cef7b1b6d64860f52176a4413b23fc5ccommit-bot@chromium.org  trunk_path = os.path.abspath(os.path.join(os.path.dirname(__file__),
49164052e9cef7b1b6d64860f52176a4413b23fc5ccommit-bot@chromium.org                                            os.pardir))
50164052e9cef7b1b6d64860f52176a4413b23fc5ccommit-bot@chromium.org  possible_paths = [os.path.join(trunk_path, 'out', 'Release', program),
51164052e9cef7b1b6d64860f52176a4413b23fc5ccommit-bot@chromium.org                    os.path.join(trunk_path, 'out', 'Debug', program),
52164052e9cef7b1b6d64860f52176a4413b23fc5ccommit-bot@chromium.org                    os.path.join(trunk_path, 'out', 'Release',
53164052e9cef7b1b6d64860f52176a4413b23fc5ccommit-bot@chromium.org                                 program + '.exe'),
54164052e9cef7b1b6d64860f52176a4413b23fc5ccommit-bot@chromium.org                    os.path.join(trunk_path, 'out', 'Debug',
55164052e9cef7b1b6d64860f52176a4413b23fc5ccommit-bot@chromium.org                                 program + '.exe')]
56164052e9cef7b1b6d64860f52176a4413b23fc5ccommit-bot@chromium.org  for try_path in possible_paths:
57164052e9cef7b1b6d64860f52176a4413b23fc5ccommit-bot@chromium.org    if os.path.isfile(try_path):
58164052e9cef7b1b6d64860f52176a4413b23fc5ccommit-bot@chromium.org      return try_path
59164052e9cef7b1b6d64860f52176a4413b23fc5ccommit-bot@chromium.org  raise Exception('cannot find %s in paths %s; maybe you need to '
60164052e9cef7b1b6d64860f52176a4413b23fc5ccommit-bot@chromium.org                  'build %s?' % (program, possible_paths, program))
61164052e9cef7b1b6d64860f52176a4413b23fc5ccommit-bot@chromium.org
62