CMS 3D CMS Logo

Functions

rename Namespace Reference

Functions

def main

Function Documentation

def rename::main ( )

Definition at line 4 of file rename.py.

00005           :
00006 #    sourcePath = "/castor/cern.ch/cms/store/caf/user/uplegger/Workflows/Repro_2011Feb24/"
00007     sourcePath = "LatestRuns/"
00008 #    sourcePath = "Files/"
00009     sourceDirList = [sourcePath+"2010A",sourcePath+"2010B",sourcePath+"Commissioning10"]
00010     destDirList   = ["2010A","2010B","Commissioning10"]
00011     path = "LatestRuns/"
00012     finalDir = path + "Results/"
00013 
00014     if not os.path.isdir(path):
00015         error = "WARNING: path directory doesn't exist! Creating it..."
00016         print error
00017         os.mkdir(path)
00018 
00019     if not os.path.isdir(finalDir):
00020         error = "WARNING: final dir directory doesn't exist! Creating it..."
00021         print error
00022         os.mkdir(finalDir)
00023 
00024     #for n in range(0,3):
00025     for n in range(0,len(sourceDirList)):
00026         sourceDir = sourceDirList[n] + '/'
00027         destDir   = path + destDirList[n] + '/'
00028         if not dirExists(sourceDir):
00029             print sourceDir + " doesn't exist!"
00030             continue
00031         fileList = ls(sourceDir)
00032         if not os.path.isdir(destDir):
00033             error = "WARNING: destination directory doesn't exist! Creating it..."
00034             print error
00035             os.mkdir(destDir)
00036         copiedFiles = cp(sourceDir,destDir,fileList,False,False)
00037 
00038         #if len(copiedFiles) != len(fileList):
00039         #    error = "ERROR: I couldn't copy all files"
00040         #    exit(error)
00041 #        exit("ok")
00042         for fileName in copiedFiles:
00043             #regExp = re.search('(\D+)(\d+)_(\d+)_.txt',fileName)
00044             regExp = re.search('(\D+)(\d+)_(\d+)_[a-zA-Z0-9]+.txt',fileName)
00045             #if regExp:
00046                 #print regExp.group(1) + regExp.group(2) + "_" + str(1) + "_" + regExp.group(3) + ".txt" 
00047                 
00048             fullFileName = destDir + fileName
00049             print fullFileName
00050             runNumber = -1
00051             with open(fullFileName,'r') as file:
00052                 allTxt = '' 
00053                 for line in file:
00054                     if line.find("Runnumber") != -1:
00055                         tmpRun = int(line.split(' ')[1])
00056                         if runNumber != -1 and tmpRun != runNumber:
00057                             error = "This file (" + fileName + ") contains more than 1 run number!"
00058                             if regExp:
00059                                 newFileName = regExp.group(1) + regExp.group(2) + "_" + str(runNumber) + "_1.txt" 
00060                                 with open(finalDir+newFileName,'w') as outFile:
00061                                     outFile.writelines(allTxt)
00062                                 outFile.close()
00063                                 allTxt = ''
00064                                 #aCmd = "cp " + destDir + fileName + " " + finalDir + newFileName
00065                                 #print aCmd
00066                                 #output =  commands.getstatusoutput(aCmd)
00067                                 #if output[0] != 0:
00068                                     #print output[1]
00069                             else:
00070                                 print "WARNING: I can't match the regular espression for file: " + fileName
00071                         runNumber = int(line.split(' ')[1])
00072                     allTxt += line
00073             file.close()
00074             if regExp:
00075                 newFileName = regExp.group(1) + regExp.group(2) + "_" + str(runNumber) + "_" + regExp.group(3) + ".txt" 
00076                 with open(finalDir+newFileName,'w') as outFile:
00077                     outFile.writelines(allTxt)
00078                 outFile.close()
00079 
00080 
00081 
00082