Package conary :: Package build :: Module use
[hide private]
[frames] | no frames]

Source Code for Module conary.build.use

  1  # 
  2  # Copyright (c) 2004-2008 rPath, Inc. 
  3  # 
  4  # This program is distributed under the terms of the Common Public License, 
  5  # version 1.0. A copy of this license should have been distributed with this 
  6  # source file in a file called LICENSE. If it is not present, the license 
  7  # is always available at http://www.rpath.com/permanent/licenses/CPL-1.0. 
  8  # 
  9  # This program is distributed in the hope that it will be useful, but 
 10  # without any warranty; without even the implied warranty of merchantability 
 11  # or fitness for a particular purpose. See the Common Public License for 
 12  # full details. 
 13  # 
 14  # Class hierarchy: 
 15  # 
 16  # Flag 
 17  #  | 
 18  #  +--UseFlag 
 19  #  | 
 20  #  +--SubArchFlag 
 21  #  | 
 22  #  +--LocalFlag 
 23   
 24  # Collection 
 25  #  |  
 26  #  +--UseCollection           
 27  #  |  
 28  #  +--ArchCollection 
 29  #  |  
 30  #  +--LocalFlagCollection 
 31  # 
 32  # MajorArch derives from CollectionWithFlag, which is a subclass of Flag and  
 33  # Collection. (maybe CollectionWithFlag and MajorArch should be collapsed?) 
 34   
 35  """ 
 36  Provides the build configuration as special dictionaries that directly 
 37  export their namespaces. 
 38  """ 
 39   
 40  import itertools 
 41   
 42  #conary 
 43  from conary.deps import arch, deps 
 44  from conary.lib import log 
 45  from conary.errors import CvcError 
 46   
