1 # ex:ts=4:sw=4:sts=4:et
2 # -*- tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*-
4 BitBake 'Data-Dict' implementation
6 Functions for interacting with the data structure used by the
9 Copyright (C) 2003, 2004 Chris Larson
10 Copyright (C) 2005 Holger Hans Peter Freyther
12 This program is free software; you can redistribute it and/or modify it under
13 the terms of the GNU General Public License as published by the Free Software
14 Foundation; either version 2 of the License, or (at your option) any later
17 This program is distributed in the hope that it will be useful, but WITHOUT
18 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License along with
22 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
23 Place, Suite 330, Boston, MA 02111-1307 USA.
25 Based on functions from the base bb module, Copyright 2003 Holger Schurig
28 import os, re, sys, types, copy
29 from bb import note, debug, fatal
31 __setvar_regexp__ = {}
32 __setvar_regexp__["_append"] = re.compile('(?P<base>.*?)%s(_(?P<add>.*))?' % "_append")
33 __setvar_regexp__["_prepend"] = re.compile('(?P<base>.*?)%s(_(?P<add>.*))?' % "_prepend")
34 __setvar_regexp__["_delete"] = re.compile('(?P<base>.*?)%s(_(?P<add>.*))?' % "_delete")
36 __expand_var_regexp__ = re.compile(r"\${[^{}]+}")
37 __expand_python_regexp__ = re.compile(r"\${@.+?}")
44 def expand(self,s, varname):
46 key = match.group()[2:-1]
49 raise Exception("variable %s references itself!" % varname)
50 var = self.getVar(key, 1)
56 def python_sub(match):
58 code = match.group()[3:-1]
61 if type(s) == types.IntType: s = str(s)
64 if type(s) is not types.StringType: # sanity check
67 while s.find('$') != -1:
70 s = __expand_var_regexp__.sub(var_sub, s)
71 s = __expand_python_regexp__.sub(python_sub, s)
73 if type(s) is not types.StringType: # sanity check
75 bb.error('expansion of %s returned non-string %s' % (olds, s))
76 except KeyboardInterrupt:
79 note("%s:%s while evaluating:\n%s" % (sys.exc_info()[0], sys.exc_info()[1], s))
83 def initVar(self, var):
84 if not var in self.dict:
87 if not "flags" in self.dict[var]:
88 self.dict[var]["flags"] = {}
90 def setVar(self,var,value):
91 for v in ["_append", "_prepend", "_delete"]:
92 match = __setvar_regexp__[v].match(var)
95 base = match.group('base')
96 override = match.group('add')
97 l = self.getVarFlag(base, v) or []
98 if override == 'delete':
99 if l.count([value, None]):
100 del l[l.index([value, None])]
101 l.append([value, override])
102 self.setVarFlag(base, v, l)
106 if self.getVarFlag(var, 'matchesenv'):
107 self.delVarFlag(var, 'matchesenv')
108 self.setVarFlag(var, 'export', 1)
109 self.dict[var]["content"] = value
111 def getVar(self,var,exp):
112 if not var in self.dict or not "content" in self.dict[var]:
116 return self.expand(self.dict[var]["content"], var)
117 return self.dict[var]["content"]
119 def delVar(self,var):
123 def setVarFlag(self,var,flag,flagvalue):
125 self.dict[var]["flags"][flag] = flagvalue
127 def getVarFlag(self,var,flag):
128 if var in self.dict and "flags" in self.dict[var] and flag in self.dict[var]["flags"]:
134 def delVarFlag(self,var,flag):
135 if var in self.dict and "flags" in self.dict[var] and flag in self.dict[var]["flags"]:
136 del self.dict[var]["flags"][flag]
138 def setVarFlags(self,var,flags):
141 debug("Setting Null Flag %s" % var)
143 self.dict[var]["flags"] = flags
145 def getVarFlags(self,var):
146 if var in self.dict and "flags" in self.dict[var]:
147 return self.dict[var]["flags"]
151 def delVarFlags(self,var):
152 if var in self.dict and "flags" in self.dict[var]:
153 del self.dict[var]["flags"]
155 def createCopy(self):
156 return copy.deepcopy(self)
160 return self.dict.keys()
163 return self.dict.iterkeys()
166 return self.dict.iteritems()
169 return self.dict.items()
171 def __getitem__(self,y):
172 return self.dict.__getitem__(y)
174 def __setitem__(self,x,y):
175 self.dict.__setitem__(x,y)