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