#!/usr/bin/env python
#----------------------------------#
# -- Author: V. Garonne
# -- Mail: garonne@lal.in2p3.fr
# -- Date: 08/25/2006 
# -- Name: generateGantt
# -- Description: script to generate 
# -- the gantt chart from a input file
# -- It uses Paw.
#----------------------------------#

import os
import sys
import time
import string
import commands

def usage ():
    print 'Usage : > generateGannt <input file>'

if __name__ == '__main__':
    if len(sys.argv) != 2:
        usage ()
        sys.exit(-1)
    
    # First read the file    
    file  = sys.argv[1]
    f     = open (file)
    lines = f.readlines()
    f.close ()
    
    # Instanciate constants
    total = len(lines)
    min   = time.time()
    max   = 0.0
    for line in lines:
        package, start, end = string.split(line)
        #print float(end)-float(start)
        if float(start)< min:
            min = float(start) 
        if float(end)> max:
            max = float(end)

    # then compute for each package its line
    packages = {}
    for line in lines:
        package, start, end = string.split(line)
        packages[package] = {'start': float(start)-min , 'end':float(end)-min}

    deb = 0.0
    index = 0
    step = 0.01
    i = float(max)-float(min)
    done  = list()
    start = list() 
    while i>0:
      for package in packages:        
            if not package in done:
                if packages[package]['end']>i:
                    index = index + 1
                    packages[package]['index']= index
                    done.append(package)
                    #print i,index, package
            if not package in start:
                if packages[package]['start']> i:        
                    index = index - 1                    
                    start.append(package)
                    #print i, 'start', package, packages[package]['start'], packages[package]['end'], packages[package]['end']-packages[package]['start']
      i = i-step

    y = 0
    for package in packages:
        print package
        if packages[package]['index'] > y:
            y = packages[package]['index']

    f = open ("gantt.dat", "w+")     
    for index in xrange(total+1):
       for package in packages:
            if packages[package]['index'] == index:
                line = str(packages[package]['index'])+'\t'+str(packages[package]['start'])+'\t'+ str(packages[package]['end'])+'\n'
                f.write  (line)
    f.close  ()    
        
#    f = open ("gantt.dat", "w+")    
#    index = 1
#    for line in lines:
#        package, start, end = string.split(line)
#        f.write  (str(index)+ ' ' + str(float(start)-min)+ ' ' + str(float(end)-min) + '\n')        
#        index = index + 1        
#    f.close  ()
    
    content= '''    
        ops
        opt nsta
        Total = %s
        v/cre y([Total])  r
        v/cre x1([Total]) r
        v/cre x2([Total]) r
        v/read y,x1,x2  gantt.dat

        deb = 0.0
        max    = %s 
        totaly = %s
        set NDVY [Totaly].15
        NULL [deb] [max] 1 [Totaly] 
        SET BORD 1
        SET FAIS 1        
        SET PLCI 2
        SET FACI 3
        *HISTOGRAM/CREATE/2DHISTO 1 'Gannt chart' 0 10000 [max] 0 0 [Total]
        SET BORD 1
        SET FAIS 1        
        SET PLCI 2
        SET FACI 3
        
        DO i= 1,[Total]  
            y2 = y([i])+0.5
            GRAPHICS/PRIMITIVES/BOX x1([i]) x2([i]) y([i]) [y2] 
        ENDDO
        GRAPHICS/HPLOT/ATITLE 'Time(s)' 'Total number of parallel execution' ! 220
        cps
        '''%(str(total), str(float(max)-float(min)), str(y))
    f = open ("gantt.kumac", "w+")
    f.write  (content)
    f.close  ()

    f = open ("exec.kumac", "w+")
    f.write  ("\n\n gantt.kumac")
    f.close  ()

    status, output = commands.getstatusoutput ("paw<exec.kumac")    
#--------- EoF --------#