#!/bin/env python
# -*- PYTHON -*-

#-----------------------------------------------------------------------------#
#		       Copyright (c) John F. Croix, 1997
#
#			      All Rights Reserved
#
# Permission to use, copy, modify, and distribute this software and its
# documentation for any non-commercial or internal-use-only purpose and without
# fee is hereby granted, provided that the above copyright notice appears in
# all copies and that both the copyright notice and this permission notice
# appear in supporting documentation, and all changes or alterations to the
# software and documentation are clearly documented.  Resale of any part of
# this software or documentation is prohibited without a license.  Licenses may
# be obtained by contacting the author, John F. Croix, at John.Croix@amd.com or
# journeyman@mail.utexas.edu.
#
# THE AUTHOR, JOHN F. CROIX, DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
# SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, INDIRECT OR
# CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
# DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
# TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OF PERFORMANCE
# OF THIS SOFTWARE.
#-----------------------------------------------------------------------------#

#-----------------------------------------------------------------------------#
# Test the Python Pipe module.  Make sure that timeouts can be found,       #
# resolved, reset, etc.                                                       #
#-----------------------------------------------------------------------------#


import pipe
import sys


class ExitTimeout(pipe.TIMEOUT):
    def __init__(Self):
	pipe.TIMEOUT.__init__(Self)

    def Apply(Self, PipeProcess, BeforeMatch, MatchingText):
	pipe.TIMEOUT.Apply( Self, PipeProcess, BeforeMatch, MatchingText )
	print "Process has timed out!  Exiting."
	sys.exit( 1 )


class FoundTimeout(pipe.TIMEOUT):
    def __init__(Self):
	pipe.TIMEOUT.__init__(Self)

    def Apply(Self, PipeProcess, BeforeMatch, MatchingText):
	pipe.TIMEOUT.Apply( Self, PipeProcess, BeforeMatch, MatchingText )
	print "\n\n\nTimeout was discovered.  Resetting ... ",
	PipeProcess.PipeProcess.ResetTimeoutTimer()
	PipeProcess.PipeProcess.ResetError()
	print "Done\n\n"


#
# Start DC Shell.  Wait for it to initialize.
#


LS = pipe.Pipe()
LS.MatchAfter( ExitTimeout() )
#LS.LogUser( 1 )
LS.SetTimeout( 3 )
print 'Starting "/bin/sh" process....'
LS.Open( "/bin/sh" )

NoSuchPattern = pipe.RegexPattern( "There should be no file with this name" )
SimpleTimeout = FoundTimeout()
LS.Match( NoSuchPattern, SimpleTimeout )
print "OK if we just got a timeout message above.  Now wait a few seconds...."
LS.Match( NoSuchPattern )
print "ERROR --> We should never reach this point."
