ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/joshmt/onlyLast.py
Revision: 1.2
Committed: Wed Aug 25 10:15:46 2010 UTC (14 years, 8 months ago) by joshmt
Content type: text/x-python
Branch: MAIN
Changes since 1.1: +41 -14 lines
Log Message:
make compatible with current CRAB

File Contents

# Content
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:
17 ## print arg
18
19 #sys.argv[1] will have the first argument
20 tmpfile = '/tmp/joshmt/onlyLast_py_'
21 mypid = os.getpid()
22 tmpfile += `mypid`
23
24 inputdir = sys.argv[1]
25 if inputdir[len(inputdir)-1] != '/':
26 inputdir+='/'
27
28 outputdir = inputdir
29 outputdir += 'EXTRAS'
30
31 mkdircommand = "nsmkdir "
32 mkdircommand += outputdir
33
34 #get a file listing into tmpfile
35 cmd = 'nsls -l '
36 cmd += inputdir
37 cmd += ' | awk \'// {print $5,$9;}\' > '
38 cmd += tmpfile
39 #print cmd
40 os.system(cmd)
41
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])
66 if size > 0:
67 if stub=='stub':
68 stub = result.group(1)
69 elif stub!=result.group(1):
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)
82 # print result.group(3)
83
84 #print indexdict
85
86 for ii in indexdict:
87 if len(indexdict[ii])==1:
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 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
104 s+=jj
105 print "moving ", s
106 cpcmd = 'rfcp '
107 cpcmd += s
108 cpcmd += ' '
109 cpcmd += outputdir
110 print cpcmd
111 os.system(cpcmd)
112 rmcmd = 'rfrm '
113 rmcmd += s
114 print rmcmd
115 os.system(rmcmd)
116 nmoved = nmoved+1
117
118 f.close()
119 os.remove(tmpfile)
120
121 print "----------------------------"
122 print "I moved this many files: ",nmoved
123 print "----------------------------"