1 #!/usr/bin/env python2.7
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.
28 import os
, os
.path
, shutil
, signal
, socket
, sys
29 from os
import environ
as env
30 from os
.path
import join
as joinpath
, expanduser
34 return time
.strftime('%a %b %e %H:%M:%S %Z %Y', time
.localtime())
37 for root
, dirs
, files
in os
.walk(dir, False):
39 os
.remove(joinpath(root
, name
))
41 os
.rmdir(joinpath(root
, name
))
52 def do(self
, src
, dst
):
57 args
.append(self
.rsync
)
63 args
.append('--delete')
65 args
.append(self
.options
)
69 return os
.spawnvp(os
.P_WAIT
, args
[0], args
)
72 def __init__(self
, dir):
75 def file(self
, filename
):
76 return joinpath(self
.dir, filename
)
79 if os
.path
.exists(self
.dir):
80 if not os
.path
.isdir(self
.dir):
81 sys
.exit('%s is not a directory. Cannot build job' % self
.dir)
86 return os
.path
.isdir(self
.dir)
91 def hasfile(self
, filename
):
92 return os
.path
.isfile(self
.file(filename
))
94 def echofile(self
, filename
, string
):
95 filename
= self
.file(filename
)
97 f
= file(filename
, 'w')
104 def rmfile(self
, filename
):
105 filename
= self
.file(filename
)
106 if os
.path
.isfile(filename
):
109 def readval(self
, filename
):
110 filename
= self
.file(filename
)
111 f
= file(filename
, 'r')
112 value
= f
.readline().strip()
116 def setstatus(self
, string
):
117 filename
= self
.file('.status')
119 f
= file(filename
, 'a')
127 filename
= self
.file('.status')
129 f
= file(filename
, 'r')
133 # fast forward to the end
136 # the first word on the last line is the status
137 return line
.split(' ')[0]
142 if __name__
== '__main__':
144 binaries
= { 'i686' : 'm5.i386',
145 'x86_64' : 'm5.amd64' }
146 binary
= binaries
[platform
.machine()]
149 rootdir
= env
.setdefault('ROOTDIR', os
.path
.dirname(cwd
))
150 oar_jobid
= int(env
['OAR_JOBID'])
151 oar_jobname
= os
.path
.basename(cwd
)
152 #pbs_jobname = env['PBS_JOBNAME']
153 basedir
= joinpath(rootdir
, 'Base')
154 jobname
= env
.setdefault('JOBNAME', oar_jobname
)
155 jobfile
= env
.setdefault('JOBFILE', joinpath(rootdir
, 'Test.py'))
156 outdir
= env
.setdefault('OUTPUT_DIR', cwd
)
157 env
['POOLJOB'] = 'True'
159 if os
.path
.isdir("/work"):
164 workdir
= joinpath(workbase
, '%s.%s' % (env
['USER'], oar_jobid
))
165 host
= socket
.gethostname()
169 jobdir
= JobDir(outdir
)
172 jobdir
.echofile('.running', started
)
173 jobdir
.rmfile('.queued')
174 jobdir
.echofile('.host', host
)
176 jobdir
.setstatus('running on %s on %s' % (host
, started
))
178 if os
.path
.isdir(workdir
):
183 if False and os
.path
.isdir('/z/dist'):
187 sync
.do('poolfs::dist/m5/', '/z/dist/m5/')
194 os
.symlink(jobdir
.file('output'), 'status.out')
196 args
= [ joinpath(basedir
, binary
), joinpath(basedir
, 'run.py') ]
198 sys
.exit("no arguments")
200 print 'starting job... %s' % started
209 fd
= os
.open(jobdir
.file("output"),
210 os
.O_WRONLY | os
.O_CREAT | os
.O_TRUNC
)
211 os
.dup2(fd
, sys
.stdout
.fileno())
212 os
.dup2(fd
, sys
.stderr
.fileno())
213 os
.execvp(args
[0], args
)
215 def handler(signum
, frame
):
217 os
.kill(childpid
, signum
)
219 signal
.signal(signal
.SIGHUP
, handler
)
220 signal
.signal(signal
.SIGINT
, handler
)
221 signal
.signal(signal
.SIGQUIT
, handler
)
222 signal
.signal(signal
.SIGTERM
, handler
)
223 signal
.signal(signal
.SIGCONT
, handler
)
224 signal
.signal(signal
.SIGUSR1
, handler
)
225 signal
.signal(signal
.SIGUSR2
, handler
)
230 thepid
,ec
= os
.waitpid(childpid
, 0)
232 print 'Exit code ', ec
241 print '\njob complete... %s' % complete
242 jobdir
.echofile('.%s' % status
, complete
)
243 jobdir
.rmfile('.running')
244 jobdir
.setstatus('%s on %s' % (status
, complete
))