File: executable.py

package info (click to toggle)
subversion 1.6.12dfsg-7
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 48,292 kB
  • ctags: 47,714
  • sloc: ansic: 578,414; python: 77,551; sh: 13,100; ruby: 12,194; cpp: 10,097; java: 8,428; lisp: 7,702; perl: 7,320; makefile: 1,035; xml: 759; sql: 62
file content (47 lines) | stat: -rw-r--r-- 1,343 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#
# executable.py -- Utilities for dealing with external executables
#

import os
import subprocess

def exists(file):
  """Is this an executable file?"""
  return os.path.isfile(file) and os.access(file, os.X_OK)

def find(file, dirs=None):
  """Search for an executable in a given list of directories.
     If no directories are given, search according to the PATH
     environment variable."""
  if not dirs:
    dirs = os.environ["PATH"].split(os.pathsep)
  for path in dirs:
    if is_executable(os.path.join(path, file)):
      return os.path.join(path, file)
    elif is_executable(os.path.join(path, "%s.exe" % file)):
      return os.path.join(path, "%s.exe" % file)
  return None

def output(cmd, strip=None):
  """Run a command and collect all output"""
  # Check that cmd is in PATH (otherwise we'd get a generic OSError later)
  import distutils.spawn
  if type(cmd) == type(''):
    cmdname = cmd
  elif type(cmd) == type([]):
    cmdname = cmd[0]
  if distutils.spawn.find_executable(cmdname) is None:
    return None

  # Run it
  (output, empty_stderr) = subprocess.Popen(cmd, stdout=subprocess.PIPE, \
                             stderr=subprocess.STDOUT).communicate()
  if strip:
    return output.strip()
  else:
    return output

def run(cmd):
  """Run a command"""
  exit_code = os.system(cmd)
  assert(not exit_code)