IMP  2.3.1
The Integrative Modeling Platform
subproc.py
1 """@namespace IMP.parallel.subproc Subprocess handling."""
2 
3 import sys
4 import subprocess
5 
6 
7 class _Popen4(subprocess.Popen):
8 
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__(
16  self, cmd, shell=shell, stdin=subprocess.PIPE,
17  stdout=subprocess.PIPE,
18  stderr=subprocess.STDOUT)
19 
20  def require_clean_exit(self):
21  """Make sure the child exited with a zero return code"""
22  r = self.wait()
23  if r != 0:
24  raise IOError("Process failed with exit status %d" % r)
25 
26 if sys.platform == 'win32':
27  def _run_background(cmdline, out):
28  """Run a process in the background and direct its output to a file"""
29  print "%s > %s" % (cmdline, out)
30  try:
31  # shell isn't needed on Win32, and may not be found under wine
32  # anyway
33  p = subprocess.Popen(cmdline, shell=False, stdout=open(out, 'w'),
34  stderr=subprocess.STDOUT)
35  # Ignore Windows "file not found" errors, so that behavior is consistent
36  # between Unix and Windows
37  except WindowsError as detail:
38  print("WindowsError: %s (ignored)" % detail)
39 
40 else:
41 
42  def _run_background(cmdline, out):
43  """Run a process in the background and direct its output to a file"""
44  print "%s > %s" % (cmdline, out)
45  subprocess.Popen(cmdline, shell=True, stdout=open(out, 'w'),
46  stderr=subprocess.STDOUT)