#
# factory/tool specific condorLogs helper
#

import time,os.path,mmap,re
import glideFactoryLogParser
import condorLogParser

# get the list of jobs that were active at a certain time
def get_glideins(log_dir_name,date_arr,time_arr):
    glidein_list=[]

    cldata=glideFactoryLogParser.dirSummaryTimingsOutFull(log_dir_name)
    cldata.load(active_only=False)
    glidein_data=cldata.data['Completed'] # I am interested only in the completed ones

    ref_ctime=time.mktime(date_arr+time_arr+(0,0,-1))

    for glidein_el in glidein_data:
        glidein_id,fistTimeStr,runningStartTimeStr,lastTimeStr=glidein_el
        runningStartTime=condorLogParser.rawTime2cTimeLastYear(runningStartTimeStr)
        if runningStartTime>ref_ctime:
            continue # not one of them, started after
        lastTime=condorLogParser.rawTime2cTimeLastYear(lastTimeStr)
        if lastTime<ref_ctime:
            continue # not one of them, ended before
        glidein_list.append(glidein_id)

    return glidein_list
            
# get the list of log files for an entry that were active at a certain time
def get_glidein_logs_entry(factory_dir,entry,date_arr,time_arr,ext="err"):
    log_list=[]
    
    log_dir_name=os.path.join(factory_dir,"entry_%s/log"%entry)
    glidein_list=get_glideins(log_dir_name,date_arr,time_arr)
    for glidein_id in glidein_list:
        glidein_log_file="job.%i.%i."%condorLogParser.rawJobId2Nr(glidein_id)
        glidein_log_file+=ext
        glidein_log_filepath=os.path.join(log_dir_name,glidein_log_file)
        if os.path.exists(glidein_log_filepath):
            log_list.append(glidein_log_filepath)

    return log_list
    
# get the list of log files for an entry that were active at a certain time
def get_glidein_logs(factory_dir,entries,date_arr,time_arr,ext="err"):
    log_list=[]
    for entry in entries:
        entry_log_list=get_glidein_logs_entry(factory_dir,entry,date_arr,time_arr,ext)
        log_list+=entry_log_list

    return log_list

SL_START_RE=re.compile("^StartdLog\n======== gzip . uuencode =============\n",re.M|re.DOTALL)

# extract the StartdLog from a glidein log file
def get_StartdLog(log_fname):
    size = os.path.getsize(log_fname)
    fd=open(log_fname)
    try:
        buf=mmap.mmap(fd.fileno(),size,access=mmap.ACCESS_READ)
        try:
            # first find the header that delimits the log in the file
            start_re=SL_START_RE.search(buf,0)
            if start_re==None:
                return "" #no StartLog section
            log_start_idx=start_re.end()

            # find where it ends
            log_end_idx=buf.find("\n====",log_start_idx)
            if log_end_idx<0: # up to the end of the file
                return buf[log_start_idx:]
            else:
                return buf[log_start_idx:log_end_idx]
        finally:
            buf.close()
    finally:
        fd.close()

@@ -2618,16 +2618,18 @@
 (%22%5Cn====
+%5Cn
 %22,log_st
@@ -2797,16 +2797,17 @@
 art_idx:
+(
 log_end_
@@ -2809,17 +2809,38 @@
 _end_idx
-%5D
++6)%5D # include endline
 %0A       

Support new debug format