4 import os, time, shutil, zipfile, commands, sys, glob
5 from datetime
import datetime
12 dir =
"/nfshome0/smaruyam/CMSSW_2_0_10/src/test/"
13 dbdir =
"/nfshome0/smaruyam/CMSSW_2_0_10/src/test/"
14 arcdir =
"/nfshome0/smaruyam/CMSSW_2_0_10/src/test/"
15 cfgfile =
" /nfshome0/smaruyam/CMSSW_2_0_10/src/test/myconfig.txt "
18 EnableFileRemoval =
False
20 EnableTransfer =
False
22 fileSizeThreshold = 1000000000
24 transferScript =
"/nfshome0/tier0/scripts/injectFileIntoTransferSystem.pl"
25 targetdir =
"/castor/cern.ch/cms/store/dqm/"
26 cfgarg =
" --config " + cfgfile
27 fullTransferArg = cfgarg +
" --type dqm --hostname srv-C2D05-19 --lumisection 1 --appname CMSSW --appversion CMSSW_2_0_10 "
28 statusCheck = cfgarg +
" --check --filename "
36 logfile = open(
'archival_log.txt',
'a')
37 tmpdb = dbdir +
"tmp/tmp.db"
38 bakdb = dbdir +
"tmp/backup.db"
47 Temporary Port form Hyunkwan's Un-Register-File Script
50 if os.path.exists(tmpdb): os.remove(tmpdb)
52 logfile.write(
'*** File Register ***\n')
53 logfile.write(os.popen(
'visDQMRegisterFile '+ tmpdb +
' "/Global/Online/ALL" "Global run" '+ file).
read())
55 tstamp = t.strftime(
"%Y%m%d")
56 a = glob.glob(bakdb+
'.'+tstamp+
'*');
58 tstamp = t.strftime(
"%Y%m%d_%H%M%S")
59 bakdb = bakdb+
'.'+tstamp
60 shutil.copy(tmpdb,bakdb)
66 if os.path.exists(tmpdb): os.remove(tmpdb)
68 logfile.write(
'*** File UnRegister ***\n')
69 logfile.write(os.popen(
'visDQMUnregisterFile '+ tmpdb +
' ' + oldfile).
read())
71 tstamp = t.strftime(
"%Y%m%d")
72 a = glob.glob(bakdb+
'.'+tstamp+
'*');
74 tstamp = t.strftime(
"%Y%m%d_%H%M%S")
75 bakdb = bakdb+
'.'+tstamp
76 shutil.copy(tmpdb,bakdb)
88 Check and Return Output
91 result = commands.getstatusoutput(cmd)
96 logfile.write(
"Command Exits with non-zero Status," + str(result[0]) +
" Error = " + result[1] +
"\n")
106 Reference to Cleaner()
107 df out put is assumed as follows.
108 Filesystem Size Used Avail Use% Mounted on
109 /dev/sda3 73G 45G 25G 65% /
110 /dev/sda1 99M 12M 83M 12% /boot
111 none 2.0G 0 2.0G 0% /dev/shm
112 /dev/sdb1 917G 83G 788G 10% /data
113 cmsnfshome0:/nfshome0
114 805G 673G 133G 84% /cmsnfshome0/nfshome0
117 logfile.write(
" *** Checking Disk Usage ***\n")
118 df_file=os.popen(
'df')
120 lines = df_file.readlines()
121 list = lines[4].
split()
122 string = list[4][:-1]
123 fusage = float(string)
124 if fusage > disk_threshold :
125 logfile.write(
"Disk Usage too high = " + string +
"%\n")
130 logfile.write(
"Disk Usage is low enough = " + string +
"%\n")
139 Reference to CheckPath(), filereg()
143 if cmp(path,emptyString) != 0:
144 newpath =
"rfio:" + path
145 logfile.write(
"Register New Path " + newpath +
"\n")
146 filereg(db,bakdb,tmpdb,newpath,logfile)
148 else :logfile.write(
"File Transferred, but not found on tape\n")
153 Reference to ConfirmPath(), ScanDir()
156 mtime = os.stat(filename).st_mtime
157 year = time.localtime(mtime)[0]
158 month = time.localtime(mtime)[1]
159 if month > 9: yearmonth = str(year) + str(month)
160 else: yearmonth = str(year) +
"0" + str(month)
161 path = targetdir + yearmonth
162 logfile.write(
"Best Guess for the path is " + path +
"\n")
164 if cmp(newpath,emptyString) != 0 :
return newpath
166 newpath =
ScanDir(filename, logfile)
170 Check File Path on Tape
171 Reference to CheckCommand()
174 logfile.write(
" *** Checking File Path ***\n ")
176 fullpath = path +
"/" + file[len(arcdir):]
183 if cmp(output,
"") != 0:
184 for line
in output.split(
"\n"):
185 error_check =
"No such file or directory"
186 if line.find(error_check) != -1 :
return emptyString
187 logfile.write(
" rfdir result is " + line +
"\n")
188 if len(line.split()) > 7:
189 string = line.split()[-1]
190 if cmp(string,fullpath) == 0:
return fullpath
194 Scan Castor Directories
195 Reference to ConfirmPath(), CheckCommand()
201 logfile.write(
"Scanning tape area " + cmd +
"\n")
204 if cmp(result[1],
"") != 0:
205 output = result[1].
split(
'\n')
207 if len(line.split()) > 8:
208 newpath = targetdir + line.split()[-1]
209 logfile.write(
"Looking for File at " + newpath +
"\n")
211 logfile.write(
"Returned Path " + confirmpath +
"\n")
212 if cmp(confirmpath, newpath +
"/" + file[len(arcdir):] ) == 0:
return confirmpath
221 Transfer File with T0 System
222 Reference to CheckCommand()
225 filename = filepath[len(arcdir):]
226 nrun = filepath[len(arcdir)+len(
"DQM_Online_R"):-len(
"_R000064807.zip")]
227 transfer_string = transferScript +
" --runnumber " + nrun +
" --path " + arcdir +
" --filename " + filename
228 if EnableTransfer
is False: transfer_string +=
" --test "
229 if flag
is True: transfer_string +=
" --renotify "
230 mycmd = transfer_string
231 myarg = fullTransferArg
235 output = result[1].
split(
'\n')
237 if line.find(
"File sucessfully submitted for transfer.") != -1
and flag
is False:
238 logfile.write(
"File is queued " + filepath +
"\n")
240 if line.find(
"File sucessfully re-submitted for transfer.") != -1
and flag
is True:
241 logfile.write(
"File is resubmitted " + filepath +
"\n")
243 if EnableTransfer
is False: logfile.write(
" *** Transfer Test Mode ***\n No File Transferred.\n")
247 Check File Status of Transferred File
248 Reference to CheckCommand(), TransferWithT0System()
251 filename = filepath[len(arcdir):]
252 checkString = statusCheck + filename
253 mycmd = transferScript
258 output = result[1].
split(
'\n')
260 if line.find(
"FILES_TRANS_CHECKED: File found in database and checked by T0 system.") != -1:
return True
261 elif line.find(
"File not found in database.") != -1:
265 elif line.find(
"FILES_INJECTED : File found in database and handed over to T0 system. ") != -1:
268 mtime = os.stat(filepath).st_mtime
269 logfile.write(
"Old M Time is " + mtime +
"\n")
270 os.utime(filepath,
None)
271 mtime2 = os.stat(filepath).st_mtime
272 logfile.write(
"New M Time is " + mtime2 +
"\n")
273 logfile.write(
"File transfer need more time, please wait!\n")
282 Get List of un-merged files, for Zipping
283 Reference to GetFileFromDB(), GetZippedFile()
286 logfile.write(
"Retrieving list of files from DB ...\n")
289 if PathReplace
is True:
291 for line
in fileList:
292 if cmp(line,
"") != 0
and cmp(line,emptyString) != 0:
293 string = line.rstrip().
split(
'|')
295 logfile.write(
"String just read is " + string +
"\n")
297 logfile.write(
"Number just read is " + number +
"\n")
298 totalSize += int(number)
299 logfile.write(
"Current File Size Sum is " + str(totalSize) +
" out of Limit" + str(fileSizeThreshold) +
"\n")
300 zipFileList +=
" " + name
301 if totalSize > fileSizeThreshold:
303 if PathReplace
is False:
308 if len(mergedfiles) > 0:
309 if cmp(mergedfiles[0],
"") != 0
and cmp(mergedfiles[0],emptyString) != 0:
310 lastfile = mergedfiles[0]
311 logfile.write(
"Last Merged Zip File is " + lastfile +
"\n")
312 elif cmp(mergedfiles[0],
"") == 0:
314 logfile.write(
"No Merged Zip File \n")
315 if len(mergedfiles) == 0:
317 logfile.write(
"No Merged Zip File \n")
319 for line
in fileList:
320 if cmp(line,
"") != 0
and cmp(line,emptyString) != 0:
321 string = line.split(
'|')
324 logfile.write(
"Name just read is " + name +
"\n")
326 logfile.write(
"Number just read is " + number +
"\n")
327 totalSize += int(number)
328 logfile.write(
"Current File Size Sum is " + str(totalSize) +
" out of Limit" + str(fileSizeThreshold) +
"\n")
329 zipFileList +=
" " + name
330 if totalSize > fileSizeThreshold:
332 if activate
is False and cmp(lastfile,
"") !=0:
333 if cmp(lastfile[len(arcdir)+len(
"DQM_Online_R000064821_"):-len(
".zip")],name[len(dir)+len(
"DQM_V0001_R000064821_"):-len(
".root")]) == 0:
338 Read and sort file from db, for Zipping
339 Reference to CheckCommand()
342 logfile.write(
" *** Getting Per-Run File List from Master DB ***\n")
343 string =
"'%DQM_V%_R%.root'"
346 sqlite =
" %s \"select name, size from t_files where name like %s and not name like %s and not name like %s order by mtime asc\" " %(db, string, search1, search2)
354 logfile.write(result[1])
358 Get the last merged File, for Zipping
359 Reference to CheckCommand()
362 logfile.write(
" *** Getting Zipped File List from Master DB ***\n")
363 string =
"'%DQM%.zip'"
364 if flag
is True: sqlite =
" %s \"select name from t_files where name like %s order by mtime desc\" " %(db, string)
365 if flag
is False: sqlite =
" %s \"select name from t_files where name like %s order by mtime asc\" " %(db, string)
372 else:
return emptyString
375 Getting All Files from DB, for File Removal
376 Reference to CheckCommand()
379 logfile.write(
" *** Getting All Files from db ***\n")
380 sqlite = db +
" \"select name from t_files where name like '%DQM%.root' or name like '%DQM%.zip'order by mtime asc\""
386 output = result[1].
split(
'\n')
388 else :
return emptyString
396 File Cleaner, Remove the oldest file
397 Reference to GetAllFiles(), CheckFileStatus(), SetPath(), Delete(), CheckZippedFiles()
400 logfile.write(
" *** Cleaning File ***\n")
403 if file.find(
".zip") != -1:
405 if status
is True and PathReplace
is True:
406 pathfind =
SetPath(file, logfile)
407 if pathfind
is True :
410 if file.find(
".root") != -1
and file.find(dir) != -1:
412 if PathReplace
is True:
Delete(file, logfile)
414 else : logfile.write(
"No File to be removed!\n")
417 Remove File if zipped
418 Reference to Delete()
421 logfile.write(
" *** Check Zipped File ***\n")
424 if len(mergedfiles) > 0:
425 for thisfile
in mergedfiles:
426 if thisfile.find(
"zip") != -1
and cmp(thisfile,
"") != 0
and cmp(thisfile,emptyString) != 0:
427 zip = zipfile.ZipFile(thisfile,
"r")# open file to see it readable
428 for info
in zip.infolist():
429 if cmp(info.filename, file) == 0:
432 logfile.write(
"This file hasn't been zipped, " + file +
" It shouldn't be deleted now!\n")
435 Remove and Register Files
436 Reference to Delete(), filereg()
439 for file
in oldFiles.split():
440 newpath = newFile +
"#" + file[len(dir):]
441 logfile.write(
"Registering New File Path " + newpath +
"\n")
442 filereg(db,bakdb,tmpdb,newpath,logfile)
446 Remove and Unregister A File
447 Reference to fileunreg()
451 logfile.write(file +
"removed from db...\n")
453 logfile.write(file +
"removed from disk...\n")
462 Reference to DiskUsage(), GetListOfFiles(), TransferWithT0System(), RemoveAndRegister()
464 if __name__ ==
"__main__":
465 logfile.write(
"Starting Archival *Test* Script ...\n")
466 if EnableFileRemoval
is True:
DiskUsage(logfile)
468 if cmp(zipFileList, emptyString) == 0 : logfile.write(
"Sum of Files is below Threshold = " + str(fileSizeThreshold) +
"\n")
470 firstFile =
"DQM_Online_" + zipFileList.split()[0][len(dir)+len(
"DQM_V0010_"):-len(
"R000064807.root")]
471 lastFile = zipFileList.split()[-1][len(dir)+len(
"DQM_V0010_R000064807_"):-len(
".root")]
472 outputFileName = arcdir + firstFile + lastFile +
".zip"
473 logfile.write(
"1st File = " + firstFile +
" Last File = " + lastFile +
"\n")
474 if os.path.exists(outputFileName)
is True: os.remove(outputFileName)
475 if lastFile.find(
"R") != -1 and firstFile.find("R") != -1:
476 zip = zipfile.ZipFile(outputFileName, "w")
477 for name
in zipFileList.split():
478 zip.write(name,name, zipfile.ZIP_STORED)
480 filepath = outputFileName
481 zipFileSize = os.path.getsize(filepath)
482 logfile.write(
"Zip File Size = " + str(zipFileSize) +
"\n")
483 if zipFileSize > fileSizeThreshold :
484 zip = zipfile.ZipFile(outputFileName,
"r")# open file to see it readable
485 for info
in zip.infolist():
486 logfile.write(
"File = " + info.filename +
"\n")
488 if PathReplace
is False:
filereg(db,bakdb,tmpdb,filepath,logfile)
491 if transfer
is True and PathReplace
is True:
RemoveAndRegister(filepath,zipFileList, logfile)
493 logfile.write(
"Inconsistency! Created Zip File too small!\n")
496 logfile.write(
"Wrong File Name Stripping! Check directory path to the file!\n")