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