find_run_binary.py 1.9 KB

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