#!/usr/bin/env python

#Python Script for checking C projects
#Laure Gonnord for IMA 3 PS - dec 2011

#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", ".tgz",".gz",".odt",".doc",".so",".a"]

binomesList = ["Burtaire_Gouenard","Amegavie_Lelaure","Bonvalet_Champagne","Boudjema_Mahir","Chretien_Gombault","Dugauquier_Moigner","Kourouma_Pesqueux","Loemblet_Theon","Mombo_Pons","Petiprez_Rodriguez","Ribreau_Yongoua","Aljane_Xi","Bangoura_Fei","Fouaide_Li","Hamdane_Liu","Imghoure_Saadane","Mabrouk_Taieb","Mboup_Vandermeersch","Romerowski_Tahry","Rukata_Wang","Vincenti_Xing","Alexandre_Mairesse","Chalaux_Husse","Debie_Monteiro","Hossie_Kemajou","Lambert_Lenormand","Leuliet_Monier","Dequidt_Gonnord"]

def check_binfiles () :
    print basename
    print "Looking for object files, temp files, in subdirs :",
    for root, dirs, files in os.walk(basename):
        for file in files:
            if file.endswith('~'):
                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 ("no such files, good job !")
    return

def check_dirs () : 
    if (not(os.path.isdir(basename+"/Code"))):
        print "Cannot find Code/ directory"
        exit(1)
    if (os.path.isdir(basename+"/LibFournie")):
        print "LibFournie/ directory should not be there !"
        exit(1)
    else:
        if (not(os.path.isdir(basename+"/Images"))):
            print "Cannot find Images/ directory"
            exit(1)
        else:
            if (not(os.path.isdir(basename+"/Images/In"))):
                print "Cannot find Images/In directory"
                exit(1)
            else:
                if (not(os.path.isdir(basename+"/Images/Out"))):
                    print "Cannot find Images/Out directory"
                    exit(1)
                else:
#check if this directory is empty or not   
                    for root, dirs, files in os.walk(basename+"/Images/Out"):
                        if files : 
                            print "Dir Images/Out should be empty !"
                            exit(1)
                    if (not(os.path.isdir(basename+"/Images/Base"))):
                        print "Cannot find Images/Base directory"
                        exit(1)
                    else :
                        print "All images directories : found"
                        return


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"
        check_binfiles () 
        print "\n**Inspecting subdirectories**"
        check_dirs()
        print "\n**Inspecting Code/ directory for Makefile**"
        if (not(os.path.isfile(basename+"/Code/Makefile"))):
            print "missing Makefile in Code"
            exit(1)
        print "found !\n\n**Now trying to compile : **"
        r=os.system("cd "+basename+"/Code/ && make clean")
        print "make clean. done"
        r=os.system("cd "+basename+"/Code/ && make")
        if(r!=0):
            print "pb : there has been a problem while compiling your project"
            exit(1) 
        else:
            print "--> the Makefile has returned successfully "
        if (not(os.path.isfile(basename+"/Code/mosaique"))):
            print "no binary mosaique found"
            exit(1)
        print "\n**Now trying to execute mosaique**"
        r=os.system("cd "+basename+"/Code/ && ./mosaique")
        if(r!=0):
            print "pb : there has been a problem while executing your project"
            exit(1) 
        else:
            print "--> Done. Good job - your project is good looking!"
            print "Make clean"
            r=os.system("cd "+basename+"/Code/ && make clean")
            print "\n** To create the archive, type the following command: "
            print "tar cvzf "+basename+".tgz --exclude-vcs "+basename
            exit(0)
