1 |
#!/usr/bin/python
|
2 |
|
3 |
import sys, os, getopt, popen2, fcntl, select, string, glob
|
4 |
|
5 |
def makeNonBlocking(fd):
|
6 |
fl = fcntl.fcntl(fd, fcntl.F_GETFL)
|
7 |
try:
|
8 |
fcntl.fcntl(fd, fcntl.F_SETFL, fl | os.O_NDELAY)
|
9 |
except AttributeError:
|
10 |
fcntl.fcntl(fd, fcntl.F_SETFL, fl | os.FNDELAY)
|
11 |
|
12 |
def runCommand(cmd, printout=0, timeout=-1):
|
13 |
"""
|
14 |
Run command 'cmd'.
|
15 |
Returns command stdoutput+stderror string on success,
|
16 |
or None if an error occurred.
|
17 |
Following recipe on http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/52296
|
18 |
"""
|
19 |
|
20 |
child = popen2.Popen3(cmd, 1) # capture stdout and stderr from command
|
21 |
child.tochild.close() # don't need to talk to child
|
22 |
outfile = child.fromchild
|
23 |
outfd = outfile.fileno()
|
24 |
errfile = child.childerr
|
25 |
errfd = errfile.fileno()
|
26 |
makeNonBlocking(outfd) # don't deadlock!
|
27 |
makeNonBlocking(errfd)
|
28 |
outdata = []
|
29 |
errdata = []
|
30 |
outeof = erreof = 0
|
31 |
|
32 |
if timeout > 0 :
|
33 |
maxwaittime = time.time() + timeout
|
34 |
|
35 |
err = -1
|
36 |
while (timeout == -1 or time.time() < maxwaittime):
|
37 |
ready = select.select([outfd,errfd],[],[]) # wait for input
|
38 |
if outfd in ready[0]:
|
39 |
outchunk = outfile.read()
|
40 |
if outchunk == '': outeof = 1
|
41 |
outdata.append(outchunk)
|
42 |
if errfd in ready[0]:
|
43 |
errchunk = errfile.read()
|
44 |
if errchunk == '': erreof = 1
|
45 |
errdata.append(errchunk)
|
46 |
if outeof and erreof:
|
47 |
err = child.wait()
|
48 |
break
|
49 |
select.select([],[],[],.1) # give a little time for buffers to fill
|
50 |
if err == -1:
|
51 |
# kill the pid
|
52 |
os.kill (child.pid, 9)
|
53 |
err = child.wait()
|
54 |
|
55 |
cmd_out = string.join(outdata,"")
|
56 |
cmd_err = string.join(errdata,"")
|
57 |
|
58 |
if err:
|
59 |
return None
|
60 |
|
61 |
cmd_out = cmd_out + cmd_err
|
62 |
return cmd_out
|
63 |
|
64 |
|
65 |
|
66 |
def main(argv) :
|
67 |
"""
|
68 |
robot_pack
|
69 |
|
70 |
pack site directories for specific day and copy it to dCache
|
71 |
|
72 |
required parameters:
|
73 |
--day <day> : day in state/init/task directory format: YYMMDD
|
74 |
--mode <mode> : JobRobot mode: condor_g or rb1 or rb2
|
75 |
|
76 |
optional parameters:
|
77 |
--local_dir <dir> : local base directory, default /home/csaba/robot_pack
|
78 |
--dcache_dir <dir> : dcache base directory, default /pnfs/cms/WAX/resilient/csaba/jobrobot/
|
79 |
--help (-h) : help
|
80 |
--debug (-d) : debug statements
|
81 |
|
82 |
"""
|
83 |
|
84 |
# defaults
|
85 |
debug = 0
|
86 |
day = ''
|
87 |
mode = ''
|
88 |
local_dir = '/home/csaba/robot_pack'
|
89 |
dcache_dir = '/pnfs/cms/WAX/resilient/csaba/jobrobot'
|
90 |
|
91 |
|
92 |
try:
|
93 |
opts, args = getopt.getopt(argv, "", ["help", "debug", "day=", "mode=", "dcache_dir="])
|
94 |
except getopt.GetoptError:
|
95 |
print main.__doc__
|
96 |
sys.exit(2)
|
97 |
|
98 |
# check command line parameter
|
99 |
for opt, arg in opts :
|
100 |
if opt == "--help" :
|
101 |
print main.__doc__
|
102 |
sys.exit()
|
103 |
elif opt == "--day" :
|
104 |
day = arg
|
105 |
elif opt == "--mode" :
|
106 |
mode = arg
|
107 |
elif opt == "--dcache_dir" :
|
108 |
dcache_dir = arg
|
109 |
elif opt == "--local_dir" :
|
110 |
local_dir = arg
|
111 |
elif opt == "--debug" :
|
112 |
debug = 1
|
113 |
|
114 |
if day == '' :
|
115 |
print main.__doc__
|
116 |
sys.exit()
|
117 |
|
118 |
if mode == '' :
|
119 |
print main.__doc__
|
120 |
sys.exit()
|
121 |
|
122 |
if mode != 'condor_g' and mode != 'rb1' and mode != 'rb2' :
|
123 |
print main.__doc__
|
124 |
sys.exit()
|
125 |
|
126 |
for dir in glob.glob('state/init/tasks/'+day+'/*') :
|
127 |
|
128 |
# make day directory in pnfs
|
129 |
cmd = 'mkdir -p '+dcache_dir+'/'+mode+'/'+day
|
130 |
if debug :
|
131 |
print cmd
|
132 |
else :
|
133 |
cmd_out = runCommand(cmd)
|
134 |
|
135 |
# make day directory locally
|
136 |
cmd = 'mkdir -p '+local_dir+'/'+day+'/'+mode
|
137 |
if debug :
|
138 |
print cmd
|
139 |
else :
|
140 |
cmd_out = runCommand(cmd)
|
141 |
|
142 |
# tar found directories to tmp
|
143 |
name = dir.split('/')[-1]
|
144 |
cmd = 'tar czvf '+local_dir+'/'+day+'/'+mode+'/'+name+'.tgz '+dir
|
145 |
if debug :
|
146 |
print cmd
|
147 |
else :
|
148 |
cmd_out = runCommand(cmd)
|
149 |
|
150 |
# dccp to dcache
|
151 |
cmd = 'dccp '+local_dir+'/'+day+'/'+mode+'/'+name+'.tgz '+dcache_dir+'/'+mode+'/'+day
|
152 |
if debug :
|
153 |
print cmd
|
154 |
else :
|
155 |
cmd_out = runCommand(cmd)
|
156 |
|
157 |
# remove local dir
|
158 |
cmd = 'rm -f '+local_dir+'/'+day+'/'+mode+'/'+name+'.tgz'
|
159 |
if debug :
|
160 |
print cmd
|
161 |
else :
|
162 |
cmd_out = runCommand(cmd)
|
163 |
|
164 |
# srmcp contact msg for fnal
|
165 |
|
166 |
print 'srmcp srm://cmssrm.fnal.gov:8443/resilient/csaba/jobrobot/'+mode+'/'+day+'/'+name+'.tgz file:////`pwd`/'+name+'.tgz'
|
167 |
|
168 |
|
169 |
if __name__ == '__main__' :
|
170 |
main(sys.argv[1:])
|