sbsv2/raptor/python/raptor_api.py
branchwip
changeset 504 ea75c3a64a17
parent 503 e182c2a8794d
child 507 54a88b895bcd
equal deleted inserted replaced
503:e182c2a8794d 504:ea75c3a64a17
    58 			string += "/>\n"
    58 			string += "/>\n"
    59 		
    59 		
    60 		return string
    60 		return string
    61 
    61 
    62 class Alias(Reply):
    62 class Alias(Reply):
    63 	pass
    63 	def __init__(self, name, meaning):
       
    64 		super(Alias,self).__init__()
       
    65 		self.name = name
       
    66 		self.meaning = meaning
    64 
    67 
    65 class Config(Reply):
    68 class Config(Reply):
    66 	pass
    69 	def __init__(self, fullname, outputpath):
       
    70 		super(Config,self).__init__()
       
    71 		self.fullname = fullname
       
    72 		self.outputpath = outputpath
    67 
    73 
    68 class Product(Reply):
    74 class Product(Reply):
    69 	pass
    75 	def __init__(self, name):
       
    76 		super(Product,self).__init__()
       
    77 		self.name = name
    70 
    78 
    71 import generic_path
    79 import generic_path
    72 import raptor
    80 import raptor
    73 import raptor_data
    81 import raptor_data
    74 import re
    82 import re
    77 	"""object to contain state information for API calls.
    85 	"""object to contain state information for API calls.
    78 	
    86 	
    79 	For example,
    87 	For example,
    80 	
    88 	
    81 	api = raptor_api.Context()
    89 	api = raptor_api.Context()
    82 	val = api.get(X)
    90 	val = api.getaliases("X")
    83 	"""
    91 	"""
    84 	def __init__(self, initialiser=None):
    92 	def __init__(self, initialiser=None):
    85 		# this object has a private Raptor object that can either be
    93 		# this object has a private Raptor object that can either be
    86 		# passed in or created internally.
    94 		# passed in or created internally.
    87 		
    95 		
    88 		if initialiser == None:
    96 		if initialiser == None:
    89 			self.__raptor = raptor.Raptor()
    97 			self.__raptor = raptor.Raptor()
    90 		else:
    98 		else:
    91 			self.__raptor = initialiser
    99 			self.__raptor = initialiser
    92 			
   100 			
    93 	def StringQuery(self, query):
   101 	def stringquery(self, query):
    94 		"""turn a string into an API call and execute it.
   102 		"""turn a string into an API call and execute it.
    95 		
   103 		
    96 		This is a convenience method for "lazy" callers.
   104 		This is a convenience method for "lazy" callers.
    97 		
   105 		
    98 		The return value is also converted into a string.
   106 		The return value is also converted into a string.
    99 		"""
   107 		"""
   100 		
   108 		
   101 		if query == "aliases":
   109 		if query == "aliases":
   102 			aliases = self.GetAliases()
   110 			aliases = self.getaliases()
   103 			return "".join(map(str, aliases)).strip()
   111 			return "".join(map(str, aliases)).strip()
   104 		
   112 		
   105 		elif query == "products":
   113 		elif query == "products":
   106 			variants = self.GetProducts()
   114 			variants = self.getproducts()
   107 			return "".join(map(str, variants)).strip()
   115 			return "".join(map(str, variants)).strip()
   108 		
   116 		
   109 		elif query.startswith("config"):
   117 		elif query.startswith("config"):
   110 			match = re.match("config\[(.*)\]", query)
   118 			match = re.match("config\[(.*)\]", query)
   111 			if match:
   119 			if match:
   112 				config = self.GetConfig(match.group(1))
   120 				config = self.getconfig(match.group(1))
   113 				return str(config).strip()
   121 				return str(config).strip()
   114 			else:
   122 			else:
   115 				raise BadQuery("syntax error")
   123 				raise BadQuery("syntax error")
   116 		
   124 		
   117 		raise BadQuery("unknown query")
   125 		raise BadQuery("unknown query")
   118 
   126 
   119 	def GetAliases(self, type=""):
   127 	def getaliases(self, type=""):
   120 		"""extract all aliases of a given type.
   128 		"""extract all aliases of a given type.
   121 		
   129 		
   122 		the default type is "".
   130 		the default type is "".
   123 		to get all aliases pass type=None
   131 		to get all aliases pass type=None
   124 		"""
   132 		"""
   125 		aliases = []
   133 		aliases = []
   126 		
   134 		
   127 		for a in self.__raptor.cache.aliases.values():
   135 		for a in self.__raptor.cache.aliases.values():
   128 			if a.type == type or type == None:
   136 			if a.type == type or type == None:
   129 				r = Alias()
       
   130 				# copy the members we want to expose
   137 				# copy the members we want to expose
   131 				r.name = a.name
   138 				aliases.append( Alias(a.name, a.meaning) )
   132 				r.meaning = a.meaning
       
   133 				aliases.append(r)
       
   134 			
   139 			
   135 		return aliases
   140 		return aliases
   136 	
   141 	
   137 	def GetConfig(self, name):
   142 	def getconfig(self, name):
   138 		"""extract the values for a given configuration.
   143 		"""extract the values for a given configuration.
   139 		
   144 		
   140 		'name' should be an alias or variant followed optionally by a
   145 		'name' should be an alias or variant followed optionally by a
   141 		dot-separated list of variants. For example "armv5_urel" or
   146 		dot-separated list of variants. For example "armv5_urel" or
   142 		"armv5_urel.savespace.vasco".
   147 		"armv5_urel.savespace.vasco".
   189 			if not varianttype:
   194 			if not varianttype:
   190 				raise BadQuery("could not get VARIANTTYPE for config '%s'" % name)
   195 				raise BadQuery("could not get VARIANTTYPE for config '%s'" % name)
   191 			
   196 			
   192 			outputpath = str(generic_path.Join(releasepath, variantplatform, varianttype))
   197 			outputpath = str(generic_path.Join(releasepath, variantplatform, varianttype))
   193 		
   198 		
   194 		r = Config()
   199 		return Config(fullname, outputpath)
   195 		r.fullname = fullname
   200 		
   196 		r.outputpath = outputpath
   201 	def getproducts(self):
   197 		return r
       
   198 		
       
   199 	def GetProducts(self):
       
   200 		"""extract all product variants."""
   202 		"""extract all product variants."""
   201 		
   203 		
   202 		variants = []
   204 		variants = []
   203 		
   205 		
   204 		for v in self.__raptor.cache.variants.values():
   206 		for v in self.__raptor.cache.variants.values():
   205 			if v.type == "product":
   207 			if v.type == "product":
   206 				r = Product()
       
   207 				# copy the members we want to expose
   208 				# copy the members we want to expose
   208 				r.name = v.name
   209 				variants.append( Product(v.name) )
   209 				variants.append(r)
       
   210 			
   210 			
   211 		return variants
   211 		return variants
   212 	
   212 	
   213 class BadQuery(Exception):
   213 class BadQuery(Exception):
   214 	pass
   214 	pass