#!/usr/bin/env python
#============================================================================
#
#     This file is part of the Code_Saturne Kernel, element of the
#     Code_Saturne CFD tool.
#
#     Copyright (C) 1998-2008 EDF S.A., France
#
#     contact: saturne-support@edf.fr
#
#     The Code_Saturne Kernel is free software; you can redistribute it
#     and/or modify it under the terms of the GNU General Public License
#     as published by the Free Software Foundation; either version 2 of
#     the License, or (at your option) any later version.
#
#     The Code_Saturne Kernel is distributed in the hope that it will be
#     useful, but WITHOUT ANY WARRANTY; without even the implied warranty
#     of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#     GNU General Public License for more details.
#
#     You should have received a copy of the GNU General Public License
#     along with the Code_Saturne Kernel; if not, write to the
#     Free Software Foundation, Inc.,
#     51 Franklin St, Fifth Floor,
#     Boston, MA  02110-1301  USA
#
#============================================================================
#
#-------------------------------------------------------------------------------
# Library modules import
#-------------------------------------------------------------------------------

import sys, os

#-------------------------------------------------------------------------------
# Application modules import
#-------------------------------------------------------------------------------

import Autovalidation.CommandLine as CommandLine
import Autovalidation.Parser as Parser
import Autovalidation.Study as Study
import Autovalidation.Case as Case
import Autovalidation.Common as Common


def runAutovalid():

    #
    # Command line
    if len(sys.argv) <= 1 :
        print CommandLine.usage()
        sys.exit(1)
        
    CommandLine.process_cmd_line(sys.argv[1:])

    #
    # Parser
    parser = Parser.Parser(Common.XMLFileName)

    #
    # Global variables
    Common.referenceVersion = parser.getReferenceVersion()
    Common.referencePath = parser.getReferencePath()
    Common.localDirectory = os.getcwd()

    #
    # Print
    print "\n Code_Saturne autovalidation"
    print " ---------------------------"
    print "     -  Version         : " + Common.referenceVersion
    print "     -  Reference path  : " + Common.referencePath
    print "     -  Tmp directory   : " + Common.tmpDirectory
    print "     -  Local directory : " + Common.localDirectory
    print "\n"
    #
    # studies labels
    studiesLabels = parser.getStudiesLabels()

    #
    # Results file opening
    reportFile =  open("report.txt", mode='w')
    reportFile.write('\n')
    reportFile.write('---------------\n')
    reportFile.write('Report file    \n')
    reportFile.write('---------------\n')
    
    #
    # Studies treatment
    if studiesLabels != None :
        for studyLabel in studiesLabels:
            #
            # study create
            print '   Study: ' + studyLabel
            reportFile.write('   Study: ' + studyLabel +'\n')
            study = Study.Study(parser, studyLabel)
            #
            # case treatment
            cases = study.getCases()
            for case in cases:
                label = case.getCaseLabel()
                print '      Case: ' + label
                
                runState = case.run()
                #
                # compare listing
                if runState != "Compilation error" and runState != "Execution error":
                    listState1, listState2 = case.listingCompare()
                    #
                    # compare chr
                    chrState = case.chrCompare() 

                if runState != "OK":
                    valid = runState
                else:
                    valid = "OK"
                    if listState1 != "OK" or listState2 != "OK" or chrState != "OK":
                        valid = "NOK"
                if len(label) > 15:
                    reportFile.write('     Case:  %15s  %17s\n'  % (label[:15], valid))
                else :
                    reportFile.write('     Case:  %15s  %17s\n'  % (label, valid))

            reportFile.write('\n')
            
    reportFile.close()
    
if __name__ == '__main__':
	runAutovalid()

