ATLAS Offline Software
Loading...
Searching...
No Matches
copyTCTOutput.py
Go to the documentation of this file.
1#!/bin/env python
2
3# Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration
4
5import sys,os,shutil
6from PROCTools.getFileLists import findTCTFiles
7
8import subprocess
9
10
11def getFileSize(pfn):
12 "Get size of a file through os.stat (regular file) or rfstat (file on castor)"
13 size=None
14 pos=pfn.rfind(':')
15 if pos!=-1:
16 pfn=pfn[1+pos:]
17 #print ("New pfn:",pfn)
18 if pfn.startswith("/castor/"):
19 cmd= "rfstat "+pfn
20 (stat,out)=subprocess.getstatusoutput(cmd)
21 if stat!=0:
22 print ("ERROR: ",cmd,"failed.")
23 return None
24 for l in out.split(os.linesep):
25 if l.startswith("Size"):
26 colon=l.index(':')
27 size=int(l[1+colon:].strip())
28 return size
29 print ("ERROR: Failed to interpret output of rfstat")
30 return None
31 else: #Assume regular file
32 try:
33 statinfo=os.stat(pfn)
34 size=statinfo[6]
35 except Exception:
36 print ("Can't acess regular file: ",pfn)
37 return size
38
39
40def freeSpace(p): #wont' work on afs ..
41 s = os.statvfs(p)
42 return s.f_bsize * s.f_bavail
43
44
45
46
47if __name__=="__main__":
48 def usage():
49 print ("Copy a full TCT to a (local) disk")
50 print (sys.argv[0],"tctpath destpath")
51
52
53 if len(sys.argv)!=3:
54 usage()
55 sys.exit(-1)
56
57 sDir=os.path.normpath(sys.argv[1])
58 dDir=os.path.normpath(sys.argv[2])
59
60 patterns=(".*myESD.*pool.root$",".*myAOD.*pool.root$","myTAGCOMM.*root$","TAG.root",)
61 os.environ['STAGE_SVCCLASS']="atlascerngroupdisk"
62 os.environ['STAGE_HOST']="castoratlast3"
63
64 if not os.access(sDir,os.R_OK):
65 print ("Can't read from",sDir)
66 sys.exit(-1)
67
68 if not os.access(dDir,os.W_OK):
69 try:
70 os.mkdir(dDir)
71 except OSError as why:
72 print ("Can't write to",dDir)
73 print (why)
74 sys.exit(-1)
75
76
77 ff=findTCTFiles(sDir,"")
78 chains=ff.getChains()
79
80 allFilesToCopy=dict()
81
82 print ("Searching for files to copy...")
83 for name,tci in ff._commonDirs.items():
84 filesToCopy=[tci[0].logfile,]
85 for p in patterns:
86 filesToCopy+=ff.findFilesInDir(tci[0].directory,p)
87
88 allFilesToCopy[name]=filesToCopy
89
90 totalSize=0
91 nFiles=0
92 for n,fs in allFilesToCopy.items():
93 #print (n)
94 nFiles+=len(fs)
95 for f in fs:
96 #print (" ",f)
97 totalSize+=getFileSize(f)
98
99 print ("Found %i files with a total Size of %.2f MB" % (nFiles, totalSize/(1024.0*1024.0)))
100 print ("Start Copying:")
101
102
103 for n,fs in allFilesToCopy.items():
104 if len(fs)>1:
105 print ("Working on ",n)
106 logpath=fs[0]
107 if not logpath.startswith(sDir):
108 print ("ERROR, log file path",logpath,"does not start with",sDir)
109 sys.exit(-1)
110 locDir="/".join(logpath[len(sDir):].split("/")[:-1])
111 #print (locDir)
112 destdir=dDir+locDir
113 try:
114 os.makedirs(destdir)
115 except OSError as why:
116 print ("Failed to create directory",destdir)
117 print (why)
118 sys.exit(-1)
119
120 try:
121 shutil.copy2(logpath,destdir+"/"+os.path.basename(logpath))
122 except Exception as why:
123 print ("Can't copy log-file to",os.path.basename(logpath))
124 print (why)
125 sys.exit(-1)
126
127 for f in fs[1:]:
128 destfile=destdir+"/"+os.path.basename(f)
129 if f. startswith("/castor/"):
130 #print ("Castor copying",f,"to",destfile)
131 cmd="rfcp "+f+" "+destfile
132 #print (cmd)
133 (stat,out)=subprocess.getstatusoutput(cmd)
134 if stat!=0:
135 print (out)
136 print ("ERROR: ",cmd,"failed.")
137 sys.exit(-1)
138 else: # Regular file
139 #print ("Copying",f,"to",destfile)
140 try:
141 shutil.copy2(f,destfile)
142 except Exception as why:
143 print ("Can't copy file to",os.path.basename(logpath))
144 print (why)
145 sys.exit(-1)
146
147 print ("Finished.")
148
149
std::vector< std::string > split(const std::string &s, const std::string &t=":")
Definition hcg.cxx:177