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
| 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, re, sys
| 29import os, popen2, re, sys
|
30
| 30
|
31def ssh(host, script, tty = False, user = ''): 32 args = [ 'ssh', '-x' ] 33 if user: 34 args.append('-l' + user) 35 if tty: 36 args.append('-t') 37 args.append(host) 38 args.append(script)
| 31class MyPOpen(object): 32 def __init__(self, cmd, input = None, output = None, bufsize = -1): 33 self.sts = -1
|
39
| 34
|
40 return os.spawnvp(os.P_WAIT, args[0], args)
| 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
|
41
| 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, 0) 68 os.dup2(c2p_write, 1) 69 os.dup2(c2p_write, 2) 70 if isinstance(cmd, basestring): 71 cmd = ['/bin/sh', '-c', cmd] 72 if False: 73 for i in range(3, MAXFD): 74 try: 75 os.close(i) 76 except OSError: 77 pass 78 try: 79 os.execvp(cmd[0], cmd) 80 finally: 81 os._exit(1) 82 83 os.close(p2c_read) 84 os.close(c2p_write) 85 86 def poll(self): 87 if self.sts < 0: 88 pid, sts = os.waitpid(self.pid, os.WNOHANG) 89 if pid == self.pid: 90 self.sts = sts 91 return self.sts 92 93 def wait(self): 94 if self.sts < 0: 95 pid, sts = os.waitpid(self.pid, 0) 96 if pid == self.pid: 97 self.sts = sts 98 return self.sts 99
|
42class qsub: 43 def __init__(self): 44 self.hold = False 45 self.join = False 46 self.keep_stdout = False 47 self.keep_stderr = False 48 self.node_type = '' 49 self.mail_abort = False 50 self.mail_begin = False 51 self.mail_end = False 52 self.name = '' 53 self.stdout = '' 54 self.priority = 0 55 self.queue = '' 56 self.pbshost = '' 57 self.qsub = 'qsub' 58 self.env = {}
| 100class qsub: 101 def __init__(self): 102 self.hold = False 103 self.join = False 104 self.keep_stdout = False 105 self.keep_stderr = False 106 self.node_type = '' 107 self.mail_abort = False 108 self.mail_begin = False 109 self.mail_end = False 110 self.name = '' 111 self.stdout = '' 112 self.priority = 0 113 self.queue = '' 114 self.pbshost = '' 115 self.qsub = 'qsub' 116 self.env = {}
|
59 self.onlyecho = False 60 self.verbose = False
| |
61
| 117
|
62 def do(self, script, ): 63 args = [self.qsub]
| 118 def build(self, script, args = []): 119 self.cmd = [ self.qsub ]
|
64 65 if self.env: 66 arg = '-v' 67 arg += ','.join([ '%s=%s' % i for i in self.env.iteritems() ])
| 120 121 if self.env: 122 arg = '-v' 123 arg += ','.join([ '%s=%s' % i for i in self.env.iteritems() ])
|
68 args.append(arg)
| 124 self.cmd.append(arg)
|
69 70 if self.hold:
| 125 126 if self.hold:
|
71 args.append('-h')
| 127 self.cmd.append('-h')
|
72 73 if len(self.stdout):
| 128 129 if len(self.stdout):
|
74 args.append('-olocalhost:' + self.stdout)
| 130 self.cmd.append('-olocalhost:' + self.stdout)
|
75 76 if self.keep_stdout and self.keep_stderr:
| 131 132 if self.keep_stdout and self.keep_stderr:
|
77 args.append('-koe')
| 133 self.cmd.append('-koe')
|
78 elif self.keep_stdout:
| 134 elif self.keep_stdout:
|
79 args.append('-ko')
| 135 self.cmd.append('-ko')
|
80 elif self.keep_stderr:
| 136 elif self.keep_stderr:
|
81 args.append('-ke')
| 137 self.cmd.append('-ke')
|
82 else:
| 138 else:
|
83 args.append('-kn')
| 139 self.cmd.append('-kn')
|
84 85 if self.join:
| 140 141 if self.join:
|
86 args.append('-joe')
| 142 self.cmd.append('-joe')
|
87 88 if len(self.node_type):
| 143 144 if len(self.node_type):
|
89 args.append('-lnodes=' + self.node_type)
| 145 self.cmd.append('-lnodes=' + self.node_type)
|
90 91 if self.mail_abort or self.mail_begin or self.mail_end: 92 flags = '' 93 if self.mail_abort: 94 flags.append('a') 95 if self.mail_begin: 96 flags.append('b') 97 if self.mail_end: 98 flags.append('e') 99 if len(flags):
| 146 147 if self.mail_abort or self.mail_begin or self.mail_end: 148 flags = '' 149 if self.mail_abort: 150 flags.append('a') 151 if self.mail_begin: 152 flags.append('b') 153 if self.mail_end: 154 flags.append('e') 155 if len(flags):
|
100 args.append('-m ' + flags)
| 156 self.cmd.append('-m ' + flags)
|
101 102 if len(self.name):
| 157 158 if len(self.name):
|
103 args.append("-N%s" % self.name)
| 159 self.cmd.append("-N%s" % self.name)
|
104 105 if self.priority != 0:
| 160 161 if self.priority != 0:
|
106 args.append('-p' + self.priority)
| 162 self.cmd.append('-p' + self.priority)
|
107 108 if len(self.queue):
| 163 164 if len(self.queue):
|
109 args.append('-q' + self.queue)
| 165 self.cmd.append('-q' + self.queue)
|
110
| 166
|
111 args.append(script)
| 167 self.cmd.extend(args) 168 self.script = script 169 self.command = ' '.join(self.cmd + [ self.script ])
|
112
| 170
|
113 if self.verbose or self.onlyecho: 114 print >>sys.stderr, 'PBS Command: ', ' '.join(args)
| 171 def do(self): 172 pbs = MyPOpen(self.cmd + [ self.script ]) 173 self.result = pbs.fromchild.read() 174 ec = pbs.wait()
|
115
| 175
|
116 if self.onlyecho: 117 return 0
| 176 if ec != 0 and self.pbshost: 177 cmd = ' '.join(self.cmd + [ '-' ]) 178 cmd = [ 'ssh', '-x', self.pbshost, cmd ] 179 self.command = ' '.join(cmd) 180 ssh = MyPOpen(cmd, input = self.script) 181 self.result = ssh.fromchild.read() 182 ec = ssh.wait()
|
118
| 183
|
119 print >>sys.stderr, 'PBS Jobid: ', 120 121 ec = os.spawnvp(os.P_WAIT, args[0], args) 122 123 if ec != 0 and len(self.pbshost): 124 ec = ssh(self.pbshost, ' '.join(args)) 125
| |
126 return ec
| 184 return ec
|