CMS 3D CMS Logo

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