1 |
joshmt |
1.1 |
import os, sys, re
|
2 |
|
|
|
3 |
|
|
#handle input arguments
|
4 |
|
|
#this includes the name of the script
|
5 |
|
|
##for arg in sys.argv:
|
6 |
|
|
## print arg
|
7 |
|
|
|
8 |
|
|
#sys.argv[1] will have the first argument
|
9 |
|
|
tmpfile = '/tmp/joshmt/onlyLast_py_'
|
10 |
|
|
mypid = os.getpid()
|
11 |
|
|
tmpfile += `mypid`
|
12 |
|
|
|
13 |
|
|
inputdir = sys.argv[1]
|
14 |
|
|
if inputdir[len(inputdir)-1] != '/':
|
15 |
|
|
inputdir+='/'
|
16 |
|
|
|
17 |
|
|
outputdir = inputdir
|
18 |
|
|
outputdir += 'EXTRAS'
|
19 |
|
|
|
20 |
|
|
mkdircommand = "nsmkdir "
|
21 |
|
|
mkdircommand += outputdir
|
22 |
|
|
|
23 |
|
|
#get a file listing into tmpfile
|
24 |
|
|
cmd = 'nsls -l '
|
25 |
|
|
cmd += inputdir
|
26 |
|
|
cmd += ' | awk \'// {print $5,$9;}\' > '
|
27 |
|
|
cmd += tmpfile
|
28 |
|
|
#print cmd
|
29 |
|
|
os.system(cmd)
|
30 |
|
|
|
31 |
|
|
print mkdircommand
|
32 |
|
|
#this will give a harmless error if the dir already exists
|
33 |
|
|
os.system(mkdircommand)
|
34 |
|
|
|
35 |
|
|
f = open(tmpfile,'r')
|
36 |
|
|
|
37 |
|
|
indexdict = {}
|
38 |
|
|
|
39 |
|
|
stub = 'stub'
|
40 |
|
|
|
41 |
|
|
for line in f:
|
42 |
|
|
mypair = line.split()
|
43 |
|
|
#file size
|
44 |
|
|
size = int(mypair[0])
|
45 |
|
|
#parse filename
|
46 |
|
|
result = re.match('(.*)_([0-9]*)_([0-9]*).root',mypair[1])
|
47 |
|
|
if size > 0:
|
48 |
|
|
if stub=='stub':
|
49 |
|
|
stub = result.group(1)
|
50 |
|
|
elif stub!=result.group(1):
|
51 |
|
|
print "oh no!"
|
52 |
|
|
|
53 |
|
|
if result.group(2) in indexdict:
|
54 |
|
|
#so the key already exists
|
55 |
|
|
indexdict[result.group(2)].append(result.group(3))
|
56 |
|
|
else:
|
57 |
|
|
indexdict[result.group(2)] = [result.group(3)]
|
58 |
|
|
# print result.group(0)
|
59 |
|
|
# print result.group(1)
|
60 |
|
|
# print result.group(2)
|
61 |
|
|
# print result.group(3)
|
62 |
|
|
|
63 |
|
|
#print indexdict
|
64 |
|
|
|
65 |
|
|
for ii in indexdict:
|
66 |
|
|
if len(indexdict[ii])==1:
|
67 |
|
|
print "nothing to do for ",ii
|
68 |
|
|
else:
|
69 |
|
|
indexdict[ii].sort()
|
70 |
|
|
goodindex = indexdict[ii].pop()
|
71 |
|
|
print ii, ": keeping index ",goodindex
|
72 |
|
|
for jj in indexdict[ii]:
|
73 |
|
|
s=inputdir
|
74 |
|
|
s+=stub
|
75 |
|
|
s+='_'
|
76 |
|
|
s+=ii
|
77 |
|
|
s+='_'
|
78 |
|
|
s+=jj
|
79 |
|
|
s+='.root'
|
80 |
|
|
# print "moving ", s
|
81 |
|
|
cpcmd = 'rfcp '
|
82 |
|
|
cpcmd += s
|
83 |
|
|
cpcmd += ' '
|
84 |
|
|
cpcmd += outputdir
|
85 |
|
|
print cpcmd
|
86 |
|
|
os.system(cpcmd)
|
87 |
|
|
rmcmd = 'rfrm '
|
88 |
|
|
rmcmd += s
|
89 |
|
|
print rmcmd
|
90 |
|
|
os.system(rmcmd)
|
91 |
|
|
|
92 |
|
|
|
93 |
|
|
f.close()
|
94 |
|
|
os.remove(tmpfile)
|
95 |
|
|
|
96 |
|
|
|