22dfdf2fec682ef4b76b964959bff5072aeb20b9
[vuplus_bitbake] / lib / bb / data_dict.py
1 # ex:ts=4:sw=4:sts=4:et
2 # -*- tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*-
3 """
4 BitBake 'Data-Dict' implementation
5
6 Functions for interacting with the data structure used by the
7 BitBake build tools.
8
9 Copyright (C) 2003, 2004  Chris Larson
10 Copyright (C) 2005        Holger Hans Peter Freyther
11
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
15 version.
16
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.
20
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. 
24
25 Based on functions from the base bb module, Copyright 2003 Holger Schurig
26 """
27
28 import os, re, sys, types, copy
29 from   bb import note, debug, fatal
30
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")
35
36 __expand_var_regexp__ = re.compile(r"\${[^{}]+}")
37 __expand_python_regexp__ = re.compile(r"\${@.+?}")
38
39
40 class DataDict:
41     def __init__(self):
42         self.dict = {}
43
44     def expand(self,s, varname):
45         def var_sub(match):
46             key = match.group()[2:-1]
47             if varname and key:
48                 if varname == key:
49                     raise Exception("variable %s references itself!" % varname)
50             var = self.getVar(key, 1)
51             if var is not None:
52                 return var
53             else:
54                 return match.group()
55
56         def python_sub(match):
57             import bb
58             code = match.group()[3:-1]
59             locals()['d'] = self
60             s = eval(code)
61             if type(s) == types.IntType: s = str(s)
62             return s
63
64         if type(s) is not types.StringType: # sanity check
65             return s
66
67         while s.find('$') != -1:
68             olds = s
69             try:
70                 s = __expand_var_regexp__.sub(var_sub, s)
71                 s = __expand_python_regexp__.sub(python_sub, s)
72                 if s == olds: break
73                 if type(s) is not types.StringType: # sanity check
74                     import bb
75                     bb.error('expansion of %s returned non-string %s' % (olds, s))
76             except KeyboardInterrupt:
77                 raise
78             except:
79                 note("%s:%s while evaluating:\n%s" % (sys.exc_info()[0], sys.exc_info()[1], s))
80                 raise
81         return s
82
83     def initVar(self, var):
84         if not var in self.dict:
85             self.dict[var] = {}
86
87         if not "flags" in self.dict[var]:
88             self.dict[var]["flags"] = {}
89
90     def setVar(self,var,value):
91         for v in ["_append", "_prepend", "_delete"]:
92             match = __setvar_regexp__[v].match(var)
93
94             if match:
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)
103                 return
104
105         self.initVar(var)
106         if self.getVarFlag(var, 'matchesenv'):
107             self.delVarFlag(var, 'matchesenv')
108             self.setVarFlag(var, 'export', 1)
109         self.dict[var]["content"] = value
110
111     def getVar(self,var,exp):
112         if not var in self.dict or not "content" in self.dict[var]:
113             return None
114
115         if exp:
116             return self.expand(self.dict[var]["content"], var)
117         return self.dict[var]["content"]
118
119     def delVar(self,var):
120         if var in self.dict:
121             del self.dict[var]
122
123     def setVarFlag(self,var,flag,flagvalue):
124         self.initVar(var)
125         self.dict[var]["flags"][flag] = flagvalue
126
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"]:
129             di = self.dict[var]
130             di = di["flags"]
131             return di[flag]
132         return None
133
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]
137
138     def setVarFlags(self,var,flags):
139         self.initVar(var)
140         if flags == None:
141             debug("Setting Null Flag %s" % var)
142
143         self.dict[var]["flags"] = flags
144
145     def getVarFlags(self,var):
146         if var in self.dict and "flags" in self.dict[var]:
147             return self.dict[var]["flags"]
148
149         return None
150
151     def delVarFlags(self,var):
152         if var in self.dict and "flags" in self.dict[var]:
153             del self.dict[var]["flags"]
154
155     def createCopy(self):
156         return copy.deepcopy(self)
157
158     # Dictionary Methods
159     def keys(self):
160         return self.dict.keys()
161
162     def iterkeys(self):
163         return self.dict.iterkeys()
164
165     def iteritems(self):
166         return self.dict.iteritems()
167
168     def items(self):
169         return self.dict.items()
170
171     def __getitem__(self,y):
172         return self.dict.__getitem__(y)
173
174     def __setitem__(self,x,y):
175         self.dict.__setitem__(x,y)
176