1 |
yanming |
1.1 |
#!/usr/bin/env python
|
2 |
|
|
|
3 |
|
|
import sys,getopt,re,fnmatch,sets
|
4 |
|
|
import os
|
5 |
|
|
name = None
|
6 |
|
|
|
7 |
|
|
try:
|
8 |
|
|
opts, args = getopt.getopt(sys.argv[1:], "", ["file="])
|
9 |
|
|
except getopt.GetoptError:
|
10 |
|
|
print 'Please give file name (e.g. --file filename)'
|
11 |
|
|
sys.exit(2)
|
12 |
|
|
|
13 |
|
|
# check command line parameter
|
14 |
|
|
for opt, arg in opts :
|
15 |
|
|
if opt == "--file" :
|
16 |
|
|
name = arg
|
17 |
|
|
|
18 |
|
|
if name == None:
|
19 |
|
|
print 'Please give file name (e.g. --file filename)'
|
20 |
|
|
sys.exit(2)
|
21 |
|
|
|
22 |
|
|
myFile = open(name)
|
23 |
|
|
output = open("_temp.txt","w")
|
24 |
|
|
|
25 |
|
|
filter = '*root','*root.*','*dat','*dat.*','*tar.gz*','*tar','*py','LoadTest07_FNAL_FA_test'
|
26 |
|
|
|
27 |
|
|
while 1:
|
28 |
|
|
lines = myFile.readline()
|
29 |
|
|
if not lines:
|
30 |
|
|
break
|
31 |
|
|
if fnmatch.fnmatch(lines, "*#*"):
|
32 |
|
|
output.write("")
|
33 |
|
|
else:
|
34 |
|
|
tempwords = lines.split("/")
|
35 |
|
|
i=0
|
36 |
|
|
while (i<len(tempwords)):
|
37 |
|
|
if (i<len(tempwords)-1):
|
38 |
|
|
output.write(tempwords[i]+"/")
|
39 |
|
|
else:
|
40 |
|
|
for char in filter :
|
41 |
|
|
if fnmatch.fnmatch(tempwords[i], char):
|
42 |
|
|
output.write("\n")
|
43 |
|
|
continue
|
44 |
|
|
i=i+1
|
45 |
|
|
output.write("\n")
|
46 |
|
|
myFile.close()
|
47 |
|
|
output.close()
|
48 |
|
|
|
49 |
|
|
myFile = open("_temp.txt")
|
50 |
|
|
output = open("_temp2.txt","w")
|
51 |
|
|
while 1:
|
52 |
|
|
lines = myFile.readline()
|
53 |
|
|
if not lines:
|
54 |
|
|
break
|
55 |
|
|
|
56 |
|
|
tempwords = lines.split("/")
|
57 |
|
|
i=len(tempwords)-1
|
58 |
|
|
j=1
|
59 |
|
|
tmphold=[]
|
60 |
|
|
while (i>=0):
|
61 |
|
|
if (i==len(tempwords)-1):
|
62 |
|
|
tmphold.append("\n")
|
63 |
|
|
elif (j<=4 and tempwords[i].isdigit()):
|
64 |
|
|
tmphold.append("")
|
65 |
|
|
else:
|
66 |
|
|
tmphold.append(tempwords[i]+"/")
|
67 |
|
|
i=i-1
|
68 |
|
|
j=j+1
|
69 |
|
|
for i in reversed(tmphold):
|
70 |
|
|
output.write(i)
|
71 |
|
|
myFile.close()
|
72 |
|
|
output.close()
|
73 |
|
|
|
74 |
|
|
os.remove("_temp.txt")
|
75 |
|
|
|
76 |
|
|
newFile=open("_temp2.txt")
|
77 |
|
|
patterns=[]
|
78 |
|
|
new_patterns=[]
|
79 |
|
|
|
80 |
|
|
while 1:
|
81 |
|
|
newlines = newFile.readline()
|
82 |
|
|
if not newlines:
|
83 |
|
|
break
|
84 |
|
|
patterns.append(newlines)
|
85 |
|
|
|
86 |
|
|
patterns.sort()
|
87 |
|
|
new_patterns=sets.Set(patterns)
|
88 |
|
|
mylist=[]
|
89 |
|
|
for item in new_patterns:
|
90 |
|
|
mylist.append(item)
|
91 |
|
|
mylist.sort()
|
92 |
|
|
|
93 |
|
|
patterns_output = open("sorted_output.txt","w")
|
94 |
|
|
for new_names in mylist:
|
95 |
|
|
patterns_output.write(new_names)
|
96 |
|
|
patterns_output.close()
|
97 |
|
|
newFile.close()
|
98 |
|
|
|
99 |
|
|
os.remove("_temp2.txt")
|
100 |
|
|
|
101 |
|
|
myFile = open("sorted_output.txt")
|
102 |
|
|
output = open("sorted_dir_tmp.txt","w")
|
103 |
|
|
while 1:
|
104 |
|
|
lines = myFile.readline()
|
105 |
|
|
if not lines:
|
106 |
|
|
break
|
107 |
|
|
tempwords = lines.split("/")
|
108 |
|
|
i=0
|
109 |
|
|
while (i<len(tempwords)):
|
110 |
|
|
if(i==0):
|
111 |
|
|
output.write("/")
|
112 |
|
|
elif (i<=2):
|
113 |
|
|
output.write(tempwords[i]+"/")
|
114 |
|
|
else:
|
115 |
|
|
output.write("")
|
116 |
|
|
i=i+1
|
117 |
|
|
output.write("\n")
|
118 |
|
|
myFile.close()
|
119 |
|
|
output.close()
|
120 |
|
|
|
121 |
|
|
newFile=open("sorted_dir_tmp.txt")
|
122 |
|
|
patterns=[]
|
123 |
|
|
new_patterns=[]
|
124 |
|
|
|
125 |
|
|
while 1:
|
126 |
|
|
newlines = newFile.readline()
|
127 |
|
|
if not newlines:
|
128 |
|
|
break
|
129 |
|
|
patterns.append(newlines)
|
130 |
|
|
|
131 |
|
|
patterns.sort()
|
132 |
|
|
new_patterns=sets.Set(patterns)
|
133 |
|
|
mylist=[]
|
134 |
|
|
for item in new_patterns:
|
135 |
|
|
mylist.append(item)
|
136 |
|
|
mylist.sort()
|
137 |
|
|
|
138 |
|
|
patterns_output = open("sorted_dir.txt","w")
|
139 |
|
|
for new_names in mylist:
|
140 |
|
|
patterns_output.write(new_names)
|
141 |
|
|
patterns_output.close()
|
142 |
|
|
newFile.close()
|
143 |
|
|
|
144 |
|
|
os.remove("sorted_dir_tmp.txt")
|
145 |
|
|
|
146 |
|
|
|
147 |
|
|
|
148 |
|
|
def reversed(x):
|
149 |
|
|
if hasattr(x, 'keys'):
|
150 |
|
|
raise ValueError("mappings do not support reverse iteration")
|
151 |
|
|
i = len(x)
|
152 |
|
|
while i > 0:
|
153 |
|
|
i -= 1
|
154 |
|
|
yield x[i]
|
155 |
|
|
|
156 |
|
|
|
157 |
|
|
|