Return to Snippet

Revision: 5839
at April 7, 2008 15:13 by kangell


Updated Code
from popen2 import popen4
from types import ListType

class RUNError:

	def __init__( self, cmd, expect, result, nomatch=None ):
		self.cmd = cmd
		self.expect = expect
		self.result = result
		self.nomatch = nomatch
		if self.nomatch is None:
			self.nomatch = []
		
	def __repr__( self ):
		
		expect = self.expect
		if len( self.nomatch ):
			expect = self.nomatch 
			
		return """Ran '%s'; expected '%s', got '%s'""" % ( self.cmd,
			str( expect ), self.result )

def run( command, expect=None, matchall=False ):
	"""Run the specified command. If expect is not None then
	look for the given text in the output. If it is not there
	we throw an error. expect can also be a list of strings. In
	that case any match results in success. Unless matchall=True"""

	stdout,stdin = popen4( command )
	result = stdout.read()
	result = result.strip()
	
	nomatch = []
	if not expect is None:
		if len( expect ):
			if not type( expect ) is ListType:
				expect = [ expect ]
			
			found = False
			for s in expect:
				if result.find( s ) > -1:
					found = True
					if not matchall:
						break
				else:
					nomatch.append( s )
					
			if not found:
				raise RUNError( command, expect, result, nomatch )
		else:
			if len( result ):
				raise RUNError( command, "<empty string>", result )

		if matchall and (type( expect ) is ListType) and ( len( nomatch ) ):
			raise RUNError( command, expect, result, nomatch )
			
	return result

Revision: 5838
at April 7, 2008 14:24 by kangell


Initial Code
from popen2 import popen4
from types import ListType

class RUNError:

	def __init__( self, cmd, expect, result ):
		self.cmd = cmd
		self.expect = expect
		self.result = result
		
	def __repr__( self ):
		return """Ran '%s'; expected '%s', got '%s'""" % ( self.cmd,
			str( self.expect ), self.result )

def run( command, expect=None, matchall=False ):
	"""Run the specified command. If expect is not None then
	look for the given text in the output. If it is not there
	we throw an error. expect can also be a list of strings. In
	that case any match results in success. Unless matchall=True"""

	stdout,stdin = popen4( command )
	result = stdout.read()
	result = result.strip()
	
	nomatch = []
	if not expect is None:
		if len( expect ):
			if not type( expect ) is ListType:
				expect = [ expect ]
			
			found = False
			for s in expect:
				if result.find( s ) > -1:
					found = True
					if not matchall:
						break
				else:
					nomatch.append( s )
					
			if not found:
				raise RUNError( command, expect, result )
		else:
			if len( result ):
				raise RUNError( command, expect, result )

		if matchall and (type( expect ) is ListType) and ( len( nomatch ) ):
			raise RUNError( command, expect, result )
			
	return result

Initial URL

                                

Initial Description
Run the specified command. If expect is not None then look for the given text in the output. If it is not there we throw an error. expect can also be a list of strings. In that case any match results in success unless matchall=True.

Initial Title
Run OS Command w/ Expected Output

Initial Tags
command

Initial Language
Python