prototype of sysdef v3.0 parser wip
authorRichard Taylor <richard.i.taylor@nokia.com>
Mon, 22 Feb 2010 14:40:46 +0000
branchwip
changeset 332 dae2dfe18db2
parent 331 638166b27f05
child 333 0fe3c56ad89c
prototype of sysdef v3.0 parser
sbsv2/raptor/python/raptor_xml.py
--- a/sbsv2/raptor/python/raptor_xml.py	Thu Feb 18 16:11:10 2010 +0000
+++ b/sbsv2/raptor/python/raptor_xml.py	Mon Feb 22 14:40:46 2010 +0000
@@ -144,21 +144,25 @@
 	"""Path sub-class that wraps up a component bld.inf file with
 	system_definition.xml context information."""
 
-	def __init__(self, aBldInfFile, aContainerNames, aSystemDefinitionFile, aSystemDefinitionBase, aSystemDefinitionVersion):
+	def __init__(self, aBldInfFile, aLayerName, aContainerNames, aSystemDefinitionFile, aSystemDefinitionBase, aSystemDefinitionVersion):
 		generic_path.Path.__init__(self, aBldInfFile.Absolute().path)
 		self.__ContainerNames = aContainerNames
+		self.__LayerName = aLayerName
 		self.__SystemDefinitionFile = aSystemDefinitionFile
-		self.__SystemDefinitionBase = aSystemDefinitionBase
-		self.__SystemDefinitionVersion = aSystemDefinitionVersion
+		self.__SystemDefinitionBase = aSystemDefinitionBase
+		self.__SystemDefinitionVersion = aSystemDefinitionVersion
 
 	def GetSystemDefinitionFile(self):
 		return self.__SystemDefinitionFile
 
 	def GetSystemDefinitionBase(self):
 		return self.__SystemDefinitionBase
-
-	def GetSystemDefinitionFile(self):
-		return self.__SystemDefinitionVersion
+
+	def GetSystemDefinitionFile(self):
+		return self.__SystemDefinitionVersion
+
+	def GetLayerName(self):
+		return self.__LayerName
 
 	def GetContainerName(self, aContainerType):
 		if self.__ContainerNames.has_key(aContainerType):
@@ -173,7 +177,8 @@
 		self.__Logger = aLogger
 		self.__SystemDefinitionFile = aSystemDefinitionFile.GetLocalString()
 		self.__SystemDefinitionBase = aSystemDefinitionBase.GetLocalString()
-		self.__Version = {'MAJOR':0,'MID':0,'MINOR':0}
+		self.__Version = {'MAJOR':0,'MID':0,'MINOR':0}
+		self.__IdAttribute = "name"
 		self.__ComponentRoot = ""
 		self.__TotalComponents = 0
 		self.__LayerList = []
@@ -263,42 +268,62 @@
 
 		if self.__Version['MAJOR'] == 1 and self.__Version['MID'] > 2:
 			self.__ComponentRoot = self.__SystemDefinitionBase
-		elif self.__Version['MAJOR'] == 2:
-			# 2.0.0 format supports SOURCEROOT as an environment specified base - we respect this, unless
-			# explicitly overridden on the command line
-			if os.environ.has_key('SOURCEROOT'):
+		elif self.__Version['MAJOR'] == 2 or self.__Version['MAJOR'] == 3:
+			# 2.0.x and 3.0.0 formats support SOURCEROOT or SRCROOT as an environment specified base - we respect this, unless
+			# explicitly overridden on the command line
+			if os.environ.has_key('SRCROOT'):
+				self.__ComponentRoot = generic_path.Path(os.environ['SRCROOT'])
+			elif os.environ.has_key('SOURCEROOT'):
 				self.__ComponentRoot = generic_path.Path(os.environ['SOURCEROOT'])
-			if self.__SystemDefinitionBase and self.__SystemDefinitionBase != ".":
-				self.__ComponentRoot = self.__SystemDefinitionBase
-				if os.environ.has_key('SOURCEROOT'):
-					self.__Logger.Info("Command line specified System Definition file base \'%s\' overriding environment SOURCEROOT \'%s\'", self.__SystemDefinitionBase, os.environ['SOURCEROOT'])
+				
+			if self.__SystemDefinitionBase and self.__SystemDefinitionBase != ".":
+				self.__ComponentRoot = self.__SystemDefinitionBase
+				if os.environ.has_key('SRCROOT'):
+					self.__Logger.Info("Command line specified System Definition file base \'%s\' overriding environment SRCROOT \'%s\'", self.__SystemDefinitionBase, os.environ['SRCROOT'])
+				elif os.environ.has_key('SOURCEROOT'):
+					self.__Logger.Info("Command line specified System Definition file base \'%s\' overriding environment SOURCEROOT \'%s\'", self.__SystemDefinitionBase, os.environ['SOURCEROOT'])
 		else:
 			self.__Logger.Error("Cannot process schema version %s of file %s", version.string, self.__SystemDefinitionFile)
 			return False
+
+		if self.__Version['MAJOR'] >= 3:
+			# id is the unique identifier for 3.0 and later schema
+			self.__IdAttribute = "id"
 
 		return True
 
 	def __Parse(self):
