data.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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' implementations
  5. Functions for interacting with the data structure used by the
  6. BitBake build tools.
  7. The expandData and update_data are the most expensive
  8. operations. At night the cookie monster came by and
  9. suggested 'give me cookies on setting the variables and
  10. things will work out'. Taking this suggestion into account
  11. applying the skills from the not yet passed 'Entwurf und
  12. Analyse von Algorithmen' lecture and the cookie
  13. monster seems to be right. We will track setVar more carefully
  14. to have faster update_data and expandKeys operations.
  15. This is a treade-off between speed and memory again but
  16. the speed is more critical here.
  17. """
  18. # Copyright (C) 2003, 2004 Chris Larson
  19. # Copyright (C) 2005 Holger Hans Peter Freyther
  20. #
  21. # This program is free software; you can redistribute it and/or modify
  22. # it under the terms of the GNU General Public License version 2 as
  23. # published by the Free Software Foundation.
  24. #
  25. # This program is distributed in the hope that it will be useful,
  26. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  27. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  28. # GNU General Public License for more details.
  29. #
  30. # You should have received a copy of the GNU General Public License along
  31. # with this program; if not, write to the Free Software Foundation, Inc.,
  32. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  33. #
  34. #Based on functions from the base bb module, Copyright 2003 Holger Schurig
  35. import sys, os, re
  36. if sys.argv[0][-5:] == "pydoc":
  37. path = os.path.dirname(os.path.dirname(sys.argv[1]))
  38. else:
  39. path = os.path.dirname(os.path.dirname(sys.argv[0]))
  40. sys.path.insert(0, path)
  41. from itertools import groupby
  42. from bb import data_smart
  43. import bb
  44. _dict_type = data_smart.DataSmart
  45. def init():
  46. """Return a new object representing the Bitbake data"""
  47. return _dict_type()
  48. def init_db(parent = None):
  49. """Return a new object representing the Bitbake data,
  50. optionally based on an existing object"""
  51. if parent:
  52. return parent.createCopy()
  53. else:
  54. return _dict_type()
  55. def createCopy(source):
  56. """Link the source set to the destination
  57. If one does not find the value in the destination set,
  58. search will go on to the source set to get the value.
  59. Value from source are copy-on-write. i.e. any try to
  60. modify one of them will end up putting the modified value
  61. in the destination set.
  62. """
  63. return source.createCopy()
  64. def initVar(var, d):
  65. """Non-destructive var init for data structure"""
  66. d.initVar(var)
  67. def setVar(var, value, d):
  68. """Set a variable to a given value"""
  69. d.setVar(var, value)
  70. def getVar(var, d, exp = 0):
  71. """Gets the value of a variable"""
  72. return d.getVar(var, exp)
  73. def renameVar(key, newkey, d):
  74. """Renames a variable from key to newkey"""
  75. d.renameVar(key, newkey)
  76. def delVar(var, d):
  77. """Removes a variable from the data set"""
  78. d.delVar(var)
  79. def setVarFlag(var, flag, flagvalue, d):
  80. """Set a flag for a given variable to a given value"""
  81. d.setVarFlag(var, flag, flagvalue)
  82. def getVarFlag(var, flag, d):
  83. """Gets given flag from given var"""
  84. return d.getVarFlag(var, flag)
  85. def delVarFlag(var, flag, d):
  86. """Removes a given flag from the variable's flags"""
  87. d.delVarFlag(var, flag)
  88. def setVarFlags(var, flags, d):
  89. """Set the flags for a given variable
  90. Note:
  91. setVarFlags will not clear previous
  92. flags. Think of this method as
  93. addVarFlags
  94. """
  95. d.setVarFlags(var, flags)
  96. def getVarFlags(var, d):
  97. """Gets a variable's flags"""
  98. return d.getVarFlags(var)
  99. def delVarFlags(var, d):
  100. """Removes a variable's flags"""
  101. d.delVarFlags(var)
  102. def keys(d):
  103. """Return a list of keys in d"""
  104. return d.keys()
  105. __expand_var_regexp__ = re.compile(r"\${[^{}]+}")
  106. __expand_python_regexp__ = re.compile(r"\${@.+?}")
  107. def expand(s, d, varname = None):
  108. """Variable expansion using the data store"""
  109. return d.expand(s, varname)
  110. def expandKeys(alterdata, readdata = None):
  111. if readdata == None:
  112. readdata = alterdata
  113. todolist = {}
  114. for key in keys(alterdata):
  115. if not '${' in key:
  116. continue
  117. ekey = expand(key, readdata)
  118. if key == ekey:
  119. continue
  120. todolist[key] = ekey
  121. # These two for loops are split for performance to maximise the
  122. # usefulness of the expand cache
  123. for key in todolist:
  124. ekey = todolist[key]
  125. renameVar(key, ekey, alterdata)
  126. def inheritFromOS(d):
  127. """Inherit variables from the environment."""
  128. for s in os.environ.keys():
  129. try:
  130. setVar(s, os.environ[s], d)
  131. setVarFlag(s, "export", True, d)
  132. except TypeError:
  133. pass
  134. def emit_var(var, o=sys.__stdout__, d = init(), all=False):
  135. """Emit a variable to be sourced by a shell."""
  136. if getVarFlag(var, "python", d):
  137. return 0
  138. export = getVarFlag(var, "export", d)
  139. unexport = getVarFlag(var, "unexport", d)
  140. func = getVarFlag(var, "func", d)
  141. if not all and not export and not unexport and not func:
  142. return 0
  143. try:
  144. if all:
  145. oval = getVar(var, d, 0)
  146. val = getVar(var, d, 1)
  147. except (KeyboardInterrupt, bb.build.FuncFailed):
  148. raise
  149. except Exception, exc:
  150. o.write('# expansion of %s threw %s: %s\n' % (var, exc.__class__.__name__, str(exc)))
  151. return 0
  152. if all:
  153. o.write('# %s=%s\n' % (var, oval))
  154. if (var.find("-") != -1 or var.find(".") != -1 or var.find('{') != -1 or var.find('}') != -1 or var.find('+') != -1) and not all:
  155. return 0
  156. varExpanded = expand(var, d)
  157. if unexport:
  158. o.write('unset %s\n' % varExpanded)
  159. return 1
  160. if not val:
  161. return 0
  162. val = str(val)
  163. if func:
  164. # NOTE: should probably check for unbalanced {} within the var
  165. o.write("%s() {\n%s\n}\n" % (varExpanded, val))
  166. return 1
  167. if export:
  168. o.write('export ')
  169. # if we're going to output this within doublequotes,
  170. # to a shell, we need to escape the quotes in the var
  171. alter = re.sub('"', '\\"', val.strip())
  172. o.write('%s="%s"\n' % (varExpanded, alter))
  173. return 1
  174. def emit_env(o=sys.__stdout__, d = init(), all=False):
  175. """Emits all items in the data store in a format such that it can be sourced by a shell."""
  176. isfunc = lambda key: bool(d.getVarFlag(key, "func"))
  177. keys = sorted((key for key in d.keys() if not key.startswith("__")), key=isfunc)
  178. grouped = groupby(keys, isfunc)
  179. for isfunc, keys in grouped:
  180. for key in keys:
  181. emit_var(key, o, d, all and not isfunc) and o.write('\n')
  182. def update_data(d):
  183. """Performs final steps upon the datastore, including application of overrides"""
  184. d.finalize()
  185. def inherits_class(klass, d):
  186. val = getVar('__inherit_cache', d) or []
  187. if os.path.join('classes', '%s.bbclass' % klass) in val:
  188. return True
  189. return False