IMP  2.0.1
The Integrative Modeling Platform
subproc.py
1 import sys
2 try:
3  import subprocess
4 except ImportError:
5  # Work with Python 2.3, which doesn't ship with subprocess
6  from IMP.parallel import compat_subprocess as subprocess
7 
8 class _Popen4(subprocess.Popen):
9  """Utility class to provide a portable way to spawn a child process and
10  communicate with its stdin and combined stdout/stderr."""
11 
12  def __init__(self, cmd):
13  # shell isn't needed on Win32, and may not be found under wine anyway
14  shell = (sys.platform != "win32")
15  subprocess.Popen.__init__(self, cmd, shell=shell, stdin=subprocess.PIPE,
16  stdout=subprocess.PIPE,
17  stderr=subprocess.STDOUT)
18 
19  def require_clean_exit(self):
20  """Make sure the child exited with a zero return code"""
21  r = self.wait()
22  if r != 0:
23  raise IOError("Process failed with exit status %d" % r)
24 
25 if sys.platform == 'win32':
26  def _run_background(cmdline, out):
27  """Run a process in the background and direct its output to a file"""
28  print "%s > %s" % (cmdline, out)
29  try:
30  # shell isn't needed on Win32, and may not be found under wine
31  # anyway
32  p = subprocess.Popen(cmdline, shell=False, stdout=open(out, 'w'),
33  stderr=subprocess.STDOUT)
34  # Ignore Windows "file not found" errors, so that behavior is consistent
35  # between Unix and Windows
36  except WindowsError, detail:
37  print("WindowsError: %s (ignored)" % detail)
38 
39 else:
40 
41  def _run_background(cmdline, out):
42  """Run a process in the background and direct its output to a file"""
43  print "%s > %s" % (cmdline, out)
44  subprocess.Popen(cmdline, shell=True, stdout=open(out, 'w'),
45  stderr=subprocess.STDOUT)