-		# find the <systemModel> element (there can be 0 or 1) and search any <layer> elements for <unit> elements with "bldFile" attributes
-		# the <layer> context of captured "bldFile" attributes is recorded as we go
+		# For 2.0 and earlier: find the <systemModel> element (there can be 0 or 1) and search any <layer> elements for <unit> elements with "bldFile" attributes
+		# the <layer> context of captured "bldFile" attributes is recorded as we go
+		# For 3.0 and later, process any architectural topmost element, use the topmost element with an id as the "layer"
 		for child in self.__SystemDefinitionElement.childNodes:
-			if child.localName == "systemModel":
+			if child.localName == "systemModel" or child.localName == "layer" or child.localName == "package" or child.localName == "collection" or child.localName == "component":
 				self.__ProcessSystemModelElement(child)
 
 	def __CreateComponent(self, aBldInfFile, aUnitElement):
 		# take a resolved bld.inf file and associated <unit/> element and returns a populated Component object
 		containers = {}
-		self.__GetElementContainers(aUnitElement, containers)
-		component = SystemModelComponent(aBldInfFile, containers, self.__SystemDefinitionFile, self.__SystemDefinitionBase, self.__Version)
+		self.__GetElementContainers(aUnitElement, containers)
+		layer = self.__GetEffectiveLayer(aUnitElement)
+		component = SystemModelComponent(aBldInfFile, layer, containers, self.__SystemDefinitionFile, self.__SystemDefinitionBase, self.__Version)
 
 		return component
+
+	def __GetEffectiveLayer(self, aElement):
+		#' return the ID of the topmost item which has an ID. For 1.x and 2.x, this will always be layer, for 3.x, it will be the topmost ID'd element in the file
+		# never call this on the root element
+		if aElement.parentNode.hasAttribute(self.__IdAttribute):
+			return self.__GetEffectiveLayer(aElement.parentNode)
+		elif aElement.hasAttribute(self.__IdAttribute):
+			return aElement.getAttribute(self.__IdAttribute)
+		return ""
 
 	def __GetElementContainers(self, aElement, aContainers):
 		# take a <unit/> element and creates a type->name dictionary of all of its parent containers
 		# We're only interested in parent nodes if they're not the top-most node
 		if aElement.parentNode.parentNode:
 			parent = aElement.parentNode
-			name = parent.getAttribute("name")
+			name = parent.getAttribute(self.__IdAttribute)
 
 			if name:
 				aContainers[parent.tagName] = name
@@ -308,9 +333,10 @@
 	def __ProcessSystemModelElement(self, aElement):
 		"""Search for XML <unit/> elements with 'bldFile' attributes and resolve concrete bld.inf locations
 		with an appreciation of different schema versions."""
-
-		if aElement.tagName == "layer":
-			currentLayer = aElement.getAttribute("name")
+
+		# The effective "layer" is the item whose parent does not have an id (or name in 2.x and earlier)
+		if not aElement.parentNode.hasAttribute(self.__IdAttribute) :
+			currentLayer = aElement.getAttribute(self.__IdAttribute)
 
 			if not self.__LayerDetails.has_key(currentLayer):
 				self.__LayerDetails[currentLayer] = []
@@ -324,10 +350,10 @@
 			if bldFileValue:
 				bldInfRoot = self.__ComponentRoot
 
-				if self.__Version['MAJOR'] == 1 and self.__Version['MID'] == 4:
-					# version 1.4.x schema paths can use DOS slashes
+				if self.__Version['MAJOR'] == 1:
+					# version 1.x schema paths can use DOS slashes
 					bldFileValue = raptor_utilities.convertToUnixSlash(bldFileValue)
-				elif self.__Version['MAJOR'] == 2:
+				elif self.__Version['MAJOR'] >= 2:
 					# version 2.x.x schema paths are subject to a "root" attribute off-set, if it exists
 					rootValue = aElement.getAttribute("root")
 
@@ -342,8 +368,14 @@
 
 				group = generic_path.Path(bldFileValue)
 
-				if not group.isAbsolute() and bldInfRoot:
-					group = generic_path.Join(bldInfRoot, group)
+				if self.__Version['MAJOR'] < 3:
+					# absolute paths are not changed by root var in 1.x and 2.x
+					if not group.isAbsolute() and bldInfRoot:
+						group = generic_path.Join(bldInfRoot, group)
+				else:
+					# only absolute paths are changed by root var in 3.x
+					if group.isAbsolute() and bldInfRoot:
+						group = generic_path.Join(bldInfRoot, group)
 
 				bldinf = generic_path.Join(group, "bld.inf").FindCaseless()
 
@@ -351,7 +383,7 @@
 					self.__Logger.Error("No bld.inf found at %s in %s", group.GetLocalString(), self.__SystemDefinitionFile)
 				else:
 					component = self.__CreateComponent(bldinf, aElement)
-					layer = component.GetContainerName("layer")
+					layer = component.GetLayerName()
 					if layer:
 						self.__LayerDetails[layer].append(component)
 						self.__TotalComponents += 1