pbs.py revision 1376:190de61fed5a
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 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) 39 40 return os.spawnvp(os.P_WAIT, args[0], args) 41 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 = {} 59 self.onlyecho = False 60 self.verbose = False 61 62 def do(self, script, ): 63 args = [self.qsub] 64 65 if self.env: 66 arg = '-v' 67 arg += ','.join([ '%s=%s' % i for i in self.env.iteritems() ]) 68 args.append(arg) 69 70 if self.hold: 71 args.append('-h') 72 73 if len(self.stdout): 74 args.append('-olocalhost:' + self.stdout) 75 76 if self.keep_stdout and self.keep_stderr: 77 args.append('-koe') 78 elif self.keep_stdout: 79 args.append('-ko') 80 elif self.keep_stderr: 81 args.append('-ke') 82 else: 83 args.append('-kn') 84 85 if self.join: 86 args.append('-joe') 87 88 if len(self.node_type): 89 args.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): 100 args.append('-m ' + flags) 101 102 if len(self.name): 103 args.append("-N%s" % self.name) 104 105 if self.priority != 0: 106 args.append('-p' + self.priority) 107 108 if len(self.queue): 109 args.append('-q' + self.queue) 110 111 args.append(script) 112 113 if self.verbose or self.onlyecho: 114 print >>sys.stderr, 'PBS Command: ', ' '.join(args) 115 116 if self.onlyecho: 117 return 0 118 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 127