Make a status test pass against old servers.
[svn.git] / build / generator / util / executable.py
blob2de0c6475a077bc2743a45619c00f411c76a9da4
2 # executable.py -- Utilities for dealing with external executables
5 import os, string
7 def exists(file):
8 """Is this an executable file?"""
9 return os.path.isfile(file) and os.access(file, os.X_OK)
11 def find(file, dirs=None):
12 """Search for an executable in a given list of directories.
13 If no directories are given, search according to the PATH
14 environment variable."""
15 if not dirs:
16 dirs = string.split(os.environ["PATH"], os.pathsep)
17 for path in dirs:
18 if is_executable(os.path.join(path, file)):
19 return os.path.join(path, file)
20 elif is_executable(os.path.join(path, "%s.exe" % file)):
21 return os.path.join(path, "%s.exe" % file)
22 return None
24 def output(cmd, strip=None):
25 """Run a command and collect all output"""
26 try:
27 # Python 2.x
28 stdin, stdout = os.popen4(cmd)
29 assert(not stdin.close())
30 except AttributeError:
31 try:
32 # Python 1.x on Unix
33 import posix
34 stdout = posix.popen('%s 2>&1' % cmd)
35 except ImportError:
36 # Python 1.x on Windows (no cygwin)
37 # There's no easy way to collect output from stderr, so we'll
38 # just collect stdout.
39 stdout = os.popen(cmd)
40 output = stdout.read()
41 assert(not stdout.close())
42 if strip:
43 return string.strip(output)
44 else:
45 return output
47 def run(cmd):
48 """Run a command"""
49 exit_code = os.system(cmd)
50 assert(not exit_code)