23 error =
"Usage: splitter fromDir"
25 sourceDir = sys.argv[1] +
'/'
27 fileList =
ls(sourceDir,
".txt")
32 for fileName
in fileList:
34 if runNumber
not in fileObjList:
35 fileObjList[runNumber] =
FileObj()
36 fileObjList[runNumber].run = runNumber
37 fileObjList[runNumber].fileNames.append(fileName)
38 aCommand =
'ls -l '+ sourceDir + fileName
39 output = subprocess.getstatusoutput( aCommand )
40 fileObjList[runNumber].size += int(output[1].
split(
' ')[4])
41 totalSize += int(output[1].
split(
' ')[4])
43 sortedKeys = sorted(fileObjList.keys())
49 for run
in sortedKeys:
50 dirSize += fileObjList[run].size
51 tmpList.append(fileObjList[run])
52 if dirSize > totalSize/split
or run == sortedKeys[len(sortedKeys)-1]:
53 newDir = sourceDir +
"Run" +
str(tmpList[0].run) +
"_" +
str(tmpList[len(tmpList)-1].run) +
"/"
54 aCommand =
'mkdir '+ newDir
55 output = subprocess.getstatusoutput( aCommand )
56 print(
str(100.*dirSize/totalSize) +
"% " +
"Run" +
str(tmpList[0].run) +
"_" +
str(tmpList[len(tmpList)-1].run))
59 cp(sourceDir,newDir,runs.fileNames)
75 if not os.path.isdir(destDir):
76 error =
"WARNING: destination directory doesn't exist! Creating it..."
79 copiedFiles =
cp(sourceDir,destDir,fileList)
81 if len(copiedFiles) != len(fileList):
82 error =
"ERROR: I couldn't copy all files from castor"
85 for fileName
in fileList:
86 fullFileName = destDir + fileName
88 with open(fullFileName,
'r') as file:
90 if line.find(
"Runnumber") != -1:
91 tmpRun = int(line.split(
' ')[1])
92 if runNumber != -1
and tmpRun != runNumber:
93 error =
"This file (" + fileName +
") contains more than 1 run number! I don't know how to deal with it!"
95 runNumber = int(line.split(
' ')[1])
97 newFileName = fileName.replace(
"None",
str(runNumber))
98 if fileName != newFileName:
99 aCmd =
"mv " + destDir + fileName +
" " + destDir + newFileName
101 output = subprocess.getstatusoutput(aCmd)
105 print(
"WARNING couldn't find keyword None in file " + fileName)