IMP  2.2.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 
11 class _Popen4(subprocess.Popen):
12 
13  """Utility class to provide a portable way to spawn a child process and
14  communicate with its stdin and combined stdout/stderr."""
15 
16  def __init__(self, cmd):
17  # shell isn't needed on Win32, and may not be found under wine anyway
18  shell = (sys.platform != "win32")
19  subprocess.Popen.__init__(
20  self, cmd, shell=shell, stdin=subprocess.PIPE,
21  stdout=subprocess.PIPE,
22  stderr=subprocess.STDOUT)
23 
24  def require_clean_exit(self):
25  """Make sure the child exited with a zero return code"""
26  r = self.wait()
27  if r != 0:
28  raise IOError("Process failed with exit status %d" % r)
29 
30 if sys.platform == 'win32':
31  def _run_background(cmdline, out):
32  """Run a process in the background and direct its output to a file"""
33  print "%s > %s" % (cmdline, out)
34  try:
35  # shell isn't needed on Win32, and may not be found under wine
36  # anyway
37  p = subprocess.Popen(cmdline, shell=False, stdout=open(out, 'w'),
38  stderr=subprocess.STDOUT)
39  # Ignore Windows "file not found" errors, so that behavior is consistent
40  # between Unix and Windows
41  except WindowsError, detail:
42  print("WindowsError: %s (ignored)" % detail)
43 
44 else:
45 
46  def _run_background(cmdline, out):
47  """Run a process in the background and direct its output to a file"""
48  print "%s > %s" % (cmdline, out)
49  subprocess.Popen(cmdline, shell=True, stdout=open(out, 'w'),
50  stderr=subprocess.STDOUT)
See IMP.parallel for more information.