ATLAS Offline Software
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 
5 import sys,os,shutil
6 from PROCTools.getFileLists import findTCTFiles
7 
8 import subprocess
9 
10 
11 def 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 
40 def freeSpace(p): #wont' work on afs ..
41  s = os.statvfs(p)
42  return s.f_bsize * s.f_bavail
43 
44 
45 
46 
47 if __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 
python.copyTCTOutput.usage
def usage()
Definition: copyTCTOutput.py:48
python.copyTCTOutput.freeSpace
def freeSpace(p)
Definition: copyTCTOutput.py:40
python.copyTCTOutput.getFileSize
def getFileSize(pfn)
Definition: copyTCTOutput.py:11
TCS::join
std::string join(const std::vector< std::string > &v, const char c=',')
Definition: Trigger/TrigT1/L1Topo/L1TopoCommon/Root/StringUtils.cxx:10
python.CaloAddPedShiftConfig.int
int
Definition: CaloAddPedShiftConfig.py:45
Trk::split
@ split
Definition: LayerMaterialProperties.h:38