Package npsgd :: Module standalone_task
[hide private]
[frames] | no frames]

Source Code for Module npsgd.standalone_task

 1  # Author: Thomas Dimson [tdimson@gmail.com] 
 2  # Date:   January 2011 
 3  # For distribution details, see LICENSE 
 4  """Module containing abstract base class for standalone models.""" 
 5  import os 
 6  import logging 
 7  import subprocess 
 8  from model_task import ModelTask 
 9  from config import config 
10   
11 -class StandaloneError(RuntimeError): pass
12 -class StandaloneTask(ModelTask):
13 """Abstract base task for standalone models. 14 15 This class is meant to be the superclass of the user's various 16 standalone models. These will generally compiled models, but can include 17 anything that needs to be launched in a subprocess 18 """ 19 20 abstractModel = "StandaloneTask" 21 executable = "ls" 22
23 - def executableParameters(self):
24 """Returns parameters to the underlying executable as a Python list.""" 25 26 return [ 27 "-al", 28 "*" 29 ]
30
31 - def runModel(self):
32 """Spawns a python subprocess of 'executable' class variable and executes. 33 34 This method is meant to run standalone binaries of models. It stores the 35 stdout/stderr in class variables called self.stdout and self.stderr. 36 """ 37 38 exe = self.__class__.executable 39 40 logging.info("Launching subprocess '%s %s'", exe, " ".join(self.executableParameters())) 41 mProcess = subprocess.Popen([exe] + self.executableParameters(), 42 cwd=self.workingDirectory, stdout=subprocess.PIPE, stderr=subprocess.PIPE) 43 self.stdout, self.stderr = mProcess.communicate() 44 logging.info("Stdout was: --------\n%s\n-----", self.stdout) 45 logging.info("Stderr was: --------\n%s\n-----", self.stderr) 46 47 if mProcess.returncode != 0: 48 raise ExecutableError("Bad return code '%s' from '%s'" % (mProcess.returnCode, exe)) 49 50 logging.info("Subprocess all done")
51