47 -class Flag(dict):
48
49 - def __init__(self, name, parent=None, value=False, 50 required=True, track=False, path=None, platform=False):
51 self._name = name 52 self._value = value 53 self._parent = parent 54 self._required = required 55 self._tracking = track 56 self._used = False 57 self._alias = None 58 self._path = path 59 self._platform = platform
60
61 - def __repr__(self):
62 if self._alias: 63 return "%s (alias %s): %s" % (self._name, self._alias, self._value) 64 else: 65 return "%s: %s" % (self._name, self._value)
66
67 - def __str__(self):
68 if self._alias: 69 return "%s (alias %s): %s" % (self._fullName(), self._alias, 70 self._value) 71 else: 72 return "%s: %s" % (self._fullName(), self._value)
73
74 - def setShortDoc(self, doc):
75 # XXX we don't do anything with this documentation currently. 76 self._shortDoc = doc
77
78 - def setRequired(self, value=True):
79 self._required = value
80
81 - def setPlatform(self, value=True):
82 self._platform = value
83
84 - def isPlatformFlag(self):
85 return self._platform
86
87 - def _set(self, value=True):
88 self._value = value
89
90 - def _get(self):
91 """ Grab value without tracking """ 92 return self._value
93
94 - def _fullName(self):
95 return ('.'.join(x._name for x in self._reverseParents()) 96 + '.' + self._name)
97
98 - def _reverseParents(self):
99 if self._parent is not None: 100 for parent in self._parent._reverseParents(): 101 yield parent 102 yield self._parent
103
104 - def _getDepSense(self):
105 if self._get(): 106 if self._required: 107 return deps.FLAG_SENSE_REQUIRED 108 else: 109 return deps.FLAG_SENSE_PREFERRED 110 else: 111 return deps.FLAG_SENSE_PREFERNOT
112
113 - def _toDependency(self):
114 """ Returns an actual Dependency Set consisting of only this flag """ 115 raise NotImplementedError
116
117 - def _resetUsed(self):
118 self._used = False
119
120 - def _trackUsed(self, value):
121 self._tracking = value
122 123 124 # --- boolean operations on Flags --- 125
126 - def __nonzero__(self):
127 if self._tracking: 128 self._setUsed(True) 129 return self._value
130
131 - def _setUsed(self, used=True):
132 self._used = used
133
134 - def __eq__(self, other):
135 if not isinstance(other, (Flag, bool)): 136 return False 137 return bool(self) == bool(other)
138
139 - def __ne__(self, other):
140 return not self.__eq__(other)
141
142 - def __ror__(self, other):
143 return bool(self) | other
144
145 - def __or__(self, other):
146 return self.__ror__(other)
147
148 - def __rand__(self, other):
149 return bool(self) & other
150
151 - def __and__(self, other):
152 return self.__rand__(other)
153 154 155
156 -class Collection(dict):
157
158 - def __init__(self, name, parent=None, track=False):
159 self._name = name 160 self._parent = parent 161 self._strictMode = True 162 self._tracking = track 163 self._attrs = {}
164 165
166 - def _addAlias(self, realKey, alias):
167 """ Add a second way to access the given item. 168 Necessary if the actual name for a flag is not a valid 169 python identifier. 170 """ 171 if alias in self or alias in self._attrs: 172 raise RuntimeError, 'alias is already set' 173 elif self[realKey]._alias: 174 raise RuntimeError, 'key %s already has an alias' % realKey 175 else: 176 self._setAttr(alias, self[realKey]) 177 self[realKey]._alias = alias
178
179 - def _setAttr(self, name, value):
180 """ A generic way to add a temporary attribute to this collection. 181 Attributes stored in this manner will be removed when the 182 collection is cleared, but are not tracked like flags. 183 """ 184 self._attrs[name] = value
185
186 - def _delAttr(self, name):
187 del self._attrs[name]
188
189 - def _getAttr(self, name):
190 return self._attrs[name]
191
192 - def _addFlag(self, key, *args, **kw):
193 if 'track' not in kw: 194 kw = kw.copy() 195 kw['track'] = self._tracking 196 dict.__setitem__(self, key, self._collectionType(key, self, 197 *args, **kw))
198
199 - def __repr__(self):
200 return "%s: {%s}" % (self._name, 201 ', '.join((repr(x) for x in self.values())))
202
203 - def __nonzero__(self):
204 raise RuntimeError( 205 'Cannot compare collection as True/False')
206 207
208 - def _clear(self):
209 for flag in self.keys(): 210 del self[flag] 211 for attr in self._attrs.keys(): 212 del self._attrs[attr]
213
214 - def __getattr__(self, key):
215 if key in self.__dict__: 216 return self.__dict__[key] 217 if key in self: 218 return self[key] 219 if key in self._attrs: 220 return self._getAttr(key) 221 if key[0] == '_': 222 raise AttributeError, key 223 return self._getNonExistantKey(key)
224
225 - def __getitem__(self, key):
226 if key in self._attrs: 227 return self._attrs[key] 228 else: 229 return dict.__getitem__(self, key)
230
231 - def __setattr__(self, key, value):
232 if key[0] == '_': 233 self.__dict__[key] = value 234 else: 235 raise RuntimeError, "Cannot set value of flags: %s" % key
236
237 - def _getNonExistantKey(self, key):
238 """ Method that is called when a nonexistant key is accessed. 239 Overridden by subclasses to allow for useful error messages 240 or default key values to be supplied """ 241 raise AttributeError, key
242
243 - def _iterAll(self):
244 for child in self.itervalues(): 245 if isinstance(child, Collection): 246 for flag in child._iterAll(): 247 yield flag 248 else: 249 yield child
250
251 - def _setStrictMode(self, value=True):
252 """ Strict mode determines whether you receive an error or 253 an empty flag upon accessing a nonexistant flag 254 """ 255 self._strictMode = value
256
257 - def _reverseParents(self):
258 """ Traverse through the parents from the topmost parent down. """ 259 if self._parent is not None: 260 for parent in self._parent._reverseParents(): 261 yield parent 262 yield self._parent
263 264 # -- Tracking Commands -- 265
266 - def _trackUsed(self, value=True):
267 self._tracking = value 268 for child in self.itervalues(): 269 child._trackUsed(value)
270
271 - def _resetUsed(self):
272 for child in self.itervalues(): 273 child._resetUsed()
274
275 - def _getUsed(self):
276 return [ x for x in self._iterUsed() ]
277
278 - def _iterUsed(self):
279 for child in self.itervalues(): 280 if isinstance(child, Collection): 281 for flag in child._iterUsed(): 282 yield flag 283 else: 284 if child._used: 285 yield child
286 287
288 -class CollectionWithFlag(Flag, Collection):
289 """ CollectionWithFlag. Currently only has one child class, MajorArch. """
290 - def __init__(self, name, parent, track=False):
291 Flag.