CMS 3D CMS Logo

copyAndRename.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 import sys,os,commands
3 from CommonMethods import *
4 def main():
5  if len(sys.argv) < 3:
6  error = "Usage: copyAndRename fromDir toDir"
7  exit(error)
8  sourceDir = sys.argv[1] + '/'
9  destDir = sys.argv[2] + '/'
10 
11  fileList = ls(sourceDir,".txt")
12  if not os.path.isdir(destDir):
13  error = "WARNING: destination directory doesn't exist! Creating it..."
14  print error
15  os.mkdir(destDir)
16  copiedFiles = cp(sourceDir,destDir,fileList)
17 
18  if len(copiedFiles) != len(fileList):
19  error = "ERROR: I couldn't copy all files from castor"
20  exit(error)
21 
22  for fileName in fileList:
23  fullFileName = destDir + fileName
24  runNumber = -1;
25  with open(fullFileName,'r') as file:
26  for line in file:
27  if line.find("Runnumber") != -1:
28  tmpRun = int(line.split(' ')[1])
29  if runNumber != -1 and tmpRun != runNumber:
30  error = "This file (" + fileName + ") contains more than 1 run number! I don't know how to deal with it!"
31  exit(error)
32  runNumber = int(line.split(' ')[1])
33  file.close()
34  newFileName = fileName.replace("1_.txt",str(runNumber)+"_1_.txt")
35  if fileName != newFileName:
36  aCmd = "mv " + destDir + fileName + " " + destDir + newFileName
37  print aCmd
38  output = commands.getstatusoutput(aCmd)
39  if output[0] != 0:
40  print output[1]
41  else:
42  print "WARNING couldn't find keyword None in file " + fileName
43 
44 
45 
46 
47 
48 if __name__ == "__main__":
49  main()
def ls(path, rec=False)
Definition: eostools.py:348
Definition: main.py:1