#!/usr/bin/env python
"""
    %prog merun

Check the outputs for the input run
"""

import sys, os
from sys import stderr
import deswl
from deswl.files import MultishearFiles
import esutil as eu
from esutil import json_util
from esutil.io import fexists

from optparse import OptionParser
parser = OptionParser(__doc__)

def main():

    options, args = parser.parse_args(sys.argv[1:])
    if len(args) < 1:
        parser.print_help()
        sys.exit(45)

    merun=args[0]
    rc=deswl.files.Runconfig(merun)

    mfobj=MultishearFiles(merun)
    all_files=mfobj.get_all_files()
    ntile=len(all_files)

    badlist=[]
    goodlist=[]
    print >>stderr,'checking',ntile
    for i,fdict in enumerate(all_files):
        print >>stderr,'%d/%d' % (i+1,ntile)

        tilename=fdict['tilename']
        band=rc['band']


        statfile=fdict['stat']
        qafile=fdict['qa']
        msfile=fdict['multishear']

        error_found=False
        missing=[]
        if not fexists(statfile):
            missing.append('stat')
            error_found=True
        if not fexists(qafile):
            missing.append('qa')
            error_found=True
        if not fexists(msfile):
            missing.append('multishear')
            error_found=True

        if len(missing) > 0:
            missmess=','.join(missing)
            print >>stderr,'%s-%s: %s missing' % (tilename,band,missmess)
            print >>stderr,'    %s' % fdict['stat']
            error_found=True

        if 'stat' not in missing:
            # this deals properly with hdfs
            stat=eu.io.read(statfile)
            exit_status= stat['exit_status']
            if exit_status != 0:
                print >>stderr,"%s-%s: Found non-zero exit status %s in stat file %s" % \
                                (tilename,band,exit_status,statfile)
                error_found=True
            
        if error_found:
            badlist.append(fdict)
        else:
            goodlist.append(fdict)

    print >>stderr,'Found %s/%s problems\n' % (len(badlist),len(all_files))

    goodfile=deswl.files.collated_path(merun, 'goodlist')
    badfile=deswl.files.collated_path(merun, 'badlist')

    eu.ostools.makedirs_fromfile(goodfile)

    print >>stderr,"Writing goodlist:",goodfile
    json_util.write(goodlist, goodfile)
    print >>stderr,"Writing badlist:",badfile
    json_util.write(badlist, badfile)
    

main()

