#
# Description:
#  Helper module for all glideinWMS pseudo-interactive commands
#
# Author:
#   Igor Sfiligoi (May 2008)
#

import sys,string,os,stat
import glideinMonitor


# Try to execute the pesudo interactive command
# based on the passed arguments
#
# argv_func is a callback that transforms the unrecognized arguments
# into a list of arguments to pass to the command line after it has
# moved into the monitored job dir
#
def exe_cmd(argv_func):
    try:
        args=glideinMonitor.parseArgs(sys.argv[1:])
        glideinMonitor.monitor(args['jid'],args['schedd_name'],args['pool_name'],
                               args['timeout'],
                               createCmdMonitorFile,argv_func(args['argv']))
    except RuntimeError,e:
        print e
        sys.exit(1)

# Try to execute the pesudo interactive command
# based on the passed arguments
#
# argv_func is a callback that transforms the unrecognized arguments
# into a list of arguments to pass to the command line
#
def exe_cmd_simple(argv_func):
    try:
        args=glideinMonitor.parseArgs(sys.argv[1:])
        glideinMonitor.monitor(args['jid'],args['schedd_name'],args['pool_name'],
                               args['timeout'],
                               createCmdMonitorFileSimple,argv_func(args['argv']))
    except RuntimeError,e:
        print e
        sys.exit(1)

# Try to execute the pesudo interactive command
# based on the passed arguments
#
# argv_func is a callback that creates the monitoring script
# arguments are (file_name,out_relname,argv,condor_status)
# where argv is the list of unrecognized arguments
#
def exe_script(argv_func):
    try:
        args=glideinMonitor.parseArgs(sys.argv[1:])
        glideinMonitor.monitor(args['jid'],args['schedd_name'],args['pool_name'],
                               args['timeout'],
                               argv_func,args['argv'])
    except RuntimeError,e:
        print e
        sys.exit(1)

######################################
# INTERNAL - Do not use directly
######################################

# create a file usable by a callback function for glideinMonitor.monitor
def createCmdMonitorFileScript(monitor_file_name,monitor_control_relname,
                               script_list):
    # create the command file
    fd=open(monitor_file_name,"w")
    try:
        fd.write("#!/bin/sh\n")
        fd.write("glidein_cmd_startdir=$PWD\n")
        for script_line in script_list:
            fd.write("%s\n"%script_line)
        fd.write('echo Done > "$glidein_cmd_startdir/%s"\n'%monitor_control_relname)
    finally:
        fd.close()

    os.chmod(monitor_file_name,stat.S_IRWXU)

# convert a list into a command line string
def argv2cmd(argv):
    # escape the spaces
    # everything else should be passed to the cmsdline as it is
    eargv=[]
    for arg in argv:
        eargv.append(string.replace(arg,' ','\ '))
    return string.join(eargv)

# callback function for glideinMonitor.monitor
# changes to the work dir and executes the command
def createCmdMonitorFile(monitor_file_name,monitor_control_relname,
                         argv,condor_status):
    script_lines=[]
    script_lines.append("outdir=`ls -lt .. | tail -1 | awk '{print $9}'`")
    # execute command in work dir
    script_lines.append("(cd ../$outdir; if [ $? -eq 0 ]; then %s; else echo Internal error; fi)"%argv2cmd(argv))
    return createCmdMonitorFileScript(monitor_file_name,monitor_control_relname,script_lines)

# callback function for glideinMonitor.monitor
# executes the command
def createCmdMonitorFileSimple(monitor_file_name,monitor_control_relname,
                         argv,condor_status):
    script_lines=[]
    script_lines.append(argv2cmd(argv))
    return createCmdMonitorFileScript(monitor_file_name,monitor_control_relname,script_lines)



@@ -182,34 +182,34 @@
 to execute the p
-e
 s
+e
 udo interactive 
@@ -794,34 +794,34 @@
 to execute the p
-e
 s
+e
 udo interactive 
@@ -1379,18 +1379,18 @@
 te the p
-e
 s
+e
 udo inte
@@ -1474,147 +1474,92 @@
 hat 
-creates the monitoring script%0A# arguments are (file_name,out_relname,argv,condor_status)%0A# where argv is the list of unrecognized arguments
+transforms the unrecognized arguments%0A# into a list of lines to populate a sh script
 %0A#%0Ad
@@ -1565,16 +1565,20 @@
 def exe_
+cmd_
 script(a
@@ -1806,24 +1806,51 @@
             
+createCmdMonitorFileScript,
 argv_func,ar
@@ -1846,17 +1846,17 @@
 rgv_func
-,
+(
 args%5B'ar
@@ -1860,16 +1860,17 @@
 'argv'%5D)
+)
 %0A    exc
@@ -2119,34 +2119,29 @@
 def 
-createCmdM
+m
 onitor
-File
 Script
+FromList
 (mon
@@ -2172,37 +2172,32 @@
 ontrol_relname,%0A
-     
                 
@@ -3357,34 +3357,29 @@
 urn 
-createCmdM
+m
 onitor
-File
 Script
+FromList
 (mon
@@ -3593,32 +3593,38 @@
                 
+      
 argv,condor_stat
@@ -3703,89 +3703,386 @@
 urn 
-createCmdMonitorFileScript(monitor_file_name,monitor_control_relname,script_lines
+monitorScriptFromList(monitor_file_name,monitor_control_relname,script_lines)%0A%0A# callback function for glideinMonitor.monitor%0A# executes the script_list lines%0Adef createCmdMonitorFileScript(monitor_file_name,monitor_control_relname,%0A                               script_list,condor_status):%0A    return monitorScriptFromList(monitor_file_name,monitor_control_relname,script_list
 )%0A%0A%0A

Add exe_cmd_script