Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:29:04

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