1 |
|
import os, sys, re |
2 |
|
|
3 |
+ |
############################################################### |
4 |
+ |
|
5 |
+ |
#Usage: |
6 |
+ |
# python onlyLast.py <path to input directory on CASTOR> |
7 |
+ |
|
8 |
+ |
#now updated to handle output from the latest CRAB with |
9 |
+ |
#file names like this: |
10 |
+ |
# myfilename_1_1_aBC.root |
11 |
+ |
|
12 |
+ |
############################################################### |
13 |
+ |
|
14 |
|
#handle input arguments |
15 |
|
#this includes the name of the script |
16 |
|
##for arg in sys.argv: |
39 |
|
#print cmd |
40 |
|
os.system(cmd) |
41 |
|
|
42 |
< |
print mkdircommand |
43 |
< |
#this will give a harmless error if the dir already exists |
33 |
< |
os.system(mkdircommand) |
42 |
> |
#keep track of whether we made the directory or not |
43 |
> |
alreadymadedir = 0 |
44 |
|
|
45 |
|
f = open(tmpfile,'r') |
46 |
|
|
47 |
+ |
#to deal with the arbitrary extension to the filename that crab |
48 |
+ |
#now adds, I will add a second associative array to keep track of the |
49 |
+ |
#whole file name. (completefilenames) |
50 |
+ |
#The only subtle part is where the file names are sorted. |
51 |
+ |
#Hopefully this does not create any bugs |
52 |
|
indexdict = {} |
53 |
+ |
completefilenames = {} |
54 |
|
|
55 |
|
stub = 'stub' |
56 |
|
|
57 |
+ |
#do some accounting |
58 |
+ |
nmoved = 0 |
59 |
+ |
|
60 |
|
for line in f: |
61 |
|
mypair = line.split() |
62 |
|
#file size |
63 |
|
size = int(mypair[0]) |
64 |
|
#parse filename |
65 |
< |
result = re.match('(.*)_([0-9]*)_([0-9]*).root',mypair[1]) |
65 |
> |
result = re.match('(.*)_([0-9]*)_([0-9]*)_(.*).root',mypair[1]) |
66 |
|
if size > 0: |
67 |
|
if stub=='stub': |
68 |
|
stub = result.group(1) |
69 |
|
elif stub!=result.group(1): |
70 |
< |
print "oh no!" |
70 |
> |
print "WARNING -- there is more than one group of filenames here!" |
71 |
|
|
72 |
|
if result.group(2) in indexdict: |
73 |
|
#so the key already exists |
74 |
|
indexdict[result.group(2)].append(result.group(3)) |
75 |
+ |
completefilenames[result.group(2)].append(mypair[1]) |
76 |
|
else: |
77 |
|
indexdict[result.group(2)] = [result.group(3)] |
78 |
+ |
completefilenames[result.group(2)] = [mypair[1]] |
79 |
|
# print result.group(0) |
80 |
|
# print result.group(1) |
81 |
|
# print result.group(2) |
88 |
|
print "nothing to do for ",ii |
89 |
|
else: |
90 |
|
indexdict[ii].sort() |
91 |
+ |
#will this work? in my one test case, yes |
92 |
+ |
completefilenames[ii].sort() |
93 |
|
goodindex = indexdict[ii].pop() |
94 |
+ |
goodfilename = completefilenames[ii].pop() |
95 |
|
print ii, ": keeping index ",goodindex |
96 |
< |
for jj in indexdict[ii]: |
96 |
> |
print ii, ": corresponds to file ",goodfilename |
97 |
> |
for jj in completefilenames[ii]: |
98 |
> |
if alreadymadedir == 0: |
99 |
> |
print mkdircommand |
100 |
> |
#this will give a harmless error if the dir already exists |
101 |
> |
os.system(mkdircommand) |
102 |
> |
|
103 |
|
s=inputdir |
74 |
– |
s+=stub |
75 |
– |
s+='_' |
76 |
– |
s+=ii |
77 |
– |
s+='_' |
104 |
|
s+=jj |
105 |
< |
s+='.root' |
80 |
< |
# print "moving ", s |
105 |
> |
print "moving ", s |
106 |
|
cpcmd = 'rfcp ' |
107 |
|
cpcmd += s |
108 |
|
cpcmd += ' ' |
113 |
|
rmcmd += s |
114 |
|
print rmcmd |
115 |
|
os.system(rmcmd) |
116 |
< |
|
116 |
> |
nmoved = nmoved+1 |
117 |
|
|
118 |
|
f.close() |
119 |
|
os.remove(tmpfile) |
120 |
|
|
121 |
< |
|
121 |
> |
print "----------------------------" |
122 |
> |
print "I moved this many files: ",nmoved |
123 |
> |
print "----------------------------" |