blob: f71a808666b13ce290442e22bb59d1788d36b370 [file] [log] [blame]
commit-bot@chromium.org164052e2014-02-07 18:41:49 +00001#!/usr/bin/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"""Module that finds and runs a binary by looking in the likely locations."""
7
8
9import os
10import subprocess
11import sys
12
13
14def run_command(args):
15 """Runs a program from the command line and returns stdout.
16
17 Args:
18 args: Command line to run, as a list of string parameters. args[0] is the
19 binary to run.
20
21 Returns:
22 stdout from the program, as a single string.
23
24 Raises:
25 Exception: the program exited with a nonzero return code.
26 """
27 proc = subprocess.Popen(args,
28 stdout=subprocess.PIPE,
29 stderr=subprocess.PIPE)
30 (stdout, stderr) = proc.communicate()
31 if proc.returncode is not 0:
32 raise Exception('command "%s" failed: %s' % (args, stderr))
33 return stdout
34
35
36def find_path_to_program(program):
37 """Returns path to an existing program binary.
38
39 Args:
40 program: Basename of the program to find (e.g., 'render_pictures').
41
42 Returns:
43 Absolute path to the program binary, as a string.
44
45 Raises:
46 Exception: unable to find the program binary.
47 """
48 trunk_path = os.path.abspath(os.path.join(os.path.dirname(__file__),
49 os.pardir))
50 possible_paths = [os.path.join(trunk_path, 'out', 'Release', program),
51 os.path.join(trunk_path, 'out', 'Debug', program),
52 os.path.join(trunk_path, 'out', 'Release',
53 program + '.exe'),
54 os.path.join(trunk_path, 'out', 'Debug',
55 program + '.exe')]
56 for try_path in possible_paths:
57 if os.path.isfile(try_path):
58 return try_path
59 raise Exception('cannot find %s in paths %s; maybe you need to '
60 'build %s?' % (program, possible_paths, program))
61