#! /usr/bin/env python

#############################################################
#                                                           #
#   Author: Nicolas Joly                                    #
#   Organization:'Biological Software and Databases' Group, #
#                Institut Pasteur, Paris.                   #
#   Distributed under GPLv2 Licence. Please refer to the    #
#   COPYING.LIB document.                                   #
#                                                           #
#############################################################

import os, sys, time

MOBYLEHOME = None
if os.environ.has_key('MOBYLEHOME'):
  MOBYLEHOME = os.environ['MOBYLEHOME']
if not MOBYLEHOME:
  sys.exit('MOBYLEHOME must be defined in your environment')

sys.path.append(os.path.join(MOBYLEHOME,'Src'))

from Mobyle.ConfigManager import Config
from Mobyle.Admin import Admin 
from Mobyle.Utils import getStatus as utils_getStatus

cfg = Config()
adm = cfg.admindir()
lst = os.listdir(adm)

for job in lst:
    fil = os.path.join(adm, job)
    if not os.path.isfile(fil) or job[0] == '.': 
        continue
    admin = Admin(fil)
    
    print "%s" % ("-"*80)
    
    jid = admin.getJobID()
    if jid == None:
        print "Unknown job %s" % job
        continue
    key = jid.split(os.path.sep)[-1]
    exc_alias = admin.getExecutionAlias()
    que = admin.getQueue()
    sta = utils_getStatus(jid)
    print "%s -- %s/%s -- %s" % (key, exc_alias , que, sta)
    
    nam = admin.getJobName()
    ema = admin.getEmail()
    tim = time.strftime("%x %X", admin.getDate())
    ses = admin.getSession()
    adr = admin.getRemote()
    src = (ses is None) and "REMOTE" or "LOCAL"
    wfid = admin.getWorkflowID( 'STANDALONE')
    print "%s -- %s -- %s -- %s -- %s -- %s " % (nam, ema, tim, src, wfid , adr)
    
    fil = os.path.join(cfg.results_path(), nam, key, '.command')
    fh = open(fil)
    cmd = fh.readline().strip()
    fh.close()
    print "%s" % cmd

sys.exit(0)
