-
Notifications
You must be signed in to change notification settings - Fork 4
/
keywordsfilter.py
executable file
·354 lines (313 loc) · 13 KB
/
keywordsfilter.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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
#!/usr/bin/python
import sys, re
import ply.yacc as yacc
import ply.lex as lex
import synonymmapping
from database import DB
# Lex =================================================================
reserved = {
'and' : 'AND',
'or' : 'OR'
}
tokens = [
'NAME', 'MULTINAME',
'LPAREN','RPAREN'
] + list(reserved.values())
t_LPAREN = r'\('
t_RPAREN = r'\)'
#Be very, VERY careful about adding symbols to these regexes.
# Adding a single or double quote could allow arbitrary code execution thanks to a eval function
# Any changes must also be made to regex in setup and the regex in _stripIllegalCharacters
def t_MULTINAME(t):
r'"[-_a-zA-Z0-9./]+ [-_a-zA-Z0-9./ ]+"'
t.type = reserved.get(t.value, 'MULTINAME')
return t
def t_NAME(t):
r'"?[-_a-zA-Z0-9./]+"?'
t.type = reserved.get(t.value, 'NAME')
return t
t_ignore = " \t\n"
def t_error(t):
print("Illegal character '%s'" % t.value[0])
t.lexer.skip(1)
# Structure ===========================================================
class Tree:
left = None
leftType = 'keyword'
right = None
rightType = 'keyword'
_keywordphrase = "(SELECT ck.keyword FROM "+ DB.commitkeyword._table +" as ck WHERE ck.commitid = c.id)"
_fulltextphrase_sql = "(SELECT wm.word FROM "+ DB.commitwordmap._table +" as wm WHERE wm.commitid = c.id)"
_fulltextphrase_eval = "c.testFulltext('%s')"
_projectphrase_sql = "r.tagname"
_projectphrase_eval = "c.repo.tagname"
_maturityphrase_sql = "r.maturity"
_maturityphrase_eval = "c.repo.maturity"
def __init__(self, l, r):
self.left = l
self.right = r
doLeft = doRight = False
if isinstance(self.left, str) or isinstance(self.left, unicode):
doLeft = True
self.leftType = 'keyword'
else:
self.leftType = 'tree'
if isinstance(self.right, str) or isinstance(self.right, unicode):
doRight = True
self.rightType = 'keyword'
elif self.right is None:
self.rightType = 'None'
else:
self.rightType = 'tree'
if not (doLeft or doRight):
return
if doLeft: self.leftType = 'fulltext'
if doRight: self.rightType = 'fulltext'
for n in synonymmapping.getMap():
if doLeft and n.__str__() == self.left:
self.leftType = 'keyword'
if doRight and n.__str__() == self.right:
self.rightType = 'keyword'
def __repr__(self):
s = "(" + repr(self.left) + " " + self.mode + " " + repr(self.right) + ")"
return s
def _getEvalNode(self, type, node, nodeType):
evalstring = ""
components = []
if nodeType == 'keyword':
if node.startswith("project-"):
if type == 'sql': evalstring += " " + self._projectphrase_sql + " = %s"
elif type == 'eval': evalstring += " " + self._projectphrase_eval + " == '%s'"
components.append(node.replace('project-', ''))
elif node.startswith("maturity-"):
if type == 'sql': evalstring += " " + self._maturityphrase_sql + " = %s"
elif type == 'eval': evalstring += " " + self._maturityphrase_eval + " == '%s'"
components.append(node.replace('maturity-', ''))
else:
if type == 'sql': evalstring += "%s IN " + self._keywordphrase
elif type == 'eval': evalstring += "'%s' in " + self._keywordphrase
components.append(node)
elif nodeType == 'fulltext':
if type == 'sql': evalstring += "%s IN " + self._fulltextphrase_sql
elif type == 'eval': evalstring += self._fulltextphrase_eval
components.append(node)
else:
innersql, newcomponents = node.getEvaluationString(type)
if node.right: evalstring += "(" + innersql + ")"
else: evalstring += innersql
components.extend(newcomponents)
return evalstring, components
def anyTree(self):
if self.leftType == 'tree':
return True
if self.rightType == 'tree':
return True
return False
def anyFulltext(self):
if self.leftType == 'fulltext':
return True
if self.rightType == 'fulltext':
return True
if self.leftType == 'tree':
if self.left.anyFulltext(): return True
if self.rightType == 'tree':
if self.right.anyFulltext(): return True
return False
def getEvaluationString(self, type):
evalstring, components = self._getEvalNode(type, self.left, self.leftType)
#We overload the AndTree to handle the single-node case
if self.right:
evalstring += " " + self.mode + " "
nextsql, newcomponents = self._getEvalNode(type, self.right, self.rightType)
evalstring += nextsql
components.extend(newcomponents)
return evalstring, components
def printTree(self, indent):
if not self.right:
if self.leftType == 'tree':
self.left.printTree(indent)
else:
print indent + self.left
else:
print indent + self.mode
if self.leftType == 'tree':
self.left.printTree(indent + "\t")
else:
print indent + self.left
if self.rightType == 'tree':
self.right.printTree(indent + "\t")
else:
print indent + self.right
class AndTree(Tree):
mode = "and"
class OrTree(Tree):
mode = "or"
# Yacc ================================================================
def p_expression_name(p):
'''expression : NAME
| MULTINAME'''
p[0] = AndTree(p[1].replace('"', ''), None)
def p_expression_names(p):
'expression : expression expression'
p[0] = AndTree(p[1], p[2])
def p_expression_and(p):
'expression : expression AND expression'
p[0] = AndTree(p[1], p[3])
def p_expression_or(p):
'expression : expression OR expression'
p[0] = OrTree(p[1], p[3])
def p_expression_parens(p):
'expression : LPAREN expression RPAREN'
p[0] = AndTree(p[2], None)
def p_error(p):
print "Syntax error in input: " + str(p) + "!"
lex.lex()
yacc.yacc()
# Interface ===========================================================
unallowed_characters = re.compile('[^-_a-zA-Z0-9./ ()]')
class KeywordsParser:
@staticmethod
def _isBalanced(keywords):
balancedParens = 0
for c in keywords:
if c == "(":
balancedParens += 1
elif c == ")":
balancedParens -= 1
if balancedParens < 0:
return False
return balancedParens == 0
@staticmethod
def _trimnonsense(tokens): #get rid of beginning or ending combining words, including inside parens
if tokens and tokens[0] in ["and", "or"]:
tokens.pop(0)
if tokens and tokens[-1] in ["and", "or"]:
tokens.pop()
for i in range(len(tokens) - 1):
thistoken = tokens[i]
nexttoken = tokens[i+1]
if thistoken == "(":
if nexttoken in ["and", "or"]:
tokens.pop(i+1)
return KeywordsParser._trimnonsense(tokens)
return tokens
@staticmethod
def _combinenonsense(tokens): #collapse repeated combining words e.g. 'slackware and and sha256'
if len(tokens) > 1:
for i in range(len(tokens)-1): #remove successive combination words
thistoken = tokens[i]
nexttoken = tokens[i+1]
if thistoken in ["and", "or"]:
if nexttoken in ["and", "or"]:
tokens.pop(i+1)
return KeywordsParser._combinenonsense(tokens)
for i in range(len(tokens)-1): #remove empty parens
thistoken = tokens[i]
nexttoken = tokens[i+1]
if thistoken == "(" and nexttoken == ")":
tokens.pop(i)
tokens.pop(i)
return KeywordsParser._combinenonsense(tokens)
return tokens
@staticmethod
def _stripIllegalCharacters(tokens): #Violently remove characters not permitted by t_NAME regex
tokens = unallowed_characters.sub('', tokens)
return tokens
@staticmethod
def _preProcess(keywords):
if not KeywordsParser._isBalanced(keywords):
keywords = keywords.replace("(", "").replace(")", "")
else:
keywords = keywords.replace("(", " ( ").replace(")", " ) ")
keywords = KeywordsParser._stripIllegalCharacters(keywords)
tokens = keywords.lower().split()
tokens = KeywordsParser._combinenonsense(tokens)
tokens = KeywordsParser._trimnonsense(tokens)
tokens = KeywordsParser._combinenonsense(tokens)
tokens = synonymmapping.projectizeTags(tokens)
return ' '.join(tokens)
def __init__(self, keywords):
self.keywords = KeywordsParser._preProcess(keywords)
if self.keywords:
self.result = yacc.parse(self.keywords)
else:
self.result = False
def getEvaluationString(self, type):
if self.result:
return self.result.getEvaluationString(type)
else:
return ('', [])
def anyFulltext(self):
return self.result.anyFulltext()
def dump(self):
if self.result:
evalstr, evalcomponents = self.result.getEvaluationString('eval')
print "\t", evalstr % tuple(evalcomponents)
evalstr, evalcomponents = self.result.getEvaluationString('sql')
print "\t", evalstr, evalcomponents
#self.result.printTree("\t")
else:
print ""
if __name__ == "__main__":
testcases = [
"slackware "
, "tag1 "
, "\"slackware\" "
,"and "
,"slackware testcases-git"
,"slackware or (tag1 and doxygen)"
,"slackware and testcases-git"
,"slackware or sha256"
,"slackware testcases-git maturity-suse"
,'"slackware testcases-git" maturity-suse'
,"slackware and and testcases-git maturity-suse"
,"slackware testcases-git and and maturity-suse"
,"slackware testcases-git and and maturity-suse and and"
,"and and slackware testcases-git and and maturity-suse and and"
,"slackware testcases-git and maturity-suse"
,"slackware and testcases-git maturity-suse"
,"slackware and testcases-git or maturity-suse"
,"or"
,"slackware testcases-git"
,"slackware or testcases-git"
,"slackware or testcases-git"
,"slackware testcases-git maturity-suse"
,"slackware or or testcases-git maturity-suse"
,"slackware testcases-git or or maturity-suse"
,"slackware testcases-git or or maturity-suse or or"
,"or or slackware testcases-git or or maturity-suse or or"
,"slackware testcases-git or maturity-suse"
,"slackware or testcases-git maturity-suse"
,"slackware or testcases-git or maturity-suse"
,"slackware and testcases-git or maturity-suse and tag4"
,")slackware( "
,"(and) "
,"((slackware) testcases-git)"
,"(slackware and testcases-git)"
,"(((slackware or sha256)))"
,"(slackware testcases-git) maturity-suse"
,"(slackware and and testcases-git) maturity-suse"
,"slackware (testcases-git and and maturity-suse)"
,"(slackware testcases-git) and and maturity-suse and and"
,"and and (slackware testcases-git) and and maturity-suse and and"
,"(slackware testcases-git) and maturity-suse"
,"(())()()()(())slackware and testcases-git maturity-suse"
,"(slackware and testcases-git) or maturity-suse"
,"slackware and (testcases-git or maturity-suse)"
,"(slackware or testcases-git) and maturity-suse"
,"slackware or (testcases-git and maturity-suse)"
,"(slackware and testcases-git) or (maturity-suse and tag4)"
,"slackware and (testcases-git or maturity-suse) and tag4"
,"slackware and (testcases-git or maturity-suse and tag4)"
,"(slackware and testcases-git or maturity-suse) and tag4"
,"(slackware and (testcases-git or maturity-suse)) and tag4"
,"slackware and ((testcases-git or maturity-suse) and tag4)"
,"tag1"
,"tag1' x"
,"tag1' or 1==1 '"
]
for t in testcases:
tree = KeywordsParser(t)
print t
tree.dump()
print ""