#!/usr/bin/python
#---------------------------------------------------------------------------------------------------
# Script to peek at stdout/stderr of a list of given condor jobs.
#
# -- match cleanly using the process record                                       (July     09 2015)
# Author: C.Paus                                                                  (November 25 2008)
#---------------------------------------------------------------------------------------------------
import os,sys,getopt,re

usage  = "\nUsage: condor_peek [  --err  --help ]  <list>\n\n"
usage += "   --out        - show standard output instead of standard error and output\n"
usage += "   --err        - show standard error instead of standard error and output\n"
usage += "   --help       - prints this short help message\n\n"
usage += "   list         - list of job ids to be peeked at\n\n"

# Define the valid options which can be specified and check out the command line
valid = ['file=','out','err','help']
try:
    opts, args = getopt.getopt(sys.argv[1:], "", valid)
except getopt.GetoptError, ex:
    print usage
    print str(ex)
    sys.exit(1)

# --------------------------------------------------------------------------------------------------
# Get all parameters for the production
# --------------------------------------------------------------------------------------------------
# Set defaults for each option
file = ''
out  = True
err  = True

# Read new values from the command line
for opt, arg in opts:
    if opt == "--help":
        print usage
        sys.exit(0)
    if opt == "--out":
        err  = False
    if opt == "--err":
        out  = False
    if opt == "--file":
        file = arg

# Check whether task useful
if len(sys.argv[1:])<1:
    print usage
    print '\n ERROR -- no jobId given.\n'
    sys.exit(0)
        
# Loop through the list and do our thing
for arg in sys.argv[1:]:

    # Skip the options
    if re.search('--',arg):
        continue

    # All right now we start
    print '\nCONDOR_PEEK -- Peeking at condor job: ' + arg + \
          '  with options=Out:"' + str(out) + '"; Err:"' + str(err) + '"'

    # Where is that job running?
    machine = "EMPTY"
    cmd = 'condor_q -global -l ' + arg + ' | grep ^RemoteHost'
    for line in os.popen(cmd).readlines():  # run find command
        line = line[:-1]                    # strip '\n'
        # split into the two pieces
        f = line.split("=")                 # splitting every '='
        machine = f[1][2:-1]
        #print ' Machine: ' + machine
        if re.search('@',machine):
            f = machine.split("@")
            machine = f[1]

    # What were the arguments
    args = "EMPTY"
    cmd = 'condor_q -global -l ' + arg + ' | egrep \(^Args\|^Arguments\)'
    for line in os.popen(cmd).readlines():  # run find command
        line = line[:-1]                    # strip '\n'
        # split into the two pieces
        f = line.split("=")                 # splitting every '='
        args = f[1]

    # When did it start, important for time matching, not 100% but should be darn close!
    startTime = 0
    cmd = 'condor_q -global -l ' + arg + ' | grep ^JobCurrentStartDate'
    for line in os.popen(cmd).readlines():  # run find command
        line = line[:-1]                    # strip '\n'
        # split into the two pieces
        f = line.split("=")                 # splitting every '='
        startTime = int(f[1])

    # Search the matching jobs on the identified machine
    condorDir = "EMPTY"
    cmd = 'ssh ' + machine + \
        ' ps auxwww | grep %s | grep -v grep | tr -s \' \'| cut -d \' \' -f 12'%(args)
    #print ' Find executeing directory: ' + cmd
    for line in os.popen(cmd).readlines():  # run find command
        line = line[:-1]                    # strip '\n'
        f = line.split("/")                 # splitting every blank
        condorDir = "/".join(f[:-1])
        
    print "\n runs on: " + machine + "   in area: " + condorDir

    catFile      = '_condor_std*'        
    if err and out:
        catFile      = '_condor_std*'
    elif out:
        catFile      = '_condor_stdout'
    elif err:
        catFile      = '_condor_stderr'

    if file != '':
        catFile = file

    list = 'ssh ' + machine + ' ls -l ' + condorDir + '/'
    print '\nCONDOR_PEEK -- List Directory contents: %s\n'%list
    status = os.system(list)

    cat  = 'ssh ' + machine + ' cat   ' + condorDir + '/' + catFile
    print '\nCONDOR_PEEK -- Cat file %s: %s\n'%(catFile,cat)
    status = os.system(cat)
            
    if cat != '':
        print ''
        print 'CONDOR_PEEK ==== INFO ===='
        print ' Output from: ' + list + '\n' + \
            ' followed by: ' + cat
        print 'CONDOR_PEEK ==== INFO ====\n'
        print ''
    else:
        print ''
        print ' No match was made.'
        print ''

sys.exit(0)
