CMS 3D CMS Logo

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