pbs.py revision 1928
16145SN/A# Copyright (c) 2005 The Regents of The University of Michigan
26145SN/A# All rights reserved.
36145SN/A#
46145SN/A# Redistribution and use in source and binary forms, with or without
56145SN/A# modification, are permitted provided that the following conditions are
66145SN/A# met: redistributions of source code must retain the above copyright
76145SN/A# notice, this list of conditions and the following disclaimer;
86145SN/A# redistributions in binary form must reproduce the above copyright
96145SN/A# notice, this list of conditions and the following disclaimer in the
106145SN/A# documentation and/or other materials provided with the distribution;
116145SN/A# neither the name of the copyright holders nor the names of its
126145SN/A# contributors may be used to endorse or promote products derived from
136145SN/A# this software without specific prior written permission.
146145SN/A#
156145SN/A# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
166145SN/A# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
176145SN/A# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
186145SN/A# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
196145SN/A# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
206145SN/A# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
216145SN/A# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
226145SN/A# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
236145SN/A# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
246145SN/A# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
256145SN/A# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
266145SN/A#
276145SN/A# Authors: Nathan Binkert
286145SN/A
296145SN/Aimport os, popen2, re, sys
307039SN/A
316145SN/Aclass MyPOpen(object):
326145SN/A    def __init__(self, cmd, input = None, output = None, bufsize = -1):
336145SN/A        self.status = -1
347039SN/A
357039SN/A        if input is None:
366145SN/A            p2c_read, p2c_write = os.pipe()
377456SN/A            self.tochild = os.fdopen(p2c_write, 'w', bufsize)
387832SN/A        else:
397456SN/A            p2c_write = None
407002SN/A            if isinstance(input, file):
418229SN/A                p2c_read = input.fileno()
427456SN/A            elif isinstance(input, str):
437002SN/A                input = file(input, 'r')
4411021Sjthestness@gmail.com                p2c_read = input.fileno()
458229SN/A            elif isinstance(input, int):
466154SN/A                p2c_read = input
476154SN/A            else:
4810301Snilay@cs.wisc.edu                raise AttributeError
4911021Sjthestness@gmail.com
5011021Sjthestness@gmail.com        if output is None:
516145SN/A            c2p_read, c2p_write = os.pipe()
5211021Sjthestness@gmail.com            self.fromchild = os.fdopen(c2p_read, 'r', bufsize)
537039SN/A        else:
547039SN/A            c2p_read = None
5511021Sjthestness@gmail.com            if isinstance(output, file):
5611021Sjthestness@gmail.com                c2p_write = output.fileno()
576145SN/A            elif isinstance(output, str):
5811025Snilay@cs.wisc.edu                output = file(output, 'w')
597922SN/A                c2p_write = output.fileno()
6011025Snilay@cs.wisc.edu            elif isinstance(output, int):
617567SN/A                c2p_write = output
627039SN/A            else:
639302SN/A                raise AttributeError
646145SN/A
657039SN/A        self.pid = os.fork()
667039SN/A        if self.pid == 0:
677039SN/A            os.dup2(p2c_read, sys.stdin.fileno())
6810893Snilay@cs.wisc.edu            os.dup2(c2p_write, sys.stdout.fileno())
697456SN/A            os.dup2(c2p_write, sys.stderr.fileno())
7010893Snilay@cs.wisc.edu            try:
717456SN/A                os.execvp(cmd[0], cmd)
7210893Snilay@cs.wisc.edu            finally:
737039SN/A                os._exit(1)
746145SN/A
7510074SN/A        os.close(p2c_read)
767039SN/A        os.close(c2p_write)
777039SN/A
789602SN/A    def poll(self):
797039SN/A        if self.status < 0:
8011021Sjthestness@gmail.com            pid, status = os.waitpid(self.pid, os.WNOHANG)
819629SN/A            if pid == self.pid:
829629SN/A                self.status = status
839629SN/A        return self.status
849629SN/A
859629SN/A    def wait(self):
869602SN/A        if self.status < 0:
877039SN/A            pid, status = os.waitpid(self.pid, 0)
886145SN/A            if pid == self.pid:
899508SN/A                self.status = status
909465SN/A        return self.status
9111021Sjthestness@gmail.com
929602SN/Aclass qsub:
939602SN/A    def __init__(self):
949508SN/A        self.afterok = None
959508SN/A        self.hold = False
969508SN/A        self.join = False
979508SN/A        self.keep_stdout = False
9811021Sjthestness@gmail.com        self.keep_stderr = False
999602SN/A        self.node_type = None
1009602SN/A        self.mail_abort = False
1019465SN/A        self.mail_begin = False
1029465SN/A        self.mail_end = False
10311021Sjthestness@gmail.com        self.name = None
1046863SN/A        self.stdout = None
10511021Sjthestness@gmail.com        self.priority = None
1066145SN/A        self.queue = None
10710074SN/A        self.pbshost = None
10810074SN/A        self.qsub = 'qsub'
10910074SN/A        self.env = {}
1106145SN/A
1117039SN/A    def build(self, script, args = []):
1127039SN/A        self.cmd = [ self.qsub ]
1137039SN/A
1147039SN/A        if self.env:
11510893Snilay@cs.wisc.edu            arg = '-v'
1167039SN/A            arg += ','.join([ '%s=%s' % i for i in self.env.iteritems() ])
1176145SN/A            self.cmd.append(arg)
1189499SN/A
1199499SN/A        if self.hold:
1209465SN/A            self.cmd.append('-h')
12110226SN/A
12210074SN/A        if self.stdout:
12310226SN/A            self.cmd.append('-olocalhost:' + self.stdout)
12410074SN/A
1257039SN/A        if self.keep_stdout and self.keep_stderr:
1267039SN/A            self.cmd.append('-koe')
12710979Sdavid.hashe@amd.com        elif self.keep_stdout:
12810979Sdavid.hashe@amd.com            self.cmd.append('-ko')
1296145SN/A        elif self.keep_stderr:
13010096SN/A            self.cmd.append('-ke')
1316145SN/A        else:
1327039SN/A            self.cmd.append('-kn')
1337039SN/A
1347039SN/A        if self.join:
1356285SN/A            self.cmd.append('-joe')
1367973SN/A
1377973SN/A        if self.node_type:
1387973SN/A            self.cmd.append('-lnodes=' + self.node_type)
13911049Snilay@cs.wisc.edu
14011049Snilay@cs.wisc.edu        if self.mail_abort or self.mail_begin or self.mail_end:
14111049Snilay@cs.wisc.edu            flags = ''
14211049Snilay@cs.wisc.edu            if self.mail_abort:
14311049Snilay@cs.wisc.edu                flags.append('a')
1449302SN/A            if self.mail_begin:
1459302SN/A                flags.append('b')
1469302SN/A            if self.mail_end:
1479302SN/A                flags.append('e')
1489302SN/A            if len(flags):
1499302SN/A                self.cmd.append('-m ' + flags)
1507039SN/A
15111021Sjthestness@gmail.com        if self.name:
15211021Sjthestness@gmail.com            self.cmd.append("-N%s" % self.name)
15311021Sjthestness@gmail.com
15410087SN/A        if self.priority:
15510087SN/A            self.cmd.append('-p' + self.priority)
15610087SN/A
1577039SN/A        if self.queue:
1589508SN/A            self.cmd.append('-q' + self.queue)
1599602SN/A
1609602SN/A        if self.afterok:
1619508SN/A            self.cmd.append('-Wdepend=afterok:%s' % self.afterok)
1629465SN/A
1639602SN/A        self.cmd.extend(args)
16410893Snilay@cs.wisc.edu        self.script = script
1659465SN/A        self.command = ' '.join(self.cmd + [ self.script ])
1668943SN/A
1678943SN/A    def do(self):
16811025Snilay@cs.wisc.edu        pbs = MyPOpen(self.cmd + [ self.script ])
1697567SN/A        self.result = pbs.fromchild.read()
1707567SN/A        ec = pbs.wait()
1716145SN/A
17211021Sjthestness@gmail.com        if ec != 0 and self.pbshost:
1739503SN/A            cmd = ' '.join(self.cmd + [ '-' ])
1749770SN/A            cmd = [ 'ssh', '-x', self.pbshost, cmd ]
1756145SN/A            self.command = ' '.join(cmd)
17610917Sbrandon.potter@amd.com            ssh = MyPOpen(cmd, input = self.script)
1777039SN/A            self.result = ssh.fromchild.read()
1789503SN/A            ec = ssh.wait()
17910097SN/A
18010097SN/A        return ec
18110097SN/A