Chad Burrus avatar Chad Burrus committed 97b99b2

added system command runner class -- only tested on Linux

Comments (0)

Files changed (1)

+from subprocess import call
+
+class Runner(object):
+	"""
+		A class to simplify running external programs.  You'll want to extend this
+		class and implement __init__, handle_ret_code, and kickoff, at minimum, for
+		each general command you want to use.
+	"""
+
+	def _run(self, command = [], stdin = None, stdout = None, shell = False):
+		"""
+			Don't call directly!  Use kickoff.
+			Wrapper around subprocess.call. See
+			http://docs.python.org/library/subprocess.html for full details.
+		"""
+		ret_code = call(command, stdin=stdin, stdout=stdout, shell=shell)
+		if ret_code:
+			self.handle_ret_code(ret_code, command, stdin, stdout, shell)
+
+	def handle_ret_code(self, ret_code, command, stdin, stdout, shell):
+		"""
+			Exception handler in case something breaks running an external command.
+			Should be overridden in child classes (I.e., in Java this would be
+			"abstract").  Parameters are as in self.run, with the exception of the
+			first parameter, which is the actual return code from the command
+			execution.
+		"""
+
+		raise RuntimeError("Command Failed!!!")
+
+	def super_me(self):
+		""" A shortcut method to call super on me. """
+		return super(type(self), self)
+
+	def kickoff(self, command = [], stdin = None, stdout = None, shell = False):
+		"""
+			Actually kick off a run of the command.  Use this for interface
+			consistency.
+		"""
+		self._run(command, stdin, stdout, shell)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.