sbsv2/raptor/python/raptor_xml.py
changeset 713 7b7f0409fc00
parent 674 37ee82a83d43
equal deleted inserted replaced
712:df89378e9223 713:7b7f0409fc00
   171 
   171 
   172 
   172 
   173 class SystemModel(object):
   173 class SystemModel(object):
   174 	"""A representation of the SystemModel section of a Symbian system_definition.xml file."""
   174 	"""A representation of the SystemModel section of a Symbian system_definition.xml file."""
   175 
   175 
   176 	def __init__(self, aLogger, aSystemDefinitionFile, aSystemDefinitionBase):
   176 	def __init__(self, aLogger, aSystemDefinitionFile = None, aSystemDefinitionBase = None, aDoRead = True):
   177 		self.__Logger = aLogger
   177 		self.__Logger = aLogger
   178 		self.__SystemDefinitionFile = aSystemDefinitionFile.GetLocalString()
   178 
   179 		self.__SystemDefinitionBase = aSystemDefinitionBase.GetLocalString()
   179 		if aSystemDefinitionFile:
       
   180 			self.__SystemDefinitionFile = aSystemDefinitionFile.GetLocalString()
       
   181 		else:
       
   182 			self.__SystemDefinitionFile = generic_path.Path('undefined').GetLocalString()
       
   183 
       
   184 		if aSystemDefinitionBase:
       
   185 			self.__SystemDefinitionBase = aSystemDefinitionBase.GetLocalString()
       
   186 		else:
       
   187 			self.__SystemDefinitionBase = generic_path.Path('undefined').GetLocalString()
       
   188 
   180 		self.__Version = {'MAJOR':0,'MID':0,'MINOR':0}
   189 		self.__Version = {'MAJOR':0,'MID':0,'MINOR':0}
   181 		self.__IdAttribute = "name"
   190 		self.__IdAttribute = "name"
   182 		self.__ComponentRoot = ""
   191 		self.__ComponentRoot = ""
   183 		self.__TotalComponents = 0
   192 		self.__TotalComponents = 0
   184 		self.__LayerList = []
   193 		self.__LayerList = []
   186 		self.__MissingBldInfs = {}
   195 		self.__MissingBldInfs = {}
   187 
   196 
   188 		self.__DOM = None
   197 		self.__DOM = None
   189 		self.__SystemDefinitionElement = None
   198 		self.__SystemDefinitionElement = None
   190 
   199 
   191 		if self.__Read():
   200 		if aDoRead:
   192 			if self.__Validate():
   201 			if self.__Read():
   193 				self.__Parse()
   202 				if self.__Validate():
   194 
   203 					self.__Parse()
   195 		if self.__DOM:
   204 
   196 			self.__DOM.unlink()
   205 			if self.__DOM:
       
   206 				self.__DOM.unlink()
   197 
   207 
   198 	def HasLayer(self, aLayer):
   208 	def HasLayer(self, aLayer):
   199 		return aLayer in self.__LayerList
   209 		return aLayer in self.__LayerList
   200 
   210 
   201 	def GetLayerNames(self):
   211 	def GetLayerNames(self):
   202 		return self.__LayerList
   212 		return self.__LayerList
       
   213 
       
   214 	def AddComponent(self, aComponent):
       
   215 		'''Add a dummy component, sufficient for the purposes of
       
   216 		writing a new system definition file. Argument is a Raptor
       
   217 		Component object.
       
   218 		'''
       
   219 		layername = aComponent.layername
       
   220 		if layername == '':
       
   221 			raise Exception("Can't add a component ("+str(aComponent.bldinf_filename)+") without a layer name to a system defintion file")
       
   222 		containers = {'layer':layername,'component':aComponent.componentname}
       
   223 		component = SystemModelComponent(aComponent.bldinf_filename, layername, containers, self.__SystemDefinitionFile, self.__SystemDefinitionBase, self.__Version)
       
   224 
       
   225 		if not layername in self.__LayerList:
       
   226 			self.__LayerList.append(layername)
       
   227 
       
   228 		if not self.__LayerDetails.has_key(layername):
       
   229 			self.__LayerDetails[layername] = []
       
   230 		self.__LayerDetails[layername].append(component)
   203 
   231 
   204 	def GetLayerComponents(self, aLayer):
   232 	def GetLayerComponents(self, aLayer):
   205 		if not self.HasLayer(aLayer):
   233 		if not self.HasLayer(aLayer):
   206 			self.__Logger.Error("System Definition layer \"%s\" does not exist in %s", aLayer, self.__SystemDefinitionFile)
   234 			self.__Logger.Error("System Definition layer \"%s\" does not exist in %s", aLayer, self.__SystemDefinitionFile)
   207 			return []
   235 			return []
   224 
   252 
   225 		for layer in self.GetLayerNames():
   253 		for layer in self.GetLayerNames():
   226 			components.extend(self.GetLayerComponents(layer))
   254 			components.extend(self.GetLayerComponents(layer))
   227 
   255 
   228 		return components
   256 		return components
   229 
       
   230 	def DumpLayerInfo(self, aLayer):
   257 	def DumpLayerInfo(self, aLayer):
   231 		if self.HasLayer(aLayer):
   258 		if self.HasLayer(aLayer):
   232 			self.__Logger.Info("Found %d bld.inf references in layer \"%s\"", len(self.GetLayerComponents(aLayer)), aLayer)
   259 			self.__Logger.Info("Found %d bld.inf references in layer \"%s\"", len(self.GetLayerComponents(aLayer)), aLayer)
   233 
   260 
   234 	def DumpInfo(self):
   261 	def DumpInfo(self):
   236 		self.__Logger.Info("\t%s", ", ".join(self.GetLayerNames()))
   263 		self.__Logger.Info("\t%s", ", ".join(self.GetLayerNames()))
   237 		self.__Logger.InfoDiscovery(object_type = "layers",
   264 		self.__Logger.InfoDiscovery(object_type = "layers",
   238 				count = len(self.GetLayerNames()))
   265 				count = len(self.GetLayerNames()))
   239 		self.__Logger.InfoDiscovery(object_type = "bld.inf references",
   266 		self.__Logger.InfoDiscovery(object_type = "bld.inf references",
   240 				count = len(self.GetAllComponents()))
   267 				count = len(self.GetAllComponents()))
       
   268 		
       
   269 	def Write(self, aFilename):
       
   270 		"""Write out a system definition that can be used to create an
       
   271 		identical SystemModel object.
       
   272 		Note it isn't guaranteed to be a valid system definition - just one
       
   273 		that will unserialise to an object identical to this one
       
   274 		"""
       
   275 		impl = xml.dom.minidom.getDOMImplementation()
       
   276 		self.__DOM = impl.createDocument(None, "SystemDefinition", None)
       
   277 		self.__SystemDefinitionElement = self.__DOM.documentElement
       
   278 		self.__DOM.insertBefore(self.__DOM.createComment('This document is generated by Raptor.  Please do not edit.'),self.__SystemDefinitionElement)
       
   279 		self.__SystemDefinitionElement.setAttribute('name','MCL')
       
   280 		self.__SystemDefinitionElement.setAttribute('schema','2.0.0')
       
   281 		systemModelNode = self.__DOM.createElement('systemModel')
       
   282 		self.__SystemDefinitionElement.appendChild(systemModelNode)
       
   283 		for layer in self.__LayerList:
       
   284 			if len(self.__LayerDetails[layer]) == 0:
       
   285 				continue
       
   286 			if layer == '':
       
   287 				self.__Logger.Error("Can't write out layer with no name to "+aFilename)
       
   288 			else:
       
   289 				layerNode = self.__DOM.createElement('layer')
       
   290 				layerNode.setAttribute('name',layer)
       
   291 				systemModelNode.appendChild(layerNode)
       
   292 				for component in self.__LayerDetails[layer]:
       
   293 					componentNode = self.__DOM.createElement('component')
       
   294 					componentNode.setAttribute('name',component.GetContainerName('component'))
       
   295 					layerNode.appendChild(componentNode)
       
   296 					path = str(component)
       
   297 					unitNode = self.__DOM.createElement('unit')
       
   298 					unitNode.setAttribute('bldFile',path)
       
   299 					componentNode.appendChild(unitNode)
       
   300 		
       
   301 		# Record that we haven't stripped the file names off our bld.infs
       
   302 		self.__SystemDefinitionElement.setAttribute('fullbldinfs','True')
       
   303 
       
   304 		self.__DOM.writexml(open(aFilename,"w"),newl="\n",indent="",addindent="\t")
       
   305 
       
   306 		self.__DOM.unlink()		
   241 
   307 
   242 	def __Read(self):
   308 	def __Read(self):
   243 		if not os.path.exists(self.__SystemDefinitionFile):
   309 		if not os.path.exists(self.__SystemDefinitionFile):
   244 			self.__Logger.Error("System Definition file %s does not exist", self.__SystemDefinitionFile)
   310 			self.__Logger.Error("System Definition file %s does not exist", self.__SystemDefinitionFile)
   245 			return False
   311 			return False
   270 			return False
   336 			return False
   271 
   337 
   272 		self.__Version['MAJOR'] = int(version.group('MAJOR'))
   338 		self.__Version['MAJOR'] = int(version.group('MAJOR'))
   273 		self.__Version['MID'] = int(version.group('MID'))
   339 		self.__Version['MID'] = int(version.group('MID'))
   274 		self.__Version['MINOR'] = int(version.group('MINOR'))
   340 		self.__Version['MINOR'] = int(version.group('MINOR'))
       
   341 
       
   342 		self.__fullbldinfs = None
       
   343 		if self.__SystemDefinitionElement.hasAttribute('fullbldinfs'):
       
   344 			# Lower case it since we're not evil
       
   345 			if self.__SystemDefinitionElement.getAttribute('fullbldinfs').lower() == 'true':
       
   346 				self.__fullbldinfs = 1
   275 
   347 
   276 		if self.__Version['MAJOR'] == 1 and self.__Version['MID'] > 2:
   348 		if self.__Version['MAJOR'] == 1 and self.__Version['MID'] > 2:
   277 			self.__ComponentRoot = self.__SystemDefinitionBase
   349 			self.__ComponentRoot = self.__SystemDefinitionBase
   278 		elif self.__Version['MAJOR'] == 2 or self.__Version['MAJOR'] == 3:
   350 		elif self.__Version['MAJOR'] == 2 or self.__Version['MAJOR'] == 3:
   279 			# 2.0.x and 3.0.0 formats support SOURCEROOT or SRCROOT as an environment specified base - we respect this, unless
   351 			# 2.0.x and 3.0.0 formats support SOURCEROOT or SRCROOT as an environment specified base - we respect this, unless
   380 							# Assume that this is an error i.e. don't attempt to resolve in relation to SOURCEROOT
   452 							# Assume that this is an error i.e. don't attempt to resolve in relation to SOURCEROOT
   381 							bldInfRoot = None
   453 							bldInfRoot = None
   382 							self.__Logger.Error("Cannot resolve \'root\' attribute value \"%s\" in %s", rootValue, self.__SystemDefinitionFile)
   454 							self.__Logger.Error("Cannot resolve \'root\' attribute value \"%s\" in %s", rootValue, self.__SystemDefinitionFile)
   383 							return
   455 							return
   384 
   456 
   385 				group = generic_path.Path(bldFileValue)
   457 				bldinfval = generic_path.Path(bldFileValue)
   386 
   458 
   387 				if self.__Version['MAJOR'] < 3:
   459 				if self.__Version['MAJOR'] < 3:
   388 					# absolute paths are not changed by root var in 1.x and 2.x
   460 					# absolute paths are not changed by root var in 1.x and 2.x
   389 					if not group.isAbsolute() and bldInfRoot:
   461 					if not bldinfval.isAbsolute() and bldInfRoot:
   390 						group = generic_path.Join(bldInfRoot, group)
   462 						bldinfval = generic_path.Join(bldInfRoot, bldinfval)
   391 				else:
   463 				else:
   392 					# relative paths for v3
   464 					# relative paths for v3
   393 					if not group.isAbsolute():
   465 					if not bldinfval.isAbsolute():
   394 						group = generic_path.Join(generic_path.Join(self.__SystemDefinitionFile).Dir(),group)
   466 						bldinfval = generic_path.Join(generic_path.Join(self.__SystemDefinitionFile).Dir(),bldinfval)
   395 					# absolute paths for v3
   467 					# absolute paths for v3
   396 					# are relative to bldInfRoot if set, or relative to the drive root otherwise
   468 					# are relative to bldInfRoot if set, or relative to the drive root otherwise
   397 					elif bldInfRoot:
   469 					elif bldInfRoot:
   398 						group = generic_path.Join(bldInfRoot, group)
   470 						bldinfval = generic_path.Join(bldInfRoot, bldinfval)
   399 
   471 				
   400 				bldinf = generic_path.Join(group, "bld.inf").FindCaseless()
   472 				if self.__fullbldinfs:
       
   473 					bldinf = bldinfval.FindCaseless()
       
   474 				else:
       
   475 					bldinf = generic_path.Join(bldinfval, "bld.inf").FindCaseless()
   401 
   476 
   402 				if bldinf == None:
   477 				if bldinf == None:
   403 					# recording layers containing non existent bld.infs
   478 					# recording layers containing non existent bld.infs
   404 					bldinfname = group.GetLocalString()
   479 					bldinfname = bldinfval.GetLocalString()
   405 					bldinfname = bldinfname+'/'+'bld.inf'
   480 					if not self.__fullbldinfs:
       
   481 						bldinfname = bldinfname+'/'+'bld.inf'
   406 					layer = self.__GetEffectiveLayer(aElement)
   482 					layer = self.__GetEffectiveLayer(aElement)
   407 					if not layer in self.__MissingBldInfs:
   483 					if not layer in self.__MissingBldInfs:
   408 						self.__MissingBldInfs[layer]=[]
   484 						self.__MissingBldInfs[layer]=[]
   409 					self.__MissingBldInfs[layer].append(bldinfname)
   485 					self.__MissingBldInfs[layer].append(bldinfname)
   410 
   486