2 from __future__
import print_function
4 from CommonMethods
import *
13 regExp = re.search(
'(\D+)_(\d+)_(\d+)_(\d+)',fileName)
16 return long(regExp.group(3))
22 error =
"Usage: splitter fromDir"
24 sourceDir = sys.argv[1] +
'/'
26 fileList =
ls(sourceDir,
".txt")
31 for fileName
in fileList:
33 if runNumber
not in fileObjList:
34 fileObjList[runNumber] =
FileObj()
35 fileObjList[runNumber].run = runNumber
36 fileObjList[runNumber].fileNames.append(fileName)
37 aCommand =
'ls -l '+ sourceDir + fileName
38 output = commands.getstatusoutput( aCommand )
39 fileObjList[runNumber].size +=
int(output[1].
split(
' ')[4])
40 totalSize +=
int(output[1].
split(
' ')[4])
42 sortedKeys = sorted(fileObjList.keys())
48 for run
in sortedKeys:
49 dirSize += fileObjList[run].size
50 tmpList.append(fileObjList[run])
51 if dirSize > totalSize/split
or run == sortedKeys[len(sortedKeys)-1]:
52 newDir = sourceDir +
"Run" +
str(tmpList[0].run) +
"_" +
str(tmpList[len(tmpList)-1].run) +
"/"
53 aCommand =
'mkdir '+ newDir
54 output = commands.getstatusoutput( aCommand )
55 print(
str(100.*dirSize/totalSize) +
"% " +
"Run" +
str(tmpList[0].run) +
"_" +
str(tmpList[len(tmpList)-1].run))
58 cp(sourceDir,newDir,runs.fileNames)
74 if not os.path.isdir(destDir):
75 error =
"WARNING: destination directory doesn't exist! Creating it..."
78 copiedFiles =
cp(sourceDir,destDir,fileList)
80 if len(copiedFiles) != len(fileList):
81 error =
"ERROR: I couldn't copy all files from castor"
84 for fileName
in fileList:
85 fullFileName = destDir + fileName
87 with open(fullFileName,
'r')
as file:
89 if line.find(
"Runnumber") != -1:
90 tmpRun =
int(line.split(
' ')[1])
91 if runNumber != -1
and tmpRun != runNumber:
92 error =
"This file (" + fileName +
") contains more than 1 run number! I don't know how to deal with it!"
94 runNumber =
int(line.split(
' ')[1])
96 newFileName = fileName.replace(
"None",
str(runNumber))
97 if fileName != newFileName:
98 aCmd =
"mv " + destDir + fileName +
" " + destDir + newFileName
100 output = commands.getstatusoutput(aCmd)
104 print(
"WARNING couldn't find keyword None in file " + fileName)
110 if __name__ ==
"__main__":