2 # Copyright (c) 2006 The Regents of The University of Michigan
5 # Redistribution and use in source and binary forms, with or without
6 # modification, are permitted provided that the following conditions are
7 # met: redistributions of source code must retain the above copyright
8 # notice, this list of conditions and the following disclaimer;
9 # redistributions in binary form must reproduce the above copyright
10 # notice, this list of conditions and the following disclaimer in the
11 # documentation and/or other materials provided with the distribution;
12 # neither the name of the copyright holders nor the names of its
13 # contributors may be used to endorse or promote products derived from
14 # this software without specific prior written permission.
16 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 import os
, os
.path
, shutil
, signal
, socket
, sys
31 from os
import environ
as env
32 from os
.path
import join
as joinpath
, expanduser
36 return time
.strftime('%a %b %e %H:%M:%S %Z %Y', time
.localtime())
39 for root
, dirs
, files
in os
.walk(dir, False):
41 os
.remove(joinpath(root
, name
))
43 os
.rmdir(joinpath(root
, name
))
54 def do(self
, src
, dst
):
59 args
.append(self
.rsync
)
65 args
.append('--delete')
67 args
.append(self
.options
)
71 return os
.spawnvp(os
.P_WAIT
, args
[0], args
)
74 def __init__(self
, dir):
77 def file(self
, filename
):
78 return joinpath(self
.dir, filename
)
81 if os
.path
.exists(self
.dir):
82 if not os
.path
.isdir(self
.dir):
83 sys
.exit('%s is not a directory. Cannot build job' % self
.dir)
88 return os
.path
.isdir(self
.dir)
93 def hasfile(self
, filename
):
94 return os
.path
.isfile(self
.file(filename
))
96 def echofile(self
, filename
, string
):
97 filename
= self
.file(filename
)
99 f
= file(filename
, 'w')
106 def rmfile(self
, filename
):
107 filename
= self
.file(filename
)
108 if os
.path
.isfile(filename
):
111 def readval(self
, filename
):
112 filename
= self
.file(filename
)
113 f
= file(filename
, 'r')
114 value
= f
.readline().strip()
118 def setstatus(self
, string
):
119 filename
= self
.file('.status')
121 f
= file(filename
, 'a')
129 filename
= self
.file('.status')
131 f
= file(filename
, 'r')
135 # fast forward to the end
138 # the first word on the last line is the status
139 return line
.split(' ')[0]
144 if __name__
== '__main__':
146 binaries
= { 'i686' : 'm5.i386',
147 'x86_64' : 'm5.amd64' }
148 binary
= binaries
[platform
.machine()]
151 rootdir
= env
.setdefault('ROOTDIR', os
.path
.dirname(cwd
))
152 oar_jobid
= int(env
['OAR_JOBID'])
153 oar_jobname
= os
.path
.basename(cwd
)
154 #pbs_jobname = env['PBS_JOBNAME']
155 basedir
= joinpath(rootdir
, 'Base')
156 jobname
= env
.setdefault('JOBNAME', oar_jobname
)
157 jobfile
= env
.setdefault('JOBFILE', joinpath(rootdir
, 'Test.py'))
158 outdir
= env
.setdefault('OUTPUT_DIR', cwd
)
159 env
['POOLJOB'] = 'True'
161 if os
.path
.isdir("/work"):
166 workdir
= joinpath(workbase
, '%s.%s' % (env
['USER'], oar_jobid
))
167 host
= socket
.gethostname()
171 jobdir
= JobDir(outdir
)
174 jobdir
.echofile('.running', started
)
175 jobdir
.rmfile('.queued')
176 jobdir
.echofile('.host', host
)
178 jobdir
.setstatus('running on %s on %s' % (host
, started
))
180 if os
.path
.isdir(workdir
):
185 if False and os
.path
.isdir('/z/dist'):
189 sync
.do('poolfs::dist/m5/', '/z/dist/m5/')
196 os
.symlink(jobdir
.file('output'), 'status.out')
198 args
= [ joinpath(basedir
, binary
), joinpath(basedir
, 'run.py') ]
200 sys
.exit("no arguments")
202 print 'starting job... %s' % started
211 fd
= os
.open(jobdir
.file("output"),
212 os
.O_WRONLY | os
.O_CREAT | os
.O_TRUNC
)
213 os
.dup2(fd
, sys
.stdout
.fileno())
214 os
.dup2(fd
, sys
.stderr
.fileno())
215 os
.execvp(args
[0], args
)
217 def handler(signum
, frame
):
219 os
.kill(childpid
, signum
)
221 signal
.signal(signal
.SIGHUP
, handler
)
222 signal
.signal(signal
.SIGINT
, handler
)
223 signal
.signal(signal
.SIGQUIT
, handler
)
224 signal
.signal(signal
.SIGTERM
, handler
)
225 signal
.signal(signal
.SIGCONT
, handler
)
226 signal
.signal(signal
.SIGUSR1
, handler
)
227 signal
.signal(signal
.SIGUSR2
, handler
)
232 thepid
,ec
= os
.waitpid(childpid
, 0)
234 print 'Exit code ', ec
243 print '\njob complete... %s' % complete
244 jobdir
.echofile('.%s' % status
, complete
)
245 jobdir
.rmfile('.running')
246 jobdir
.setstatus('%s on %s' % (status
, complete
))