sbsv2/raptor/python/raptor_api.py
author jjkang
Fri, 25 Jun 2010 19:56:45 +0800
changeset 604 b33dd54aaa52
parent 602 3145852acc89
child 625 a1925fb7753a
permissions -rw-r--r--
add cpptoolsplat
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
602
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
     1
#
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
     2
# Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
     3
# All rights reserved.
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
     4
# This component and the accompanying materials are made available
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
     5
# under the terms of the License "Eclipse Public License v1.0"
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
     6
# which accompanies this distribution, and is available
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
     7
# at the URL "http://www.eclipse.org/legal/epl-v10.html".
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
     8
#
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
     9
# Initial Contributors:
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    10
# Nokia Corporation - initial contribution.
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    11
#
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    12
# Contributors:
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    13
#
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    14
# Description: 
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    15
#
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    16
# raptor_api module
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    17
#
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    18
# Python API for Raptor. External code should interact with Raptor via this
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    19
# module only, as it is the only programatic interface considered public. The
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    20
# command line --query option is also implemented using this module.
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    21
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    22
# constants
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    23
ALL = 1
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    24
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    25
# objects
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    26
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    27
class Reply(object):
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    28
	"""object to return values from API calls.
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    29
	"""
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    30
	def __init__(self, text=""):
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    31
		self.text = text
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    32
	
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    33
	def __str__(self):
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    34
		name = type(self).__name__.lower()
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    35
		
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    36
		string = "<" + name
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    37
		children = []
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    38
		longend = False
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    39
		
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    40
		for attribute,value in self.__dict__.items():
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    41
			if attribute != "text":
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    42
				if isinstance(value, Reply):
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    43
					children.append(value)
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    44
				else:
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    45
					string += " %s='%s'" % (attribute, value)
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    46
		
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    47
		if children or self.text:
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    48
			string += ">"
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    49
			longend = True
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    50
		
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    51
		if self.text:
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    52
			string += self.text
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    53
		
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    54
		if children:
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    55
			string += "\n"
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    56
				
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    57
		for c in children:
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    58
			string += str(c)
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    59
			
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    60
		if longend:
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    61
			string += "</%s>\n" % name
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    62
		else:	
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    63
			string += "/>\n"
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    64
		
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    65
		return string
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    66
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    67
class Alias(Reply):
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    68
	def __init__(self, name, meaning):
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    69
		super(Alias,self).__init__()
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    70
		self.name = name
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    71
		self.meaning = meaning
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    72
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    73
class Config(Reply):
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    74
	def __init__(self, fullname, outputpath):
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    75
		super(Config,self).__init__()
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    76
		self.fullname = fullname
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    77
		self.outputpath = outputpath
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    78
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    79
class Product(Reply):
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    80
	def __init__(self, name):
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    81
		super(Product,self).__init__()
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    82
		self.name = name
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    83
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    84
import generic_path
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    85
import raptor
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    86
import raptor_data
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    87
import re
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    88
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    89
class Context(object):
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    90
	"""object to contain state information for API calls.
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    91
	
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    92
	For example,
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    93
	
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    94
	api = raptor_api.Context()
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    95
	val = api.getaliases("X")
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    96
	"""
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    97
	def __init__(self, initialiser=None):
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    98
		# this object has a private Raptor object that can either be
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    99
		# passed in or created internally.
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   100
		
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   101
		if initialiser == None:
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   102
			self.__raptor = raptor.Raptor()
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   103
		else:
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   104
			self.__raptor = initialiser
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   105
			
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   106
	def stringquery(self, query):
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   107
		"""turn a string into an API call and execute it.
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   108
		
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   109
		This is a convenience method for "lazy" callers.
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   110
		
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   111
		The return value is also converted into a well-formed XML string.
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   112
		"""
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   113
		
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   114
		if query == "aliases":
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   115
			aliases = self.getaliases()
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   116
			return "".join(map(str, aliases)).strip()
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   117
		
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   118
		elif query == "products":
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   119
			variants = self.getproducts()
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   120
			return "".join(map(str, variants)).strip()
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   121
		
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   122
		elif query.startswith("config"):
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   123
			match = re.match("config\[(.*)\]", query)
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   124
			if match:
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   125
				config = self.getconfig(match.group(1))
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   126
				return str(config).strip()
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   127
			else:
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   128
				raise BadQuery("syntax error")
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   129
		
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   130
		raise BadQuery("unknown query")
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   131
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   132
	def getaliases(self, type=""):
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   133
		"""extract all aliases of a given type.
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   134
		
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   135
		the default type is "".
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   136
		to get all aliases pass type=ALL
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   137
		"""
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   138
		aliases = []
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   139
		
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   140
		for a in self.__raptor.cache.aliases.values():
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   141
			if type == ALL or a.type == type:
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   142
				# copy the members we want to expose
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   143
				aliases.append( Alias(a.name, a.meaning) )
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   144
			
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   145
		return aliases
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   146
	
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   147
	def getconfig(self, name):
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   148
		"""extract the values for a given configuration.
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   149
		
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   150
		'name' should be an alias or variant followed optionally by a
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   151
		dot-separated list of variants. For example "armv5_urel" or
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   152
		"armv5_urel.savespace.vasco".
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   153
		"""
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   154
		names = name.split(".")
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   155
		if names[0] in self.__raptor.cache.aliases:
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   156
			x = self.__raptor.cache.FindNamedAlias(names[0])
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   157
			
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   158
			if len(names) > 1:
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   159
				fullname = x.meaning + "." + ".".join(names[1:])
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   160
			else:
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   161
				fullname = x.meaning
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   162
				
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   163
		elif names[0] in self.__raptor.cache.variants:
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   164
			fullname = name
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   165
			
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   166
		else:
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   167
			raise BadQuery("'%s' is not an alias or a variant" % names[0])
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   168
		
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   169
		# create an evaluator for the named configuration
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   170
		tmp = raptor_data.Alias("tmp")
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   171
		tmp.SetProperty("meaning", fullname)
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   172
		
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   173
		units = tmp.GenerateBuildUnits(self.__raptor.cache)
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   174
		evaluator = self.__raptor.GetEvaluator(None, units[0])
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   175
		
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   176
		# get the outputpath
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   177
		# this is messy as some configs construct the path inside the FLM
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   178
		# rather than talking it from the XML: usually because of some
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   179
		# conditional logic... but maybe some refactoring could avoid that.
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   180
		releasepath = evaluator.Get("RELEASEPATH")
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   181
		if not releasepath:
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   182
			raise BadQuery("could not get RELEASEPATH for config '%s'" % name)
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   183
		
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   184
		variantplatform = evaluator.Get("VARIANTPLATFORM")
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   185
		varianttype = evaluator.Get("VARIANTTYPE")
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   186
		featurevariantname = evaluator.Get("FEATUREVARIANTNAME")
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   187
		
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   188
		platform = evaluator.Get("TRADITIONAL_PLATFORM")
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   189
		
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   190
		if platform == "TOOLS2":
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   191
			outputpath = releasepath
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   192
		else:
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   193
			if not variantplatform:
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   194
				raise BadQuery("could not get VARIANTPLATFORM for config '%s'" % name)
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   195
			
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   196
			if featurevariantname:
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   197
				variantplatform += featurevariantname
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   198
				
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   199
			if not varianttype:
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   200
				raise BadQuery("could not get VARIANTTYPE for config '%s'" % name)
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   201
			
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   202
			outputpath = str(generic_path.Join(releasepath, variantplatform, varianttype))
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   203
		
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   204
		return Config(fullname, outputpath)
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   205
		
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   206
	def getproducts(self):
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   207
		"""extract all product variants."""
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   208
		
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   209
		variants = []
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   210
		
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   211
		for v in self.__raptor.cache.variants.values():
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   212
			if v.type == "product":
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   213
				# copy the members we want to expose
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   214
				variants.append( Product(v.name) )
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   215
			
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   216
		return variants
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   217
	
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   218
class BadQuery(Exception):
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   219
	pass
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   220
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   221
# end of the raptor_api module