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