qdo revision 1940
11884Sstever@eecs.umich.edu#! /usr/bin/env python
21884Sstever@eecs.umich.edu
31884Sstever@eecs.umich.edu# Copyright (c) 2004-2005 The Regents of The University of Michigan
41884Sstever@eecs.umich.edu# All rights reserved.
51884Sstever@eecs.umich.edu#
61884Sstever@eecs.umich.edu# Redistribution and use in source and binary forms, with or without
71884Sstever@eecs.umich.edu# modification, are permitted provided that the following conditions are
81884Sstever@eecs.umich.edu# met: redistributions of source code must retain the above copyright
91884Sstever@eecs.umich.edu# notice, this list of conditions and the following disclaimer;
101884Sstever@eecs.umich.edu# redistributions in binary form must reproduce the above copyright
111884Sstever@eecs.umich.edu# notice, this list of conditions and the following disclaimer in the
121884Sstever@eecs.umich.edu# documentation and/or other materials provided with the distribution;
131884Sstever@eecs.umich.edu# neither the name of the copyright holders nor the names of its
141884Sstever@eecs.umich.edu# contributors may be used to endorse or promote products derived from
151884Sstever@eecs.umich.edu# this software without specific prior written permission.
161884Sstever@eecs.umich.edu#
171884Sstever@eecs.umich.edu# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
181884Sstever@eecs.umich.edu# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
191884Sstever@eecs.umich.edu# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
201884Sstever@eecs.umich.edu# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
211884Sstever@eecs.umich.edu# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
221884Sstever@eecs.umich.edu# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
231884Sstever@eecs.umich.edu# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
241884Sstever@eecs.umich.edu# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
251884Sstever@eecs.umich.edu# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
261884Sstever@eecs.umich.edu# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
271884Sstever@eecs.umich.edu# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
281884Sstever@eecs.umich.edu
291884Sstever@eecs.umich.eduimport sys
301884Sstever@eecs.umich.eduimport os
311884Sstever@eecs.umich.eduimport re
321884Sstever@eecs.umich.eduimport time
331884Sstever@eecs.umich.eduimport optparse
341884Sstever@eecs.umich.edu
351884Sstever@eecs.umich.eduimport pexpect
361884Sstever@eecs.umich.edu
371884Sstever@eecs.umich.eduprogname = os.path.basename(sys.argv[0])
381884Sstever@eecs.umich.edu
391884Sstever@eecs.umich.eduusage = "%prog [options] command [command arguments]"
401884Sstever@eecs.umich.eduoptparser = optparse.OptionParser(usage=usage)
411884Sstever@eecs.umich.eduoptparser.allow_interspersed_args=False
421884Sstever@eecs.umich.eduoptparser.add_option('-e', dest='stderr_file',
431884Sstever@eecs.umich.edu                     help='command stderr output file')
441884Sstever@eecs.umich.eduoptparser.add_option('-o', dest='stdout_file',
451884Sstever@eecs.umich.edu                     help='command stdout output file')
461884Sstever@eecs.umich.eduoptparser.add_option('-l', dest='save_log', action='store_true',
471884Sstever@eecs.umich.edu                     help='save qsub output log file')
481930Sstever@eecs.umich.eduoptparser.add_option('-N', dest='job_name',
491930Sstever@eecs.umich.edu                     help='qsub job name')
501930Sstever@eecs.umich.eduoptparser.add_option('-q', dest='dest_queue',
511930Sstever@eecs.umich.edu                     help='qsub destination queue')
521930Sstever@eecs.umich.eduoptparser.add_option('--qwait', dest='qsub_timeout', type='int',
531884Sstever@eecs.umich.edu                     help='qsub queue wait timeout', default=30*60)
541884Sstever@eecs.umich.eduoptparser.add_option('-t', dest='cmd_timeout', type='int',
551884Sstever@eecs.umich.edu                     help='command execution timeout', default=600*60)
561884Sstever@eecs.umich.edu
571884Sstever@eecs.umich.edu(options, cmd) = optparser.parse_args()
581884Sstever@eecs.umich.edu
591884Sstever@eecs.umich.eduif cmd == []:
601884Sstever@eecs.umich.edu    print >>sys.stderr, "%s: missing command" % progname
611884Sstever@eecs.umich.edu    sys.exit(1)
621884Sstever@eecs.umich.edu
631940Sstever@eecs.umich.edu# If we want to do this, need to add check here to make sure cmd[0] is
641940Sstever@eecs.umich.edu# a valid PBS job name, else qsub will die on us.
651940Sstever@eecs.umich.edu#
661940Sstever@eecs.umich.edu#if not options.job_name:
671940Sstever@eecs.umich.edu#    options.job_name = cmd[0]
681930Sstever@eecs.umich.edu
691884Sstever@eecs.umich.educwd = os.getcwd()
701884Sstever@eecs.umich.edu
711884Sstever@eecs.umich.edu# Deal with systems where /n is a symlink to /.automount
721884Sstever@eecs.umich.eduif cwd.startswith('/.automount/'):
731884Sstever@eecs.umich.edu    cwd = cwd.replace('/.automount/', '/n/', 1)
741884Sstever@eecs.umich.edu
751884Sstever@eecs.umich.eduif not cwd.startswith('/n/poolfs/'):
761884Sstever@eecs.umich.edu    print >>sys.stderr, "Error: current directory must be under /n/poolfs."
771884Sstever@eecs.umich.edu    sys.exit(1)
781884Sstever@eecs.umich.edu
791884Sstever@eecs.umich.edu# The Shell class wraps pexpect.spawn with some handy functions that
801884Sstever@eecs.umich.edu# assume the thing on the other end is a Bourne/bash shell.
811884Sstever@eecs.umich.educlass Shell(pexpect.spawn):
821884Sstever@eecs.umich.edu    # Regexp to match the shell prompt.  We change the prompt to
831884Sstever@eecs.umich.edu    # something fixed and distinctive to make it easier to match
841884Sstever@eecs.umich.edu    # reliably.
851884Sstever@eecs.umich.edu    prompt_re = re.compile('qdo\$ ')
861884Sstever@eecs.umich.edu
871884Sstever@eecs.umich.edu    def __init__(self, cmd):
881884Sstever@eecs.umich.edu        # initialize base pexpect.spawn object
891884Sstever@eecs.umich.edu	try:
901884Sstever@eecs.umich.edu            pexpect.spawn.__init__(self, cmd)
911884Sstever@eecs.umich.edu	except pexpect.ExceptionPexpect, exc:
921884Sstever@eecs.umich.edu	    print "%s:" % progname, exc
931884Sstever@eecs.umich.edu	    sys.exit(1)
941884Sstever@eecs.umich.edu        # full_output accumulates the full output of the session
951884Sstever@eecs.umich.edu        self.full_output = ""
961884Sstever@eecs.umich.edu        self.quick_timeout = 15
971884Sstever@eecs.umich.edu        # wait for a prompt, then change it
981884Sstever@eecs.umich.edu        try:
991884Sstever@eecs.umich.edu            self.expect('\$ ', options.qsub_timeout)
1001884Sstever@eecs.umich.edu        except pexpect.TIMEOUT:
1011884Sstever@eecs.umich.edu            print >>sys.stderr, "%s: qsub timed out." % progname
1021884Sstever@eecs.umich.edu            self.kill(15)
1031884Sstever@eecs.umich.edu            self.close(wait=True)
1041884Sstever@eecs.umich.edu            sys.exit(1)
1051891Sstever@eecs.umich.edu        self.do_command('unset PROMPT_COMMAND; PS1="qdo$ "')
1061884Sstever@eecs.umich.edu
1071884Sstever@eecs.umich.edu    # version of expect that updates full_output too
1081884Sstever@eecs.umich.edu    def expect(self, regexp, timeout = -1):
1091884Sstever@eecs.umich.edu        pexpect.spawn.expect(self, regexp, timeout)
1101884Sstever@eecs.umich.edu        self.full_output += self.before + self.after
1111884Sstever@eecs.umich.edu
1121884Sstever@eecs.umich.edu    # Just issue a command and wait for the next prompt.
1131884Sstever@eecs.umich.edu    # Returns a string containing the output of the command.
1141884Sstever@eecs.umich.edu    def do_bare_command(self, cmd, timeout = -1):
1151884Sstever@eecs.umich.edu        global full_output
1161884Sstever@eecs.umich.edu        self.sendline(cmd)
1171884Sstever@eecs.umich.edu        # read back the echo of the command
1181884Sstever@eecs.umich.edu        self.readline()
1191884Sstever@eecs.umich.edu        # wait for the next prompt
1201884Sstever@eecs.umich.edu        self.expect(self.prompt_re, timeout)
1211884Sstever@eecs.umich.edu        output = self.before.rstrip()
1221884Sstever@eecs.umich.edu        return output
1231884Sstever@eecs.umich.edu
1241884Sstever@eecs.umich.edu    # Issue a command, then query its exit status.
1251884Sstever@eecs.umich.edu    # Returns a (string, int) tuple with the command output and the status.
1261884Sstever@eecs.umich.edu    def do_command(self, cmd, timeout = -1):
1271884Sstever@eecs.umich.edu        # do the command itself
1281884Sstever@eecs.umich.edu        output = self.do_bare_command(cmd, timeout)
1291884Sstever@eecs.umich.edu        # collect status
1301884Sstever@eecs.umich.edu        status = int(self.do_bare_command("echo $?", self.quick_timeout))
1311884Sstever@eecs.umich.edu        return (output, status)
1321884Sstever@eecs.umich.edu
1331884Sstever@eecs.umich.edu    # Check to see if the given directory exists.
1341884Sstever@eecs.umich.edu    def dir_exists(self, dirname):
1351884Sstever@eecs.umich.edu        (output, status) = shell.do_command('[ -d %s ]' % dirname,
1361884Sstever@eecs.umich.edu                                            self.quick_timeout)
1371884Sstever@eecs.umich.edu        return status == 0
1381884Sstever@eecs.umich.edu
1391884Sstever@eecs.umich.edu
1401884Sstever@eecs.umich.edu# Spawn the interactive pool job.
1411884Sstever@eecs.umich.edu
1421884Sstever@eecs.umich.edu# Hack to do link on poolfs... disabled for now since
1431884Sstever@eecs.umich.edu# compiler/linker/library versioning problems between poolfs and
1441884Sstever@eecs.umich.edu# nodes.  May never work since poolfs is x86-64 and nodes are 32-bit.
1451884Sstever@eecs.umich.eduif False and len(cmd) > 50:
1461884Sstever@eecs.umich.edu    shell_cmd = 'ssh -t poolfs /bin/sh -l'
1471884Sstever@eecs.umich.edu    print "%s: running %s on poolfs" % (progname, cmd[0])
1481884Sstever@eecs.umich.eduelse:
1491884Sstever@eecs.umich.edu    shell_cmd = 'qsub -I -S /bin/sh'
1501940Sstever@eecs.umich.edu    if options.job_name:
1511940Sstever@eecs.umich.edu        shell_cmd += ' -N "%s"' % options.job_name
1521930Sstever@eecs.umich.edu    if options.dest_queue:
1531930Sstever@eecs.umich.edu        shell_cmd += ' -q ' + options.dest_queue
1541884Sstever@eecs.umich.edu
1551884Sstever@eecs.umich.edushell = Shell(shell_cmd)
1561884Sstever@eecs.umich.edu
1571884Sstever@eecs.umich.edutry:
1581884Sstever@eecs.umich.edu    # chdir to cwd
1591884Sstever@eecs.umich.edu    (output, status) = shell.do_command('cd ' + cwd)
1601884Sstever@eecs.umich.edu
1611884Sstever@eecs.umich.edu    if status != 0:
1621884Sstever@eecs.umich.edu        raise OSError, "Can't chdir to %s" % cwd
1631884Sstever@eecs.umich.edu
1641884Sstever@eecs.umich.edu    # wacky hack: sometimes scons will create an output directory then
1651884Sstever@eecs.umich.edu    # fork a job to generate files in that directory, and the job will
1661884Sstever@eecs.umich.edu    # get run before the directory creation propagates through NFS.
1671884Sstever@eecs.umich.edu    # This hack looks for a '-o' option indicating an output file and
1681884Sstever@eecs.umich.edu    # waits for the corresponding directory to appear if necessary.
1691884Sstever@eecs.umich.edu    try:
1701884Sstever@eecs.umich.edu        if 'cc' in cmd[0] or 'g++' in cmd[0]:
1711884Sstever@eecs.umich.edu            output_dir = os.path.dirname(cmd[cmd.index('-o')+1])
1721884Sstever@eecs.umich.edu        elif 'm5' in cmd[0]:
1731884Sstever@eecs.umich.edu            output_dir = cmd[cmd.index('-d')+1]
1741884Sstever@eecs.umich.edu        else:
1751884Sstever@eecs.umich.edu            output_dir = None
1761884Sstever@eecs.umich.edu    except (ValueError, IndexError):
1771884Sstever@eecs.umich.edu        # no big deal if there's no '-o'/'-d' or if it's the final argument
1781884Sstever@eecs.umich.edu        output_dir = None
1791884Sstever@eecs.umich.edu
1801884Sstever@eecs.umich.edu    if output_dir:
1811884Sstever@eecs.umich.edu        secs_waited = 0
1821884Sstever@eecs.umich.edu        while not shell.dir_exists(output_dir) and secs_waited < 45:
1831884Sstever@eecs.umich.edu            time.sleep(5)
1841884Sstever@eecs.umich.edu            secs_waited += 5
1851884Sstever@eecs.umich.edu        if secs_waited > 10:
1861884Sstever@eecs.umich.edu            print "waited", secs_waited, "seconds for", output_dir
1871884Sstever@eecs.umich.edu
1881884Sstever@eecs.umich.edu    # run command
1891884Sstever@eecs.umich.edu    if options.stdout_file:
1901884Sstever@eecs.umich.edu        cmd += ['>', options.stdout_file]
1911884Sstever@eecs.umich.edu    if options.stderr_file:
1921884Sstever@eecs.umich.edu        cmd += ['2>', options.stderr_file]
1931884Sstever@eecs.umich.edu    try:
1941884Sstever@eecs.umich.edu        (output, status) = shell.do_command(' '.join(cmd), options.cmd_timeout)
1951884Sstever@eecs.umich.edu    except pexpect.TIMEOUT:
1961884Sstever@eecs.umich.edu            print >>sys.stderr, "%s: command timed out after %d seconds." \
1971884Sstever@eecs.umich.edu                  % (progname, options.cmd_timeout)
1981884Sstever@eecs.umich.edu            shell.sendline('~.') # qsub/ssh termination escape sequence
1991884Sstever@eecs.umich.edu            shell.close(wait=True)
2001884Sstever@eecs.umich.edu            status = 3
2011884Sstever@eecs.umich.edu    if output:
2021884Sstever@eecs.umich.edu        print output
2031884Sstever@eecs.umich.edu
2041884Sstever@eecs.umich.edufinally:
2051884Sstever@eecs.umich.edu    # end job
2061884Sstever@eecs.umich.edu    if shell.isalive():
2071884Sstever@eecs.umich.edu        shell.sendline('exit')
2081884Sstever@eecs.umich.edu        shell.expect('qsub: job .* completed\r\n')
2091884Sstever@eecs.umich.edu        shell.close(wait=True)
2101884Sstever@eecs.umich.edu
2111884Sstever@eecs.umich.edu    # if there was an error, log the output even if not requested
2121884Sstever@eecs.umich.edu    if status != 0 or options.save_log:
2131884Sstever@eecs.umich.edu        log = file('qdo-log.' + str(os.getpid()), 'w')
2141884Sstever@eecs.umich.edu        log.write(shell.full_output)
2151884Sstever@eecs.umich.edu        log.close()
2161884Sstever@eecs.umich.edu
2171884Sstever@eecs.umich.edudel shell
2181884Sstever@eecs.umich.edu
2191884Sstever@eecs.umich.edusys.exit(status)
220