PageRenderTime 50ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/tut2/Runner.py

https://bitbucket.org/chadburrus/rabbitmq-tutorials
Python | 34 lines | 22 code | 2 blank | 10 comment | 2 complexity | 3aa5146f98b5a5d0aa7775e01aae54bd MD5 | raw file
  1. from subprocess import call
  2. class Runner(object):
  3. """ A class to simplify running external programs. """
  4. def _run(self, command = [], stdin = None, stdout = None, shell = False):
  5. """
  6. Don't call directly! Use kickoff.
  7. Wrapper around subprocess.call. See
  8. http://docs.python.org/library/subprocess.html for full details.
  9. """
  10. ret_code = call(command, stdin=stdin, stdout=stdout, shell=shell)
  11. if ret_code:
  12. self.handle_ret_code(ret_code, command, stdin, stdout, shell)
  13. def handle_ret_code(self, ret_code, command, stdin, stdout, shell):
  14. """
  15. Exception handler in case something breaks running an external command.
  16. Should be overridden in child classes. Parameters are as in self.run,
  17. with the exception of the first parameter, which is the actual return code
  18. from the command execution.
  19. """
  20. raise RuntimeError("Command Failed!!!")
  21. def super_me(self):
  22. """ A shortcut method to call super on me. """
  23. return super(type(self), self)
  24. def kickoff(self, command = [], stdin = None, stdout = None, shell = False):
  25. """
  26. Actually kick off a run of the command. Use this for interface
  27. consistency.
  28. """
  29. self._run(command, stdin, stdout, shell)