summaryrefslogtreecommitdiffstats
path: root/lib/bb/data_dict.py
blob: 8c6f3d836a2d797db8ae8f1c772907a059fbcf78 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# ex:ts=4:sw=4:sts=4:et
# -*- tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*-
"""
BitBake 'Data-Dict' implementation

Functions for interacting with the data structure used by the
BitBake build tools.

Copyright (C) 2003, 2004  Chris Larson
Copyright (C) 2005        Holger Hans Peter Freyther

This program is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation; either version 2 of the License, or (at your option) any later
version.

This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with
this program; if not, write to the Free Software Foundation, Inc., 59 Temple
Place, Suite 330, Boston, MA 02111-1307 USA. 

Based on functions from the base bb module, Copyright 2003 Holger Schurig
"""

import os, re, sys, types
from   bb import note, debug, fatal

__setvar_regexp__ = {}
__setvar_regexp__["_append"]  = re.compile('(?P<base>.*?)%s(_(?P<add>.*))?' % "_append")
__setvar_regexp__["_prepend"] = re.compile('(?P<base>.*?)%s(_(?P<add>.*))?' % "_prepend")
__setvar_regexp__["_delete"]  = re.compile('(?P<base>.*?)%s(_(?P<add>.*))?' % "_delete")

__expand_var_regexp__ = re.compile(r"\${[^{}]+}")
__expand_python_regexp__ = re.compile(r"\${@.+?}")


class DataDict:
    def __init__(self):
        self.dict = {}

    def expand(self,s, varname):
        def var_sub(match):
            key = match.group()[2:-1]
            if varname and key:
                if varname == key:
                    raise Exception("variable %s references itself!" % varname)
            var = self.getVar(key, 1)
            if var is not None:
                return var
            else:
                return match.group()

        def python_sub(match):
            import bb
            code = match.group()[3:-1]
            locals()['d'] = self
            s = eval(code)
            if type(s) == types.IntType: s = str(s)
            return s

        if type(s) is not types.StringType: # sanity check
            return s

        while s.find('$') != -1:
            olds = s
            try:
                s = __expand_var_regexp__.sub(var_sub, s)
                s = __expand_python_regexp__.sub(python_sub, s)
                if s == olds: break
                if type(s) is not types.StringType: # sanity check
                    import bb
                    bb.error('expansion of %s returned non-string %s' % (olds, s))
            except KeyboardInterrupt:
                raise
            except:
                note("%s:%s while evaluating:\n%s" % (sys.exc_info()[0], sys.exc_info()[1], s))
                raise
        return s

    def initVar(self, var):
        if not var in self.dict:
            self.dict[var] = {}

        if not "flags" in self.dict[var]:
            self.dict[var]["flags"] = {}

    def setVar(self,var,value):
        for v in ["_append", "_prepend", "_delete"]:
            match = __setvar_regexp__[v].match(var)

            if match:
                base = match.group('base')
                override = match.group('add')
                l = self.getVarFlag(base, v) or []
                if override == 'delete':
                    if l.count([value, None]):
                        del l[l.index([value, None])]
                l.append([value, override])
                self.setVarFlag(base, v, l)
                return

        self.initVar(var)
        if self.getVarFlag(var, 'matchesenv'):
            self.delVarFlag(var, 'matchesenv')
            self.setVarFlag(var, 'export', 1)
        self.dict[var]["content"] = value

    def getVar(self,var,exp):
        if not var in self.dict or not "content" in self.dict[var]:
            return None

        if exp:
            return self.expand(self.dict[var]["content"], var)
        return self.dict[var]["content"]

    def delVar(self,var):
        if var in self.dict:
            del self.dict[var]

    def setVarFlag(self,var,flag,flagvalue):
        self.initVar(var)
        self.dict[var]["flags"][flag] = flagvalue

    def getVarFlag(self,var,flag):
        if var in self.dict and "flags" in self.dict[var] and flag in self.dict[var]["flags"]:
            di = self.dict[var]
            di = di["flags"]
            return di[flag]
        return None

    def delVarFlag(self,var,flag):
        if var in self.dict and "flags" in self.dict[var] and flag in self.dict[var]["flags"]:
            del self.dict[var]["flags"][flag]

    def setVarFlags(self,var,flags):
        self.initVar(var)
        if flags == None:
            debug("Setting Null Flag %s" % var)

        self.dict[var]["flags"] = flags

    def getVarFlags(self,var):
        if var in self.dict and "flags" in self.dict[var]:
            return self.dict[var]["flags"]

        return None

    def delVarFlags(self,var):
        if var in self.dict and "flags" in self.dict[var]:
            del self.dict[var]["flags"]

    # Dictionary Methods
    def keys(self):
        return self.dict.keys()

    def iterkeys(self):
        return self.dict.iterkeys()

    def iteritems(self):
        return self.dict.iteritems()

    def items(self):
        return self.dict.items()

    def __getitem__(self,y):
        return self.dict.__getitem__(y)

    def __setitem__(self,x,y):
        self.dict.__setitem__(x,y)