import os
import stat
import subprocess
import md5
import time
import exceptions

indexdir="/ghostcache/glusterclient/testdir/"
inputdirs=[
	  # "/ghostcache/glusterclient/input/",
          # "/ghostcache/glusterclient/input1/",
          # "/ghostcache/glusterclient/input2/",
          # "/ghostcache/glusterclient/input3/",
          # "/ghostcache/glusterclient/input4/",
          # "/ghostcache/glusterclient/input5/",
          # "/ghostcache/glusterclient/input6/",
          # "/ghostcache/glusterclient/input7/",
          # "/ghostcache/glusterclient/input8/",
          # "/ghostcache/glusterclient/input9/",
          # "/ghostcache/glusterclient/input10/",
           "/ghostcache/glusterclient/input11/",
           "/ghostcache/glusterclient/input12/",
           "/ghostcache/glusterclient/input13/",
           "/ghostcache/glusterclient/input14/",
           "/ghostcache/glusterclient/input15/",
           "/ghostcache/glusterclient/input16/",
           "/ghostcache/glusterclient/input17/",
           "/ghostcache/glusterclient/input18/",
           "/ghostcache/glusterclient/input19/",
           "/ghostcache/glusterclient/input20/",
           "/ghostcache/glusterclient/input21/",
           "/ghostcache/glusterclient/input22/",
           "/ghostcache/glusterclient/input23/",
           "/ghostcache/glusterclient/input24/"]

def getstat(filepath):
    return os.stat(filepath)

def getmd5sum(path):
    f = open(path,'rb')
    m = md5.new()
    while True:  
       data = f.read(8096)
       if(not data):
          break
       m.update(data)
    f.close()
    return m.hexdigest()

def listdir(path):
    
    cmd = "ls -rt " + path
    filelist = [] 
    process = subprocess.Popen(cmd, shell=True,stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
    
    for line in process.stdout:
        #print line
        line = str(line).rstrip()
        filelist.append(path + str(line))	
   
    output,error = process.communicate()
    #print output
    return filelist


def writeindex(indexfilepath,map):
    
    tempfile = indexfilepath + ".tmp"
    fd = open(tempfile, 'w')
    for fname in map.keys():
        lst = map[fname]
        print >> fd , '%s %s' % (lst[0], lst[1])

    fd.flush()
    fd.close()
    os.rename(tempfile, indexfilepath)

def loadindex(indexfilepath):
    try:
    	f = open(indexfilepath,'r')
    	lst = []
    	for line in f:
        	lst.append(line)
    	f.close() 
    except Exception,e:
        print e,indexfilepath 

while(True):

	for dir in inputdirs:
            map = {}
            ret = listdir(dir)
            fname = dir.split("/")[-2]
            idxname = fname + ".idx"
            print "dir = " + str(dir)
            for x in ret:
                sts = getstat(x)
                m5 = getmd5sum(x)
                lst = [m5,sts] 
                map[x] = lst
            if os.path.exists(indexdir + "/" + idxname):
               loadindex(indexdir + "/" + idxname)
            writeindex(indexdir + "/" + idxname,map) 
	
	met = listdir(indexdir)
        for z in met:
            loadindex(z)
        mapx={}
        metname = indexdir + "/meta.idx"
        for y in met:
              sts = getstat(y)
              m5 = getmd5sum(y)
              lst = [m5,sts]
              mapx[y] = lst          
        writeindex(metname,mapx)

        print "sleeping for 60 secs"
        time.sleep(60)

#listdir(inputdirs[0])




