import os
import time
import subprocess


# set max processors to use
numlocalProc = 12
rootdir  = os.getcwd()

# loop through canines 1,2,3,4
#for caninenumber in [2]:
for caninenumber in [2,1,3,4]:
   # get complete list of dir
   dirjobid = "dog%dfem9800" % caninenumber
   joblist  = os.listdir( dirjobid )
   joblist.sort()

   # loop until all have been processed 
   process = []
   while ( len(joblist) > 0 or  len(process) > 0 ): 
     # only run numlocalProc at a time
     if (len(process) > numlocalProc):
       raise RuntimeError("\n\n running too many jobs at a time??")
     elif (len(process) == numlocalProc):
       print len(joblist), " jobs remaining..."
       time.sleep(1) # pause wait for jobs to finish
     elif( len(joblist) > 0 ):
       directory = joblist.pop(0)
       if( os.path.isdir( "%s/%s" % (dirjobid,directory)) ):
         if( os.path.exists( "%s.dat" % directory ) ):
           print "%s/%s completed already.. %d jobs left" % (dirjobid,directory,len(joblist))
         elif( os.path.exists( "%s/%s/profile.%s.1.log" % (dirjobid,directory,directory)) ):
           cmd = "sed 's/dog%dfem980032/%s/g' dog%derrorMeshTemplate.pvsm > %s.pvsm" % (caninenumber,directory,caninenumber,directory)
           print cmd
           os.system(cmd)
           process.append( subprocess.Popen("vglrun paraview --state=%s.pvsm --exit" % directory ,shell=True)  )
         else:
           print "%s/%s not done.. %d jobs left" % (dirjobid,directory,len(joblist))
           joblist.append(directory)
       else:
         pass #not a directory
     # check status of running jobs
     if( len(process) > 0 ):
       runningJob = process.pop(0)
       if ( runningJob.poll() == None ):
         # job not done put it back in the list
         # not that we pop from the front of the list and pushback at the 
         # end to cycle through
         print " pid ",runningJob.pid, " still running"
         process.append( runningJob )
       elif ( runningJob.poll() == 0 ):
         pass # job is done 
       else:
         print "job exiting with ", runningJob.poll() 
         raise RuntimeError("\n\n unknown exit code ")
