#!/usr/bin/python
import shutil
import os, sys
from os import *
from sys import *
import time 

SERVER='nfs2_gb'
exportPath='/export'
mountPath='mnt/'

class Machine:
    def mkdir(self,dir):
            self.command="mkdir -p "+dir
            self.do()
    def rmdir(self,dir):
            self.command="rm -rf "+dir
            self.do()

    def printc(self):
            print "->"+self.command
            print "\n"

class Client(Machine):
    
    def __init__(self, mountPath):
        self.command=""
        self.mountPath=mountPath

    def do(self):
            os.system(self.command)

    def isomount(self, dir):
        export=SERVER+":/"+dir
        mntpoint=self.mountPath+"/"+dir
        self.command="mount -t nfs4 "+export+" "+mntpoint
        self.do()
    def mount(self, remoteDir):
        export=SERVER+":/"+remoteDir
        mntpoint=self.mountPath
        self.command="mount -t nfs4 "+export+" "+mntpoint
        self.do()
    
    def umount(self, dir):
        mntpoint=self.mountPath+"/"+dir
        self.command="umount "+mntpoint
        self.do()
        





class Serveur(Machine):

    def __init__(self, ip, exportPath):
            self.SERVEUR=ip
            self.exportPath=exportPath
            
    def do(self):
            self.command="ssh "+self.SERVEUR+" "+self.command
            os.system(self.command)

    def configure(self, dir):
            exportDir=self.exportPath+'/'+dir
            self.exportDir=exportDir
            self. mkdir(exportDir)
            self.export(exportDir)
    def clean(self):
            unexportDir=self.exportDir
            self.unexport(unexportDir)
            self.rmdir(unexportDir)

    def unexport(self, dir):
            exportOptions="-u "
            allowedUsers="*"
            self.command="exportfs "+exportOptions+allowedUsers+":"+dir
            self.do()




    def export(self, dir):
            exportOptions="-orw,nohide,insecure,no_subtree_check "
            allowedUsers="*"
            self.command="exportfs "+exportOptions+allowedUsers+":"+dir
            self.do()


            


           


class TestFiles:
        #Prepare test
        def setup_environement(self):
            dir="max_file_test"

            self.client=Client(mountPath)
            self.serveur=Serveur("nfs2_gb", "/export_nfs")
            self.serveur.configure(dir)
            self.client.mount(dir)
            self.path=mountPath+dir+"/"
            self.client.mkdir(self.path)

            
        def __init__(self, result):
                #We direclty open the result file
                self.fichier=file(result, "w")
                self.setup_environement()
                #We will perform a measure each 1000 files
                self.base=10000 
                #number of files when starting the test
                self.n=0

        def create_files(self, i):
                j=0
                filename=""
                
                while j<self.base:
                    #I play a little with decimal numerotation for naming ;)
                    n=self.base*i+j
                    filename=self.path+str(n)
                    #Open with O_CREATE only
                    fd=open(filename, O_CREAT)
                    close(fd)   
                    j+=1
                #save the number of files
                self.n=n
                return 0
                

        
                
#########################################################                
#The idea is to generate 
#1/ Empty files
#2/ stat them
#3/ Measure the time to perform the stat
#4/ Repeat the operation
#########################################################       
        def start(self, max):
            i=0
            while self.base*i<max:
                if(1):
                try:
                    self.create_files(i)
                    #mesure time to stat all the files
                    ls_t0=time.time()
                    ls=listdir(self.path)
                    ls_t1=time.time()
                except:
                        break
                
                self.fichier.write("Time to list "+str(self.n)+" files: "+str(ls_t1-ls_t0)+" seconds ")
                self.fichier.write("\n")
                #flush data ... 
                self.fichier.flush()
                i+=1
            self.fichier.close()
            print "Number of files created "+str(self.n)
            print "-----------------------------------------"



        def clean(self,max):
        #We ignore max and destroy all files.
                self.client.rmdir(self.path)
                self.clean_environement()

        def clean_environement(self):
            self.client.umount("")
            self.serveur.clean()


 
def usage():
        print "test maximum mount available for NFSv4"
        print "-h, --help   :this message"
        print "max=<value>  :maximum exported filesystem to test"
        print "--clean      :clean tests only"
        print "--test       :test only"
        print " "
        print "send bugs to Vincent ROQUETA: vincent.roqueta@ext.bull.net"



def main():
        global fichier
        args=sys.argv[1:]
        max=1024
        test=True
        clean=True
        for a in args:
            if a in ("-h", "--help"):
                usage()
                sys.exit(1)
            if a[:3]=="max":
                v=a.split("=")
                max=int(v[1])
                continue
            if a=="--clean":
                test=False
                continue
            if a=="--test":
                clean=False
                continue

        t=TestFiles("test_files")

        if(test):
            t.start(max)
            print "test completed"
        if(clean):
            t.clean(max)
            print "Cleanup completed"
                



main()

