pbs.py revision 1928:b75ae11a5e8f
1# Copyright (c) 2005 The Regents of The University of Michigan 2# All rights reserved. 3# 4# Redistribution and use in source and binary forms, with or without 5# modification, are permitted provided that the following conditions are 6# met: redistributions of source code must retain the above copyright 7# notice, this list of conditions and the following disclaimer; 8# redistributions in binary form must reproduce the above copyright 9# notice, this list of conditions and the following disclaimer in the 10# documentation and/or other materials provided with the distribution; 11# neither the name of the copyright holders nor the names of its 12# contributors may be used to endorse or promote products derived from 13# this software without specific prior written permission. 14# 15# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 16# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 17# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 18# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 19# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 20# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 21# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26# 27# Authors: Nathan Binkert 28 29import os, popen2, re, sys 30 31class MyPOpen(object): 32 def __init__(self, cmd, input = None, output = None, bufsize = -1): 33 self.status = -1 34 35 if input is None: 36 p2c_read, p2c_write = os.pipe() 37 self.tochild = os.fdopen(p2c_write, 'w', bufsize) 38 else: 39 p2c_write = None 40 if isinstance(input, file): 41 p2c_read = input.fileno() 42 elif isinstance(input, str): 43 input = file(input, 'r') 44 p2c_read = input.fileno() 45 elif isinstance(input, int): 46 p2c_read = input 47 else: 48 raise AttributeError 49 50 if output is None: 51 c2p_read, c2p_write = os.pipe() 52 self.fromchild = os.fdopen(c2p_read, 'r', bufsize) 53 else: 54 c2p_read = None 55 if isinstance(output, file): 56 c2p_write = output.fileno() 57 elif isinstance(output, str): 58 output = file(output, 'w') 59 c2p_write = output.fileno() 60 elif isinstance(output, int): 61 c2p_write = output 62 else: 63 raise AttributeError 64 65 self.pid = os.fork() 66 if self.pid == 0: 67 os.dup2(p2c_read, sys.stdin.fileno()) 68 os.dup2(c2p_write, sys.stdout.fileno()) 69 os.dup2(c2p_write, sys.stderr.fileno()) 70 try: 71 os.execvp(cmd[0], cmd) 72 finally: 73 os._exit(1) 74 75 os.close(p2c_read) 76 os.close(c2p_write) 77 78 def poll(self): 79 if self.status < 0: 80 pid, status = os.waitpid(self.pid, os.WNOHANG) 81 if pid == self.pid: 82 self.status = status 83 return self.status 84 85 def wait(self): 86 if self.status < 0: 87 pid, status = os.waitpid(self.pid, 0) 88 if pid == self.pid: 89 self.status = status 90 return self.status 91 92class qsub: 93 def __init__(self): 94 self.afterok = None 95 self.hold = False 96 self.join = False 97 self.keep_stdout = False 98 self.keep_stderr = False 99 self.node_type = None 100 self.mail_abort = False 101 self.mail_begin = False 102 self.mail_end = False 103 self.name = None 104 self.stdout = None 105 self.priority = None 106 self.queue = None 107 self.pbshost = None 108 self.qsub = 'qsub' 109 self.env = {} 110 111 def build(self, script, args = []): 112 self.cmd = [ self.qsub ] 113 114 if self.env: 115 arg = '-v' 116 arg += ','.join([ '%s=%s' % i for i in self.env.iteritems() ]) 117 self.cmd.append(arg) 118 119 if self.hold: 120 self.cmd.append('-h') 121 122 if self.stdout: 123 self.cmd.append('-olocalhost:' + self.stdout) 124 125 if self.keep_stdout and self.keep_stderr: 126 self.cmd.append('-koe') 127 elif self.keep_stdout: 128 self.cmd.append('-ko') 129 elif self.keep_stderr: 130 self.cmd.append('-ke') 131 else: 132 self.cmd.append('-kn') 133 134 if self.join: 135 self.cmd.append('-joe') 136 137 if self.node_type: 138 self.cmd.append('-lnodes=' + self.node_type) 139 140 if self.mail_abort or self.mail_begin or self.mail_end: 141 flags = '' 142 if self.mail_abort: 143 flags.append('a') 144 if self.mail_begin: 145 flags.append('b') 146 if self.mail_end: 147 flags.append('e') 148 if len(flags): 149 self.cmd.append('-m ' + flags) 150 151 if self.name: 152 self.cmd.append("-N%s" % self.name) 153 154 if self.priority: 155 self.cmd.append('-p' + self.priority) 156 157 if self.queue: 158 self.cmd.append('-q' + self.queue) 159 160 if self.afterok: 161 self.cmd.append('-Wdepend=afterok:%s' % self.afterok) 162 163 self.cmd.extend(args) 164 self.script = script 165 self.command = ' '.join(self.cmd + [ self.script ]) 166 167 def do(self): 168 pbs = MyPOpen(self.cmd + [ self.script ]) 169 self.result = pbs.fromchild.read() 170 ec = pbs.wait() 171 172 if ec != 0 and self.pbshost: 173 cmd = ' '.join(self.cmd + [ '-' ]) 174 cmd = [ 'ssh', '-x', self.pbshost, cmd ] 175 self.command = ' '.join(cmd) 176 ssh = MyPOpen(cmd, input = self.script) 177 self.result = ssh.fromchild.read() 178 ec = ssh.wait() 179 180 return ec 181