From a537d223a76b9fcccdec94e8d11ade8d1e723d45 Mon Sep 17 00:00:00 2001
From: Alexey Min <alexey.min@gmail.com>
Date: Sat, 2 May 2020 03:17:39 +0300
Subject: [PATCH] port driver generator scripts to py3

---
 drivers/hisi/tzdriver/Makefile           |  2 +-
 drivers/hisi/tzdriver/cfc_graph.py       | 16 ++++++++++------
 drivers/hisi/tzdriver/cfc_graphgen.py    |  4 ++--
 drivers/hisi/tzdriver/cfc_rule_parser.py |  4 +++-
 4 files changed, 16 insertions(+), 10 deletions(-)

diff --git a/drivers/hisi/tzdriver/Makefile b/drivers/hisi/tzdriver/Makefile
index 45521e25..24b3235b 100644
--- a/drivers/hisi/tzdriver/Makefile
+++ b/drivers/hisi/tzdriver/Makefile
@@ -17,7 +17,7 @@ obj-$(CONFIG_TEE_LIBHWSECUREC) += libhwsecurec/
 obj-$(CONFIG_TEE_CFC) += cfc_data.o cfc_coresight.o
 $(obj)/cfc_data.o: $(obj)/cfc_data.c
 $(obj)/cfc_data.c: $(src)/cfc_graphgen.py $(src)/cfc_graph.py $(src)/cfc_codegen.py $(src)/cfc_rule_parser.py $(src)/template $(obj)/rule.out
-	+python $(srctree)/$(src)/cfc_codegen.py $(obj)/rule.out > $@
+	+python3 $(srctree)/$(src)/cfc_codegen.py $(obj)/rule.out > $@
 $(obj)/rule.out: $(src)/rule.in
 	aarch64-linux-android-cpp $(srctree)/$(src)/rule.in > $@
 
diff --git a/drivers/hisi/tzdriver/cfc_graph.py b/drivers/hisi/tzdriver/cfc_graph.py
index 9388f44d..b27cad76 100644
--- a/drivers/hisi/tzdriver/cfc_graph.py
+++ b/drivers/hisi/tzdriver/cfc_graph.py
@@ -55,13 +55,13 @@ class NFAState(object):
 
 	# add an out state for a given event
 	def addOutEdge(self, event, state):
-		if not self.transMap.has_key(event):
+		if event not in self.transMap:
 			self.transMap[event] = set()
 		self.transMap[event].add(state)
 
 	# add a set of out states for a given event
 	def addOutEdges(self, event, states):
-		if not self.transMap.has_key(event):
+		if event not in self.transMap:
 			self.transMap[event] = set()
 		self.transMap[event] |= states
 
@@ -92,6 +92,10 @@ class DFAState(object):
 	def __eq__(self, other):
 		return self.idx == other.idx or self.NFAStates == other.NFAStates
 
+	def __hash__(self):
+		# should hash the same values that are compared in __eq__()
+		return hash((self.idx, tuple(self.NFAStates)))
+
 	# add a NFAState into NFAStateSet, also merge the flags
 	# TODO: need a conflict detection method
 	def addNFAStateSingle(self, nfaState):
@@ -115,7 +119,7 @@ class DFAState(object):
 	def genNextDFAState(self, event):
 		nextDFAState = DFAState()
 		for nfaState in self.NFAStates:
-			if not nfaState.transMap.has_key(event):
+			if event not in nfaState.transMap:
 				continue
 			for nextNFAState in nfaState.transMap[event]:
 				nextDFAState.addNFAState(nextNFAState)
@@ -159,7 +163,7 @@ class Graph(object):
 
 	# add an event in dict and give it a sequential index
 	def addEvent(self, flags, value):
-		if not self.events.has_key(value):
+		if value not in self.events:
 			if flags & Event.FLAG_WILDCARD:
 				self.events[value] = Event(flags, value, Graph.EVENT_WILDCARD_IDX)
 			else:
@@ -196,7 +200,7 @@ class Graph(object):
 	def completeNFAGraph(self):
 		# 1. For GLOBAL events, non-STOP states (without out edge for these events) accepts them as loopback
 		for event in filter(lambda x: x.flags & Event.FLAG_GLOBAL != 0, self.events.values()):
-			for nfaState in filter(lambda x: x.flags & NFAState.FLAG_STOP == 0 and not x.transMap.has_key(event), self.NFAStates.values()):
+			for nfaState in filter(lambda x: x.flags & NFAState.FLAG_STOP == 0 and event not in x.transMap, self.NFAStates.values()):
 				nfaState.addOutEdge(event, nfaState)
 
 		# 2. For RESET events, non-STOP states accept them and go to STATE_STOP_IDX state
@@ -206,7 +210,7 @@ class Graph(object):
 
 		# 3. For WILDCARD events, replace it with all non-RESET events (there should be only 1 WILDCARD event)
 		for event in filter(lambda x: x.flags & Event.FLAG_WILDCARD != 0, self.events.values()):
-			for nfaState in filter(lambda x: x.transMap.has_key(event), self.NFAStates.values()):
+			for nfaState in filter(lambda x: event in x.transMap, self.NFAStates.values()):
 				wildcastNextNFAStates = nfaState.transMap.pop(event)
 				for otherEvent in self.events.values():
 					if otherEvent.flags & (Event.FLAG_RESET | Event.FLAG_WILDCARD) == 0:
diff --git a/drivers/hisi/tzdriver/cfc_graphgen.py b/drivers/hisi/tzdriver/cfc_graphgen.py
index 5d614ea5..9d6d261a 100644
--- a/drivers/hisi/tzdriver/cfc_graphgen.py
+++ b/drivers/hisi/tzdriver/cfc_graphgen.py
@@ -89,7 +89,7 @@ def parseCommandPatternUnit(context, graph, start, end, commandPattern):
 		return
 
 	# check if the var is known
-	if commandPattern != '.' and not context.markers.has_key(commandPattern):
+	if commandPattern != '.' and commandPattern not in context.markers:
 		raise Exception('unrecorgnized var: {}'.format(commandPattern))
 
 	# only ATTRIBUTE_SEND_DATA_START event can reach FLAG_WAIT_DATA state
@@ -161,7 +161,7 @@ def genNFAGraph(context):
 
 	start = graph.addNFAState()
 	for commandID, commandPattern in context.commandPatterns:
-		if not finalStates.has_key(commandID):
+		if commandID not in finalStates:
 			finalStates[commandID] = graph.addNFAState().setSucceed().setStop().setCMD(int(commandID))
 		parseCommandPattern(context, graph, start, finalStates[commandID], commandPattern)
 
diff --git a/drivers/hisi/tzdriver/cfc_rule_parser.py b/drivers/hisi/tzdriver/cfc_rule_parser.py
index bf91a966..03e32c93 100644
--- a/drivers/hisi/tzdriver/cfc_rule_parser.py
+++ b/drivers/hisi/tzdriver/cfc_rule_parser.py
@@ -61,7 +61,9 @@ class CFCRuleTextContent(object):
 	# Var := Symbol[, Attribute]
 	def parseMarker(self, s):
 		s = s.replace(',', ' ')
-		tokens = filter(bool, s.split())
+		# tokens = filter(bool, s.split())
+		# same as tokens = [element for element in s.split if bool(element)]
+		tokens = list(filter(bool, s.split()))
 		if len(tokens) < 3 or len(tokens) > 4:
 			raise Exception('Line {}: unexpected tokens numbers {}.'.format(self.lineNum, len(tokens)))
 		if tokens[1] != ':=':
-- 
2.25.3

