/ Published in: Python
                    
                                        
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.
                
                            
                                Expand |
                                Embed | Plain Text
                            
                        
                        Copy this code and paste it in your HTML
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
Comments
 Subscribe to comments
                    Subscribe to comments
                
                