#! /usr/bin/env python
##**************************************************************
##
## Copyright (C) 1990-2008, Condor Team, Computer Sciences Department,
## University of Wisconsin-Madison, WI.
## 
## Licensed under the Apache License, Version 2.0 (the "License"); you
## may not use this file except in compliance with the License.  You may
## obtain a copy of the License at
## 
##    http://www.apache.org/licenses/LICENSE-2.0
## 
## Unless required by applicable law or agreed to in writing, software
## distributed under the License is distributed on an "AS IS" BASIS,
## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
## See the License for the specific language governing permissions and
## limitations under the License.
##
##**************************************************************

import re
import os
import sys
import time
import signal
from optparse import *
from condor_log_reader import *

class EventLogVerifier( EventLogParser ) :
    def __init__( self, filename ) :
        EventLogParser.__init__( self, filename )
        self._errors = []

    def ProcessEvent( self, event ) :
        try :
            event.Verify( 1 )
        except EventVerifyException, e :
            self._errors.append( e )
            if self.Verbose() :
                print >>sys.stderr, e

    def HandleParseError( self, s ) :
        self._errors.append( s )
        if self.Verbose() :
            print >>sys.stderr, s

    def PrintSummary( self ) :
        print "Summary:",self.GetNumEvents(),"events processed:"
        names = self.GetEventNames()
        for name in sorted(names) :
            print "  %s: %d" % ( name, self.GetNumEvents(name) )
        if len(self._errors) :
            print "Summary:",len(self._errors),"errors detected:"
            for e in self._errors :
                print e
            

class Verify ( object ) :

    def __init__( self ) :
        self.Options( )

        signal.signal(signal.SIGTERM, self.SigHandler)
        signal.signal(signal.SIGINT,  self.SigHandler)
        signal.signal(signal.SIGQUIT, self.SigHandler)

    # Install a signal handler
    def SigHandler(self, signum, frame):
        sys.exit(1)

    def Options( self ) :
        usage = "usage: %prog [options] file [...]"
        version = "0.1"
        p = OptionParser( usage = usage,
                          version = "%prog "+version )

        # Quiet & verbose options
        p.set_defaults( verbose=0 )
        p.set_defaults( quiet=False )
        def VerboseCallback(option, opt, value, parser):
            parser.values.quiet = False
            parser.values.verbose += 1
        p.add_option( "-v", "--verbose",
                      action="callback", callback=VerboseCallback,
                      help="Verbose output (increment verbose level)" )
        def QuietCallback(option, opt, value, parser):
            parser.values.quiet = True
            parser.values.verbose = 0
        p.add_option( "-q", "--quiet",
                      action="callback", callback=QuietCallback,
                      help="be vewwy quiet (I'm hunting wabbits)" )

        
        (self._options, self._files) = p.parse_args()
        if len(self._files) == 0 :
            p.error( "No files specified" )


    def IsVerbose( self, level = 1 ) :
        return self._options.verbose >= level
    def IsQuiet( self ) :
        return self._options.quiet

    def VerifyFiles( self ) :
        for f in self._files :
            self.VerifyFile( f )

    def VerifyFile( self, filename ) :
        if self.IsVerbose( 1 ) :
            print "Verifying file", filename
        try:
            verifier = EventLogVerifier( filename )
        except Exception, e:
            print >>sys.stderr, "Failed to initialize log verifier: ", e
            return False
        verifier.Verbose( self._options.verbose )
        verifier.Quiet( self._options.quiet )
        verifier.ParseLog( False )
        verifier.PrintSummary( )


main = Verify( )
main.VerifyFiles()

### Local Variables: ***
### py-indent-offset:4 ***
### python-indent:4 ***
### python-continuation-offset:4 ***
### tab-width:4  ***
### End: ***
