CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_6_1_1/src/RecoVertex/BeamSpotProducer/scripts/copyAndRename.py

Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 import sys,os,commands
00003 from CommonMethods import *
00004 def main():
00005     if len(sys.argv) < 3:
00006         error = "Usage: copyAndRename fromDir toDir"
00007         exit(error)
00008     sourceDir = sys.argv[1] + '/'
00009     destDir   = sys.argv[2] + '/'
00010 
00011     fileList = ls(sourceDir,".txt")
00012     if not os.path.isdir(destDir):
00013         error = "WARNING: destination directory doesn't exist! Creating it..."
00014         print error
00015         os.mkdir(destDir)
00016     copiedFiles = cp(sourceDir,destDir,fileList)
00017 
00018     if len(copiedFiles) != len(fileList):
00019         error = "ERROR: I couldn't copy all files from castor"
00020         exit(error)
00021 
00022     for fileName in fileList:
00023         fullFileName = destDir + fileName
00024         runNumber = -1;
00025         with open(fullFileName,'r') as file:
00026             for line in file:
00027                 if line.find("Runnumber") != -1:
00028                     tmpRun = int(line.split(' ')[1])
00029                     if runNumber != -1 and tmpRun != runNumber:
00030                         error = "This file (" + fileName + ") contains more than 1 run number! I don't know how to deal with it!"
00031                         exit(error)
00032                     runNumber = int(line.split(' ')[1])
00033         file.close()
00034         newFileName = fileName.replace("1_.txt",str(runNumber)+"_1_.txt")
00035         if fileName != newFileName:
00036             aCmd = "mv " + destDir + fileName + " " + destDir + newFileName
00037             print aCmd
00038             output =  commands.getstatusoutput(aCmd)
00039             if output[0] != 0:
00040                 print output[1]
00041         else:
00042             print "WARNING couldn't find keyword None in file " + fileName
00043 
00044 
00045 
00046 
00047         
00048 if __name__ == "__main__":
00049     main()