2 from __future__
import print_function
3 from builtins
import range
4 import os, time, sys, glob, re, shutil, stat, smtplib, socket
5 from email.MIMEText
import MIMEText
6 from fcntl
import lockf, LOCK_EX, LOCK_UN
7 from hashlib
import md5
8 from traceback
import print_exc, format_exc
9 from datetime
import datetime
10 from subprocess
import Popen,PIPE
11 sys.stdout = os.fdopen(sys.stdout.fileno(),
'w', 0)
14 COLLECTDIR = sys.argv[2]
15 TFILEDONEDIR = sys.argv[3]
20 EMAILINTERVAL = 15 * 60
21 SBASEDIR = os.path.abspath(__file__).rsplit(
"/",1)[0]
22 TMPDROPBOX =
"%s/.tmpdropbox" % DROPBOX
24 SENDMAIL =
"/usr/sbin/sendmail"
25 HOSTNAME = socket.gethostname().lower()
28 lastEmailSent = datetime.now()
32 procid =
"[%s/%d]" % (__file__.rsplit(
"/", 1)[-1], os.getpid())
33 print(datetime.now(), procid, msg % args)
36 cmd =
'root -l -b -q %s/filechk.C"(\\"%s\\")"' % (SBASEDIR,rootfile)
39 if tag ==
'(int)(-1)' or tag ==
'(int)0':
48 cmd =
'root -l -b -q %s/sistrip_reduce_file.C++"' \
49 '(\\"%s\\", \\"%s\\")" >& /dev/null' % (SBASEDIR,infile, ofile)
52 def sendmail(body="Hello from visDQMZipCastorVerifier"):
53 scall = Popen(
"%s -t" % SENDMAIL, shell=
True, stdin=PIPE)
54 scall.stdin.write(
"To: %s\n" % EMAIL)
55 scall.stdin.write(
"Subject: File Collector on server %s has a Critical Error\n" %
57 scall.stdin.write(
"\n")
58 scall.stdin.write(
"%s\n" % body)
62 logme(
"ERROR: Sendmail exit with status %s", rc)
65 if not os.path.exists(TMPDROPBOX):
66 os.makedirs(TMPDROPBOX)
68 if not os.path.exists(TFILEDONEDIR):
69 os.makedirs(TFILEDONEDIR)
71 if not os.path.exists(DROPBOX):
80 for dir, subdirs, files
in os.walk(COLLECTDIR):
82 fMatch=re.match(
'^DQM_V[0-9]{4}_(?P<subsys>.*)_R(?P<runnr>[0-9]{9})(|_T[0-9]*)\.root$',f)
84 fMatch=re.match(
'^Playback_V[0-9]{4}_(?P<subsys>.*)_R(?P<runnr>[0-9]{9})(|_T[0-9]*)\.root$', f)
87 runnr =
int(fMatch.group(
"runnr"))
88 subsystem=fMatch.group(
"subsys")
90 donefile =
"%s/%s/%s/%s" % (TFILEDONEDIR, runstr[0:3], runstr[3:6], f)
91 f =
"%s/%s" % (dir, f)
92 if os.path.exists(donefile)
and os.stat(donefile).st_size == os.stat(f).st_size:
93 logme(
"WARNING: File %s was already processed but re-appeared", f)
97 NEW.setdefault(runnr, {}).setdefault(subsystem,[]).
append(f)
104 TAGS=sorted(glob.glob(
'%s/tagfile_runend_*' % COLLECTDIR ),reverse=
True)
110 TAGRUNEND=long(sorted(NEW.keys(),reverse=
True)[1])
113 TAGRUNEND=long(TAGS[0].
split(
"_")[2])
118 for run,subsystems
in NEW.items():
122 for subsystem,files
in subsystems.items():
125 Tfiles=sorted(files,cmp=
lambda x,y:
"_T" not in x
and x != y
and 1
or cmp(x,y))[::-1]
127 seed=HOSTNAME.replace(
"-",
"t")[-6:]
128 finalTMPfile=
"%s/DQM_V0001_%s_R%09d.root.%s" % (TMPDROPBOX,subsystem,run,seed)
130 finalTfile=
"%s/%s/%s/%s" % (TFILEDONEDIR,runstr[0:3],runstr[3:6],Tfile.split(
"/")[-1])
131 finalTdir=
"%s/%s/%s" % (TFILEDONEDIR,runstr[0:3],runstr[3:6])
132 if not os.path.exists(finalTdir):
133 os.makedirs(finalTdir)
135 if os.path.exists(finalTMPfile):
136 os.remove(finalTMPfile)
141 shutil.move(Tfile,finalTfile+
"_d")
149 logme(
"INFO: File %s is incomplete looking for next"
150 " DQM_V*_%s_R%09d_T*.root valid file",
151 Tfile, subsystem, run)
154 shutil.move(Tfile,finalTfile+
"_d")
161 if "Playback" in Tfile
and "SiStrip" in Tfile:
162 dqmfile = Tfile.replace(
'Playback',
'DQM')
164 if not os.path.exists(dqmfile):
165 logme(
"WARNING: Problem converting %s skiping", Tfile)
166 shutil.move(Tfile,finalTfile+
"_d")
169 os.rename(Tfile,finalTfile.replace(
'Playback',
'Playback_full'))
172 for i
in range(RETRIES):
174 originStr=
"md5:%s %d %s" % (md5Digest.hexdigest(),os.stat(Tfile).st_size,Tfile)
175 originTMPFile=
"%s.origin" % finalTMPfile
176 originFile=open(originTMPFile,
"w")
177 originFile.write(originStr)
179 shutil.copy(Tfile,finalTMPfile)
181 lFile=open(
"%s/lock" % TMPDROPBOX ,
"a")
183 for vdir,vsubdir,vfiles
in os.walk(DROPBOX):
184 if 'DQM_V0001_%s_R%09d.root' % (subsystem,run)
not in vfiles:
188 if not os.path.exists(
"%s/V%04d" % (DROPBOX,version)):
189 os.makedirs(
"%s/V%04d" % (DROPBOX,version))
191 finalfile=
"%s/V%04d/DQM_V0001_%s_R%09d.root" % (DROPBOX,version,subsystem,run)
192 originFileName=
"%s.origin" % finalfile
193 if os.path.exists(finalTMPfile)
and os.stat(finalTMPfile).st_size == os.stat(Tfile).st_size:
194 os.rename(Tfile,finalTfile)
195 os.rename(finalTMPfile,finalfile)
196 os.rename(originTMPFile,originFileName)
197 os.chmod(finalfile,stat.S_IREAD|stat.S_IRGRP|stat.S_IROTH| stat.S_IWRITE|stat.S_IWGRP|stat.S_IWOTH)
198 os.chmod(originFileName,stat.S_IREAD|stat.S_IRGRP|stat.S_IROTH| stat.S_IWRITE|stat.S_IWGRP|stat.S_IWOTH)
199 logme(
"INFO: File %s has been successfully sent to the DROPBOX" , Tfile)
204 logme(
"ERROR: Problem transfering final file for run"
205 " %09d. Retrying in %d", run, WAITTIME)
208 if now - EMAILINTERVAL > lastEmailSent:
209 sendmail(
"ERROR: Problem transfering final file for run"
210 " %09d.\n Retrying in %d seconds" % (run, WAITTIME))
218 except KeyboardInterrupt
as e:
221 except Exception
as e:
222 logme(
'ERROR: %s', e)
224 if now - EMAILINTERVAL > lastEmailSent:
225 sendmail (
'ERROR: %s\n%s' % (e, format_exc()))