CMS 3D CMS Logo

Functions

copyAndRename Namespace Reference

Functions

def main

Function Documentation

def copyAndRename::main ( )

Definition at line 4 of file copyAndRename.py.

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