#!/usr/bin/env jython

from subprocess import call, PIPE, Popen
import tempfile
from xml.etree.ElementTree import ElementTree, Element, tostring
import re
import sys

import logging.config
from pslogger import PSLogger
from ipptopspsdb import IppToPspsDb
from odm import Odm
from batch import Batch


if len(sys.argv) > 1: CONFIG  = sys.argv[1]
else:
    print "\n** Usage: " + sys.argv[0] + " <configPath>\n"
    sys.exit(1)

# open config file
configDoc = ElementTree(file=CONFIG)

# set up logging
logging.setLoggerClass(PSLogger)
logger = logging.getLogger(sys.argv[0])
logger.setup(configDoc, sys.argv[0])

ippToPspsDb = IppToPspsDb(logger, configDoc)
odm = Odm(logger, configDoc)

DVOLABEL = configDoc.find("dvo/gpc1Label").text
EPOCH = configDoc.find("options/epoch").text

ids = ippToPspsDb.getFailedLoadedToODMBatchIDs(EPOCH, DVOLABEL)


logger.infoPair("Batches failed load to ODM", "%d" % len(ids))


results = {}

for id in ids:

   if odm.checkBatch(id, results):
       logger.info("  %9s : %s" % (Batch.getNameFromID(id), results['DETAILS']))
   else:
       logger.info("  %9s : failed to poll ODM" % Batch.getNameFromID(id))


