test
CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
rename.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 import sys,os,commands,re
3 from CommonMethods import *
4 def main():
5 # sourcePath = "/castor/cern.ch/cms/store/caf/user/uplegger/Workflows/Repro_2011Feb24/"
6  sourcePath = "LatestRuns/"
7 # sourcePath = "Files/"
8  sourceDirList = [sourcePath+"2010A",sourcePath+"2010B",sourcePath+"Commissioning10"]
9  destDirList = ["2010A","2010B","Commissioning10"]
10  path = "LatestRuns/"
11  finalDir = path + "Results/"
12 
13  if not os.path.isdir(path):
14  error = "WARNING: path directory doesn't exist! Creating it..."
15  print error
16  os.mkdir(path)
17 
18  if not os.path.isdir(finalDir):
19  error = "WARNING: final dir directory doesn't exist! Creating it..."
20  print error
21  os.mkdir(finalDir)
22 
23  #for n in range(0,3):
24  for n in range(0,len(sourceDirList)):
25  sourceDir = sourceDirList[n] + '/'
26  destDir = path + destDirList[n] + '/'
27  if not dirExists(sourceDir):
28  print sourceDir + " doesn't exist!"
29  continue
30  fileList = ls(sourceDir)
31  if not os.path.isdir(destDir):
32  error = "WARNING: destination directory doesn't exist! Creating it..."
33  print error
34  os.mkdir(destDir)
35  copiedFiles = cp(sourceDir,destDir,fileList,False,False)
36 
37  #if len(copiedFiles) != len(fileList):
38  # error = "ERROR: I couldn't copy all files"
39  # exit(error)
40 # exit("ok")
41  for fileName in copiedFiles:
42  #regExp = re.search('(\D+)(\d+)_(\d+)_.txt',fileName)
43  regExp = re.search('(\D+)(\d+)_(\d+)_[a-zA-Z0-9]+.txt',fileName)
44  #if regExp:
45  #print regExp.group(1) + regExp.group(2) + "_" + str(1) + "_" + regExp.group(3) + ".txt"
46 
47  fullFileName = destDir + fileName
48  print fullFileName
49  runNumber = -1
50  with open(fullFileName,'r') as file:
51  allTxt = ''
52  for line in file:
53  if line.find("Runnumber") != -1:
54  tmpRun = int(line.split(' ')[1])
55  if runNumber != -1 and tmpRun != runNumber:
56  error = "This file (" + fileName + ") contains more than 1 run number!"
57  if regExp:
58  newFileName = regExp.group(1) + regExp.group(2) + "_" + str(runNumber) + "_1.txt"
59  with open(finalDir+newFileName,'a') as outFile:
60  outFile.writelines(allTxt)
61  outFile.close()
62  allTxt = ''
63  #aCmd = "cp " + destDir + fileName + " " + finalDir + newFileName
64  #print aCmd
65  #output = commands.getstatusoutput(aCmd)
66  #if output[0] != 0:
67  #print output[1]
68  else:
69  print "WARNING: I can't match the regular espression for file: " + fileName
70  runNumber = int(line.split(' ')[1])
71  allTxt += line
72  file.close()
73  if regExp:
74  newFileName = regExp.group(1) + regExp.group(2) + "_" + str(runNumber) + "_" + regExp.group(3) + ".txt"
75  with open(finalDir+newFileName,'a') as outFile:
76  outFile.writelines(allTxt)
77  outFile.close()
78 
79 
80 
81 
82 
83 if __name__ == "__main__":
84  main()
Definition: main.py:1
def main
Definition: rename.py:4