#!/usr/bin/env python

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

#usage : ./testProject.py nombinome : nombinome must be a valid binom name (and a directory)


from __future__ import with_statement

import sys
import os
import time
import glob
import filecmp

extList = [".o", ".log",".tgz",".gz",".odt",".doc",".so",".a"]

binomesList = ["ElhasnaouiGloria","ConflandDujardin","ChanoussiKemajou",
"AboulhassanBaiba","ChekkouriLeguennec","LiuHuoWang","AyziGosse",
"HenryKhodr","EttaldiFaddi","Eljalaoui",
"HijaziRealRosener","MairesseSueur","CheminZhangL","RobacheWallet",
"DelbergueZhangP","TeufTsimba","LessieuxSogoba","LinSantos","DequidtGonnord"]


unbinome=sys.argv[1]
print "-------------------------"
print "Analysing "+unbinome
print "-------------------------"
if not(unbinome in binomesList):
    print "not a valid binom name"
    exit(1)
else :
    print "found your name in the list"
    basename=unbinome
    print "Inspecting "+basename
    if (not(os.path.isfile(basename+"/"+basename+".pdf"))):
        print "The report is missing (or has wrong name). abort"
        exit(1)
    else :
        print "Report : found"
        if (not(os.path.isfile(basename+"/Readme.txt"))):
            print "missing Readme.txt (or has wrong name). abort"
            exit(1)
        print "Readme : found"
        print "looking for object files, temp files, in subdirs :",
        #cfiles = []
        for root, dirs, files in os.walk(basename):
            for file in files:
                if file.endswith('~'):
       #             cfiles.append(os.path.join(root, file))
                     print "there is a temp file : "+file+": abort."
                     exit(1)
                if os.path.splitext(file)[1] in extList:
                    print "there is an object file : "+file+": abort."
                    exit(1)
        print ("OK")
        if (not(os.path.isdir(basename+"/Code"))):
            print "Cannot find Code/ directory"
            exit(1)
        print "Code dir : found"
        if (not(os.path.isdir(basename+"/Tests"))):
            print "Cannot find Tests/ directory"
            exit(1)
        print "Test dir : found"
        print "\n**Inspecting Code/ directory**"
        if (not(os.path.isfile(basename+"/Code/Makefile"))):
            print "missing Makefile in Code"
            exit(1)
        r=os.system("cd "+basename+"/Code/ && make clean")
        print "make clean. done"
        print "trying to compile : ",
        r=os.system("cd "+basename+"/Code/ && make")
        if(r!=0):
            print "pb : return value of the script is non 0"
            exit(1) 
        else:
            print "the Makefile has returned successfully "
        if (not(os.path.isfile(basename+"/Code/mandelbrot"))):
            print "no binary mandelbrot found"
            exit(1)
        print "\n**Inspecting Tests/ directory**"
        print "trying to execute : ",
        r=os.system("cd "+basename+"/Tests/ && ./mytests.sh")
        print ("OK")
        print "\nDone. Good job - your project is good looking!"
        exit(0)
