forked from cl4u2/ninuxoo
-
Notifications
You must be signed in to change notification settings - Fork 1
/
resources.py
328 lines (294 loc) · 8.43 KB
/
resources.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
#!/usr/bin/env python2
import re
import commands
class Resource():
uri = ""
comments =""
server = ""
path = ""
protocol = ""
tags = set()
filetype = ""
filesize = 0
firstseen = ""
smbproxy = ""
def __init__(self, uri="", server="", comments="", protocol="", path="", filetype="", filesize=0, firstseen=""):
self.uri = uri.strip()
self.server = server
self.comments = comments
self.path = path
self.protocol = protocol
self.filetype = filetype
self.filesize = filesize
self.tags = set()
self.firstseen = firstseen
def addTags(self, newtags):
if newtags.__class__ == list:
for tag in newtags:
self.addTags(tag.strip())
else:
self.tags.add(newtags.upper())
def makeTags(self):
"populate the tags attribute from the uri and comments attributes"
# add the server's ip address
m = re.match("([a-z]{3,4})://([^/]*)/(.*)\.([a-zA-Z0-9]{2,4}$)", self.uri)
if not m:
m = re.match("([a-z]{3,4})://([^/]*)/(.*)", self.uri)
try: # protocol
self.protocol = m.group(1)
self.addTags(self.protocol)
except:
self.protocol = ""
try: # server
#assert self.server == m.group(2)
self.addTags(m.group(2))
except:
pass
try: # path
urisrest = m.group(3)
self.path = urisrest
except:
urisrest = self.uri
if not self.filetype:
try: # filetype
self.filetype = m.group(4).upper()
self.addTags(self.filetype)
self.path += "." + m.group(4)
except:
self.filetype = ""
# split the uris into tags
separators = "./\\_' ,-!\"#$%^&*()[];:{}=@|"
tmptags = [urisrest, self.comments]
for s in list(separators):
tmptagsnew = list()
for e in tmptags:
tmptagsnew += e.split(s)
tmptags = tmptagsnew
#detect CamelCase
tmptagsnew = list()
for e in tmptags:
if len(e) <= 2:
continue
currentupper = e[0].isupper()
justchanged = True
currentword = [e[0]]
for c in e[1:]:
if justchanged or (c.isupper() == currentupper):
currentword.append(c)
justchanged = False
else:
tmptagsnew.append("".join(currentword))
currentword = [c]
justchanged = True
currentupper = c.isupper()
tmptagsnew.append("".join(currentword))
tmptags += tmptagsnew
#try to deal with unicode
def utf8tag(tag):
try:
return tag.encode('utf-8', 'ignore')
except:
return ""
tmptags = [utf8tag(e) for e in tmptags]
# delete duplicates and the empty string
tmptags = list(set(tmptags))
stopwords = ['THE', 'IL', 'UN', 'UNA', 'GLI', 'LE', 'LO', 'A', 'E', 'I', 'O', 'L', 'OF', 'DI', 'DA']
tmptags = [e for e in tmptags if len(e) > 0 and not e.upper() in stopwords]
self.addTags(tmptags)
def tokenize(self):
return [tok for tok in self.uri.split('/') if len(tok) > 0]
def getFilename(self):
splituri = self.uri.split('/')
res = ""
try:
res = splituri[-1]
except IndexError:
res = self.uri
return res
def getServer(self):
return self.server
def getPath(self):
splituri = self.uri.split('/')
try:
res = "/".join(splituri[4:-1])
except IndexError:
res = ""
return res
def getShare(self):
splituri = self.uri.split('/')
try:
res = splituri[3]
except IndexError:
res = ""
return res
def getFileSize(self):
return self.filesize
def __repr__(self):
return self.uri + " {" + self.comments + "} " + str(list(self.tags))
def __str__(self):
try:
r = repr(self)
except UnicodeEncodeError:
try:
r = repr(self).encode('utf-8', 'ignore')
except UnicodeEncodeError:
r = ""
return r
def __eq__(self, other):
try:
scmpuri = self.uri.split('/')[-1]
ocmpuri = other.uri.split('/')[-1]
except IndexError:
return False
return scmpuri == ocmpuri
def __ne__(self, other):
return not (self == other)
class Query(Resource):
def __init__(self, query):
Resource.__init__(self)
self.uri = query
if self.uri.upper().startswith("FARMSAY"):
commands.getoutput("""echo '(SayText "%s")' | nc localhost 1314""" % self.uri[7:])
class ResourceTrie():
def __init__(self, label="", rank=0):
self.label = label
self.children = dict()
self.resources = list()
self.nres = 0
self.rank = rank
def insert(self, resource):
self.__insert(resource, resource.tokenize())
def __insert(self, resource, tokens=None):
if len(tokens) <= 1:
if resource.filetype != "directory":
self.resources.append(resource)
self.nres += 1
return 1
else:
return 0
t0 = tokens[0]
if not self.children.has_key(t0):
newtrie = ResourceTrie(t0, self.rank+1)
self.children.update({t0: newtrie})
res = self.children[t0].__insert(resource, tokens[1:])
self.nres += res
return res
def fancyrepr(self, spacen=0):
spaces = " " * spacen
spacesr = " " * (spacen+1)
res = ""
res += spaces + "|-" + self.label + "\n"
for resource in self.resources:
res += spacesr + "|-* " + resource.uri + "\n"
for child in self.children.values():
res += child.fancyrepr(spacen + 1) + "\n"
return res
def prune(self):
"delete subtries that differ only on the server's IP address/hostname"
if self.rank == 0:
for child in self.children.values():
if child.rank > 0: #should always be True
child.prune()
return
res = {}
blacklist = []
childrenkv = [(k,v) for k,v in self.children.iteritems()]
for i in range(len(childrenkv)):
if i in blacklist:
continue
ki, ci = childrenkv[i]
for j in range(len(childrenkv)):
if i == j:
continue
cj = childrenkv[j][1]
if ci == cj:
blacklist.append(j)
res.update({ki: ci})
self.children = res
def __str__(self):
return self.fancyrepr()
def __eq__(self, other):
if self.rank > 3 and other.rank > 3: #don't compare the top labels, i.e. URL scheme and server IP
if self.label != other.label:
return False
if len(self.children.keys()) != len(other.children.keys()):
return False
if len(self.resources) != len(other.resources):
return False
for rs, ro in zip(self.resources, other.resources):
if rs != ro:
return False
for cs, co in zip(self.children.values(), other.children.values()):
if not cs.__eq__(co):
return False
return True
def __ne__(self, other):
return not self.__eq__(other)
def dictify(self):
"return a nested dictionary representing this trie"
resresources = list()
for resource in self.resources:
resresources.append({
'uri': resource.uri,
'filename': resource.getFilename(),
'filetype': resource.filetype,
'filesize': resource.getFileSize(),
'server': resource.getServer(),
'share': resource.getShare(),
'path': resource.getPath(),
})
reschildren = list()
for child in self.children.values():
reschildren.append(child.dictify())
resdict = {'label': self.label, 'resources': resresources, 'children': reschildren, 'rank': self.rank}
return resdict
if __name__ == "__main__":
r = Resource(uri="smb://10.0.1.1/public.h/uuuu/ciao.ciao/bello.mp3", server="10.0.1.1")
r.makeTags()
print r
print r.protocol, "|", r.server, "|", r.path, "|", r.filetype
q = Query("ciao")
q.makeTags()
print q
q = Query("VinicioCapossela")
q.makeTags()
print q
r1 = Resource(uri="smb://10.0.1.1/public.h/uuuu/ciaociao/brutto.avi", server="10.0.1.1")
r1.makeTags()
print r1
print r1.protocol, "|", r1.server, "|", r1.path, "|", r1.filetype
print "-----"
rt = ResourceTrie()
rt.insert(r)
rt.insert(r1)
print "-----"
print str(rt)
print "-----"
r2 = Resource(uri="smb://10.0.2.1/public.h/uuuu/ciaociao/brutto.avi", server="10.0.2.1")
rt2 = ResourceTrie()
rt2.insert(r)
print rt == rt2
print rt != rt2
rt2.insert(r1)
print rt == rt2
print rt != rt2
rt2.insert(r2)
print rt == rt2
rt.insert(r2)
print rt == rt2
print "-----"
rt = ResourceTrie()
rt.insert(r1)
rt2 = ResourceTrie()
rt2.insert(r2)
print rt == rt2
print "-----"
rt = ResourceTrie()
rt.insert(r1)
rt.insert(r2)
print str(rt)
print "--------<>-----"
rt.prune()
print "--------<>-----"
print str(rt)
print rt.dictify()