-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvmodel.py
482 lines (415 loc) · 17.1 KB
/
vmodel.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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
import copy
import re
from typing import Optional, List, Set
from graphviz import Digraph
import datastructs
from condition_data_structure import Rule
from datalayer import Definition, Literal, Reference, Target, Concat, Select, CustomCommand, Option
from datastructs import DefinitionNode, LiteralNode, Node, Lookup, RefNode, SelectNode, TargetNode, TestNode, ConcatNode, \
CustomCommandNode, OptionNode
from graph_illustration import getNodeShape, getEdgeLabel
VARIABLE_REGEX = r"\${(\S*)}"
def infinite_sequence():
num = 0
while True:
yield num
num += 1
class VModel:
_instance = None
RESERVED_NODES = {
'NOT': LiteralNode('NOT'),
'OR': LiteralNode('OR')
}
def __init__(self):
Node.created_commands = dict()
self.flattenMemoize = {}
self.nodes: Optional[List[Node]] = []
self.cmakeVersion = None
self.ifLevel = 0
self.ifConditions = []
self.targets = set()
self.testTargets = set()
self.options = dict()
self.lookupTable = Lookup.getInstance()
self.recordCommands = False
self.COUNTER = infinite_sequence()
self.tmpLookupTableStack = []
self.systemState: List[Rule] = []
self.functions = {}
self.currentFunctionCommand = None
# Defined properties
self.definedProperties = {
'GLOBAL': {},
'DIRECTORY': {},
'TARGET': {},
'SOURCE': {},
'TEST': {},
'VARIABLE': {},
'CACHED_VARIABLE': {}
}
# These data structures are for properties related to a directory
self.directory_to_properties = {'.': Lookup()}
self.directory_definition_stack = {}
self.DIRECTORY_PROPERTIES = self.directory_to_properties.get('.')
self.DIRECTORY_PROPERTIES.setKey('VARIABLES', self.lookupTable.items[-1])
# A temp variable to keep changes between nodes
self.nodeStack = []
# A new property to support nested if statements
self.ifLevel = 0
self.definitionOrder = 0
def pushSystemState(self, rule: Rule):
self.systemState.append(rule)
def getCurrentSystemState(self) -> Optional[Rule]:
if self.systemState:
return self.systemState[-1]
else:
return None
def popSystemState(self):
return self.systemState.pop()
def pushCurrentLookupTable(self):
newLookup = copy.copy(self.lookupTable)
self.tmpLookupTableStack.append(newLookup)
def getLastPushedLookupTable(self) -> Lookup:
return self.tmpLookupTableStack[-1]
def popLookupTable(self):
return self.tmpLookupTableStack.pop()
def getNextCounter(self):
return str(next(self.COUNTER))
def getDefinitionOrder(self):
self.definitionOrder += 1
return self.definitionOrder
def addOption(self, optionName, initialValue):
self.options[optionName] = initialValue
def enableRecordCommands(self):
self.recordCommands = True
def disableRecordCommands(self):
self.recordCommands = False
def shouldRecordCommand(self):
return self.recordCommands
def setInsideIf(self):
self.ifLevel += 1
def setOutsideIf(self):
self.ifLevel -= 1
def isInsideIf(self):
return self.ifLevel > 0
def setParents(self):
result = set()
# result.add(node)
nodesToVisit = []
for node in self.nodes:
nodesToVisit.append(node)
while nodesToVisit:
node = nodesToVisit.pop()
if node in result:
continue
result.add(node)
if node.getChildren() is not None:
for child in node.getChildren():
child.addParent(node)
nodesToVisit += node.getChildren()
def getNodesCluster(self):
self.setParents()
nodeSet = self.getNodeSet()
clusters = []
for node in nodeSet:
if node.isVisited:
continue
newCluster = []
nodeToVisit = [node]
while nodeToVisit:
node = nodeToVisit.pop()
if node in newCluster:
continue
node.isVisited = True
newCluster.append(node)
nodeToVisit += node.getNeighbours()
clusters.append(newCluster)
return clusters
def getNodeSet(self) -> Set[Node]:
nodesToVisit = []
for node in self.nodes:
nodesToVisit.append(node)
nodes = set()
while nodesToVisit:
node = nodesToVisit.pop()
if node in nodes:
continue
nodes.add(node)
if node.getChildren() is not None:
nodesToVisit += [childNode for childNode in node.getChildren() if childNode is not None]
# nodes.update(node.getChildren())
return nodes
@staticmethod
def getNodeChildren(node: Node) -> Set[Node]:
nodesToVisit = [node]
result = set()
while nodesToVisit:
node = nodesToVisit.pop()
if node in result:
continue
result.add(node)
if node.getChildren() is not None:
nodesToVisit += node.getChildren()
return result
def findNode(self, name: str):
# Some reserved keywords like NOT AND OR etc use too many times. We don't want to search for them nor
# Create a new node. Also, if a keyword used more than one for the first time in a string, like
# A AND B AND C, previously this function created two AND because cannot find the first one.
if name in self.RESERVED_NODES:
return self.RESERVED_NODES.get(name)
for node in self.getNodeSet():
if node.getName() == name:
return node
return None
# Iterate over a list of arguments, if it's a variable, we return the corresponding RefNode
# Otherwise we return or create a literal node for that
def flatten(self, textToFlat: List[str]) -> List[Node]:
result = []
processedText = []
# To prevent stacking up double quotation
for item in textToFlat:
tempText = re.split("(\${[A-Za-z_][A-Za-z0-9_]*})", item)
for t in tempText:
if len(t) and t[0] == '"':
t = t[1:]
if len(t) and t[-1] == '"':
t = t[:-1]
if t != "":
processedText.append(t)
for item in processedText:
variableName = re.findall(VARIABLE_REGEX, item)
if variableName:
node = self.lookupTable.getKey("${{{}}}".format(variableName[0]))
# Create an empty RefNode for variables which are not defined like ${CMAKE_CURRENT_BINARY_DIR}
if not node:
node = RefNode("{}".format(variableName[0]), None)
self.lookupTable.setKey("${{{}}}".format(variableName[0]), node)
# if not isinstance(node, RefNode):
# raise Exception("NOT_IMPLEMENTED")
result.append(node)
else:
# We might have a target with the same name
node = self.lookupTable.getKey(f't:{item}')
if not node:
node = self.findNode(item)
if not node:
if item[0] == '"' and item[-1] == '"':
item = item[:-1]
item = item[1:]
node = LiteralNode(item, item)
result.append(node)
return result
def addNode(self, node: Node):
previousNode = self.findNode(node.getName())
if self.isInsideIf():
selectNodeName = "SELECT_{}_{}".format(node.getName(), " ".join(self.ifConditions))
if self.findNode(selectNodeName):
raise Exception("DUPLICATE_SELECT_NODE_FOUND!")
newSelectNode = SelectNode(selectNodeName, self.ifConditions)
if isinstance(node, RefNode):
newSelectNode.setTrueNode(node.getPointTo())
if previousNode:
if not isinstance(previousNode, RefNode):
raise Exception("PREVIOUS NODE OF AN REF NODE SHOULD ALSO BE REF!")
newSelectNode.setFalseNode(previousNode.getPointTo())
previousNode.pointTo = newSelectNode
return
else:
node.pointTo = newSelectNode
self.nodes.append(node)
return
elif isinstance(node, TargetNode) or isinstance(node, TestNode):
newSelectNode.setTrueNode(node.pointTo)
node.pointTo = newSelectNode
self.nodes.append(node)
return
else:
raise Exception("NOT SUPPORTED YET!")
if previousNode and not self.isInsideIf():
raise Exception("NOT IMPLEMENTED YET!")
self.nodes.append(node)
def expand(self, expression: List[str], forceConcatNode=False) -> Node:
# A recursive function which retrieve or create nodes
if len(expression) == 1:
result = self.flatten(expression)
if len(result) == 1 and not forceConcatNode:
return result[0]
else:
concatNode = ConcatNode("{}".format(",".join(expression)))
concatNode.listOfNodes += result
concatNode.concatString = True
return concatNode
concatNode = ConcatNode(",".join(expression))
for expr in expression:
concatNode.addNode(self.expand([expr]))
return concatNode
@staticmethod
def convertOrGetConcatNode(node: Node):
if isinstance(node, ConcatNode):
return node
newConcatNode = ConcatNode("CONCATED_" + node.getName())
newConcatNode.addNode(node)
return newConcatNode
def export(self, writeToNeo=False, writeDotFile=True):
if writeDotFile:
dot = Digraph(comment='SDG')
dot.graph_attr['ordering'] = 'out'
clusterId = 0
for cluster in self.getNodesCluster():
newGraph = Digraph(name="cluster_" + str(clusterId))
[newGraph.node(node.getName(), node.getNodeName(), shape=getNodeShape(node)) for node in cluster]
visitedNodes = list()
for node in cluster:
if node in visitedNodes:
continue
visitedNodes.append(node)
if node.getChildren() is not None:
for child in node.getChildren():
newGraph.edge(node.getName(), child.getName(), label=getEdgeLabel(node, child))
clusterId += 1
dot.subgraph(newGraph)
dot.unflatten()
dot.render('graph.gv', view=False,format='pdf')
# Doing DFS to create nodes in NEO4j
if writeToNeo:
for node in self.getNodeSet():
if node.dbNode:
continue
self.exportToNeo(node)
def exportToNeo(self, node: Node):
dbNode = node.dbNode
if dbNode:
return dbNode
if isinstance(node, LiteralNode):
dbNode = Literal(name=node.getName(), value=node.getValue()).save()
node.dbNode = dbNode
if isinstance(node, OptionNode):
dbNode = Option(name=node.getName()).save()
node.dbNode = dbNode
if isinstance(node, RefNode):
dbNode = Reference(name=node.getName()).save()
if node.getPointTo():
refNode = self.exportToNeo(node.getPointTo())
dbNode.pointTo.connect(refNode)
node.dbNode = dbNode
if isinstance(node, TargetNode):
dbNode = Target(name=node.rawName, scope=node.scope).save()
pointToDBNode = None
definitionDBNode = None
librariesDBNode = None
if node.sources:
pointToDBNode = self.exportToNeo(node.sources)
dbNode.pointTo.connect(pointToDBNode)
if node.definitions:
definitionDBNode = self.exportToNeo(node.definitions)
dbNode.definitions.connect(definitionDBNode)
if node.linkLibraries:
librariesDBNode = self.exportToNeo(node.linkLibraries)
dbNode.linkLibraries.connect(librariesDBNode)
properties = {
'isExecutable': node.isExecutable,
'isAlias': node.isAlias,
'imported': node.imported,
'isObjectLibrary': node.isObjectLibrary,
'interfaceLibrary': node.interfaceLibrary,
'isCustomTarget': node.isCustomTarget,
'defaultBuildTarget': node.defaultBuildTarget,
'libraryType': node.libraryType,
}
dbNode.properties = properties
node.dbNode = dbNode
if isinstance(node, ConcatNode):
dbNode = Concat(name=node.getName()).save()
for localNode in node.getNodes():
dbNode.contains.connect(self.exportToNeo(localNode))
node.dbNode = dbNode
if isinstance(node, SelectNode):
dbNode = Select(name=node.getName()).save()
if node.trueNode:
dbNode.trueNode.connect(self.exportToNeo(node.trueNode))
if node.falseNode:
dbNode.falseNode.connect(self.exportToNeo(node.falseNode))
dbNode.condition.connect(self.exportToNeo(node.args))
node.dbNode = dbNode
if isinstance(node, CustomCommandNode):
dbNode = CustomCommand(name=node.getName()).save()
for localNode in node.commands:
if localNode is None:
continue
dbNode.commands.connect(self.exportToNeo(localNode))
for localNode in node.depends:
if localNode is None:
continue
dbNode.depends.connect(self.exportToNeo(localNode))
dbNode.extraInfo = node.extraInfo
if isinstance(node, DefinitionNode):
dbNode = Definition(name=node.getName()).save()
for localNode in node.commands:
if localNode is None:
continue
dbNode.commands.connect(self.exportToNeo(localNode))
for localNode in node.depends:
if localNode is None:
continue
dbNode.depends.connect(self.exportToNeo(localNode))
dbNode.from_dir = node.from_dir
dbNode.ordering = node.ordering
for localNode in node.inherits:
if localNode is None:
continue
dbNode.inherits.connect(self.exportToNeo(localNode))
return dbNode
def checkIntegrity(self):
nodeNames = []
for node in self.getNodeSet():
if node.getName() in nodeNames:
raise Exception("INTEGRITY CHECK FAILED!")
nodeNames.append(node.getName())
def findAndSetTargets(self):
for node in self.getNodeSet():
if isinstance(node, TargetNode):
self.targets.add(node)
if isinstance(node, TestNode):
self.testTargets.add(node)
def getLibrariesForTarget(self):
pass
def pathWithCondition(self, startNode, endNode, **kwargs):
nodeToVisit = [startNode]
visitedNode = []
while nodeToVisit:
node = nodeToVisit.pop()
if node == endNode:
return True
if node in visitedNode:
continue
visitedNode.append(node)
if node.getChildren() is not None:
if isinstance(node, SelectNode):
conditions = {}
for expression in node.conditionList:
if expression in kwargs:
conditions[expression] = kwargs[expression]
else:
conditions[expression] = "UNDEFINED"
if False in conditions.values():
if node.falseNode:
nodeToVisit.append(node.falseNode)
elif "UNDEFINED" in conditions.values():
if node.falseNode:
nodeToVisit.append(node.falseNode)
if node.trueNode:
nodeToVisit.append(node.trueNode)
else:
nodeToVisit.append(node.trueNode)
else:
nodeToVisit += node.getChildren()
return False
@classmethod
def getInstance(cls):
if cls._instance is None:
cls._instance = VModel()
return cls._instance
@classmethod
def clearInstance(cls):
datastructs.created_commands = dict()
cls._instance = None