#!/usr/bin/env python

#Python Script for checking C projects
#Laure Gonnord for IMA 3

#usage : ./testone.py filename.tgz : filename must be a valid binom name


from __future__ import with_statement

import sys
import os
import time
import glob
import filecmp

binomesList = ["AboudlhassanConfland","BouaibaHuo","ChanoussiFaddi","ChekkouriWang",
"HenryKhodr","EljalaouiKemajou","AkhasnaouiLeguennec","GloriaGosse","DujardinLiu",
"CheminSogoba","MairesseSueur","AbdoullahRosener","TeufWallet","AyziLessieux",
"DelbergueZhangp","EttaldiLin","RealZhangl","SantosTsimba","RachenneRobache","DequidtGonnord"]


CfileName=sys.argv[1]
print "-------------------------"
print "Analysing "+CfileName
print "-------------------------"
basename=CfileName.split('.')[0]
if not(basename in binomesList):
    print "not a valid binom name"
    exit(1)
else :
    print "found your name in the list"
typefile=os.system("file " + CfileName + "> "+ basename + ".log")
if(typefile!=0):
    print "pb with your TGZ"
    exit(1) 
else :
    os.system("rm -Rf "+basename)
    print "Dezipping",
    with open(basename+".log",'r') as f:
        lines=f.readlines()
        try : 
            ifzip= [i for i in lines if(i.find("gzip") !=-1)][0]
            os.system("tar xzf " + CfileName)
            os.system("rm -f "+basename+".log")
        except IndexError:
            print CfileName+" is not a valid gzip file"
            os.system("rm -f "+basename+".log")
            exit(1)
    print "... done."
    if (not(os.path.isdir(basename))):
        print "Cannot find "+basename+" directory"
        exit(1)
    else: # le coeur du sujet
        extList = [".o", ".log"]
        print "Directory with the right name, now inspecting"
        if (not(os.path.isfile(basename+"/rapport"+basename+".pdf"))):
            print "missing pdf. abort"
            exit(1)
        if (not(os.path.isfile(basename+"/ReadMe.txt"))):
            print "missing ReadMe.txt. abort"
            exit(1)
        if (not(os.path.isdir(basename+"/codes"))):
            print "Cannot find codes/ directory"
            exit(1)
        if (not(os.path.isdir(basename+"/tests"))):
            print "Cannot find tests/ directory"
            exit(1)
        print("pdf, readme, codes/ and tests/ are ok")
        print "\n**Inspecting codes/ directory**"
        if (not(os.path.isfile(basename+"/codes/compilall.sh"))):
            print "missing compilall.sh script."
            exit(1)
        print "looking for object files, temp files, ... :",
        for elem in glob.glob(basename+"/codes/*~"):
            print "there  still exists some temp files !"
            exit(1)
        for i in os.listdir(basename+"/codes"):
            if os.path.splitext(i)[1] in extList:
                print "there still exists some object files !"
                exit(1)
        print ("OK")
        print "checking indentation"
        for elem in glob.glob(basename+"/codes/*.c"):
            print "cheking indent for "+elem+":",
            r=os.system("indent -kr -o toto.c "+elem);
            if(r!=0):
                print "pb with indent"
                exit(1)
            if filecmp.cmp('toto.c', elem):
                print "OK."
            else:
                print "wrong indent!"
                exit(1)
        for elem in glob.glob(basename+"/codes/*.h"):
            print "cheking indent for "+elem+":",
            r=os.system("indent -kr -o toto.h "+elem);
            if(r!=0):
                print "pb with indent"
                exit(1)
            if filecmp.cmp('toto.h', elem):
                print "OK."
            else:
                print "wrong indent!"
                exit(1)
        print "trying to compile : ",
        r=os.system("cd "+basename+"/codes/ && ./compilall.sh")
        if(r!=0):
            print "pb : return value of the script is non 0"
            exit(1) 
        else:
            print "the script has returned successfully "
        if (not(os.path.isfile(basename+"/codes/jolimage"))):
            print "no binary jolimage found"
            exit(1)
        r=os.system("cd "+basename+"/codes/ && ./jolimage")
        if(r!=0):
             print "pb with the execution of jolimage"
             exit(1)
        print "\n**Inspecting tests/ directory**"
        if (not(os.path.isfile(basename+"/tests/testall.sh"))):
            print "missing testall.sh script."
            exit(1)  
        print "looking for object files, temp files, ... :",
        for elem in glob.glob(basename+"/tests/*~"):
            print "there  still exists some temp files !"
            exit(1)
        for i in os.listdir(basename+"/tests"):
            if os.path.splitext(i)[1] in extList:
                print "there still exists some temporary files !"
                exit(1)
        print ("OK")
        print "trying to test : ",
        r=os.system("cd "+basename+"/tests/ && ./testall.sh")
        if(r!=0):
            print "pb : return value of the script is non 0"
            exit(1) 
        else:
            print "the script has returned successfully "
        print "\nDone. Good job - Your archive is correct. "
        os.system("rm toto.c toto.h -f")
        exit(0)
