Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-11-27 03:18:06

0001 #!/usr/bin/env python3
0002 import sys,os,subprocess
0003 from CommonMethods import *
0004 def main():
0005     if len(sys.argv) < 3:
0006         error = "Usage: copyAndRename fromDir toDir"
0007         exit(error)
0008     sourceDir = sys.argv[1] + '/'
0009     destDir   = sys.argv[2] + '/'
0010 
0011     fileList = ls(sourceDir,".txt")
0012     if not os.path.isdir(destDir):
0013         error = "WARNING: destination directory doesn't exist! Creating it..."
0014         print(error)
0015         os.mkdir(destDir)
0016     copiedFiles = cp(sourceDir,destDir,fileList)
0017 
0018     if len(copiedFiles) != len(fileList):
0019         error = "ERROR: I couldn't copy all files from castor"
0020         exit(error)
0021 
0022     for fileName in fileList:
0023         fullFileName = destDir + fileName
0024         runNumber = -1;
0025         with open(fullFileName,'r') as file:
0026             for line in file:
0027                 if line.find("Runnumber") != -1:
0028                     tmpRun = int(line.split(' ')[1])
0029                     if runNumber != -1 and tmpRun != runNumber:
0030                         error = "This file (" + fileName + ") contains more than 1 run number! I don't know how to deal with it!"
0031                         exit(error)
0032                     runNumber = int(line.split(' ')[1])
0033         file.close()
0034         newFileName = fileName.replace("1_.txt",str(runNumber)+"_1_.txt")
0035         if fileName != newFileName:
0036             aCmd = "mv " + destDir + fileName + " " + destDir + newFileName
0037             print(aCmd)
0038             output =  subprocess.getstatusoutput(aCmd)
0039             if output[0] != 0:
0040                 print(output[1])
0041         else:
0042             print("WARNING couldn't find keyword None in file " + fileName)
0043 
0044 
0045 
0046 
0047         
0048 if __name__ == "__main__":
0049     main()