PageRenderTime 43ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/zepto-v2/bitbake-1.17.0/lib/bb/data.py

https://gitlab.com/vipemb/yocto-study
Python | 362 lines | 359 code | 0 blank | 3 comment | 0 complexity | 7e960d21dbf82dc1c54dde1df72f00b0 MD5 | raw file
  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. from bb import codeparser
  44. import bb
  45. logger = data_smart.logger
  46. _dict_type = data_smart.DataSmart
  47. def init():
  48. """Return a new object representing the Bitbake data"""
  49. return _dict_type()
  50. def init_db(parent = None):
  51. """Return a new object representing the Bitbake data,
  52. optionally based on an existing object"""
  53. if parent:
  54. return parent.createCopy()
  55. else:
  56. return _dict_type()
  57. def createCopy(source):
  58. """Link the source set to the destination
  59. If one does not find the value in the destination set,
  60. search will go on to the source set to get the value.
  61. Value from source are copy-on-write. i.e. any try to
  62. modify one of them will end up putting the modified value
  63. in the destination set.
  64. """
  65. return source.createCopy()
  66. def initVar(var, d):
  67. """Non-destructive var init for data structure"""
  68. d.initVar(var)
  69. def setVar(var, value, d):
  70. """Set a variable to a given value"""
  71. d.setVar(var, value)
  72. def getVar(var, d, exp = 0):
  73. """Gets the value of a variable"""
  74. return d.getVar(var, exp)
  75. def renameVar(key, newkey, d):
  76. """Renames a variable from key to newkey"""
  77. d.renameVar(key, newkey)
  78. def delVar(var, d):
  79. """Removes a variable from the data set"""
  80. d.delVar(var)
  81. def setVarFlag(var, flag, flagvalue, d):
  82. """Set a flag for a given variable to a given value"""
  83. d.setVarFlag(var, flag, flagvalue)
  84. def getVarFlag(var, flag, d):
  85. """Gets given flag from given var"""
  86. return d.getVarFlag(var, flag)
  87. def delVarFlag(var, flag, d):
  88. """Removes a given flag from the variable's flags"""
  89. d.delVarFlag(var, flag)
  90. def setVarFlags(var, flags, d):
  91. """Set the flags for a given variable
  92. Note:
  93. setVarFlags will not clear previous
  94. flags. Think of this method as
  95. addVarFlags
  96. """
  97. d.setVarFlags(var, flags)
  98. def getVarFlags(var, d):
  99. """Gets a variable's flags"""
  100. return d.getVarFlags(var)
  101. def delVarFlags(var, d):
  102. """Removes a variable's flags"""
  103. d.delVarFlags(var)
  104. def keys(d):
  105. """Return a list of keys in d"""
  106. return d.keys()
  107. __expand_var_regexp__ = re.compile(r"\${[^{}]+}")
  108. __expand_python_regexp__ = re.compile(r"\${@.+?}")
  109. def expand(s, d, varname = None):
  110. """Variable expansion using the data store"""
  111. return d.expand(s, varname)
  112. def expandKeys(alterdata, readdata = None):
  113. if readdata == None:
  114. readdata = alterdata
  115. todolist = {}
  116. for key in keys(alterdata):
  117. if not '${' in key:
  118. continue
  119. ekey = expand(key, readdata)
  120. if key == ekey:
  121. continue
  122. todolist[key] = ekey
  123. # These two for loops are split for performance to maximise the
  124. # usefulness of the expand cache
  125. for key in todolist:
  126. ekey = todolist[key]
  127. renameVar(key, ekey, alterdata)
  128. def inheritFromOS(d, savedenv, permitted):
  129. """Inherit variables from the initial environment."""
  130. exportlist = bb.utils.preserved_envvars_exported()
  131. for s in savedenv.keys():
  132. if s in permitted:
  133. try:
  134. setVar(s, getVar(s, savedenv, True), d)
  135. if s in exportlist:
  136. setVarFlag(s, "export", True, d)
  137. except TypeError:
  138. pass
  139. def emit_var(var, o=sys.__stdout__, d = init(), all=False):
  140. """Emit a variable to be sourced by a shell."""
  141. if getVarFlag(var, "python", d):
  142. return 0
  143. export = getVarFlag(var, "export", d)
  144. unexport = getVarFlag(var, "unexport", d)
  145. func = getVarFlag(var, "func", d)
  146. if not all and not export and not unexport and not func:
  147. return 0
  148. try:
  149. if all:
  150. oval = getVar(var, d, 0)
  151. val = getVar(var, d, 1)
  152. except (KeyboardInterrupt, bb.build.FuncFailed):
  153. raise
  154. except Exception as exc:
  155. o.write('# expansion of %s threw %s: %s\n' % (var, exc.__class__.__name__, str(exc)))
  156. return 0
  157. if all:
  158. commentVal = re.sub('\n', '\n#', str(oval))
  159. o.write('# %s=%s\n' % (var, commentVal))
  160. if (var.find("-") != -1 or var.find(".") != -1 or var.find('{') != -1 or var.find('}') != -1 or var.find('+') != -1) and not all:
  161. return 0
  162. varExpanded = expand(var, d)
  163. if unexport:
  164. o.write('unset %s\n' % varExpanded)
  165. return 0
  166. if not val:
  167. return 0
  168. val = str(val)
  169. if func:
  170. # NOTE: should probably check for unbalanced {} within the var
  171. o.write("%s() {\n%s\n}\n" % (varExpanded, val))
  172. return 1
  173. if export:
  174. o.write('export ')
  175. # if we're going to output this within doublequotes,
  176. # to a shell, we need to escape the quotes in the var
  177. alter = re.sub('"', '\\"', val.strip())
  178. alter = re.sub('\n', ' \\\n', alter)
  179. o.write('%s="%s"\n' % (varExpanded, alter))
  180. return 0
  181. def emit_env(o=sys.__stdout__, d = init(), all=False):
  182. """Emits all items in the data store in a format such that it can be sourced by a shell."""
  183. isfunc = lambda key: bool(d.getVarFlag(key, "func"))
  184. keys = sorted((key for key in d.keys() if not key.startswith("__")), key=isfunc)
  185. grouped = groupby(keys, isfunc)
  186. for isfunc, keys in grouped:
  187. for key in keys:
  188. emit_var(key, o, d, all and not isfunc) and o.write('\n')
  189. def exported_keys(d):
  190. return (key for key in d.keys() if not key.startswith('__') and
  191. d.getVarFlag(key, 'export') and
  192. not d.getVarFlag(key, 'unexport'))
  193. def exported_vars(d):
  194. for key in exported_keys(d):
  195. try:
  196. value = d.getVar(key, True)
  197. except Exception:
  198. pass
  199. if value is not None:
  200. yield key, str(value)
  201. def emit_func(func, o=sys.__stdout__, d = init()):
  202. """Emits all items in the data store in a format such that it can be sourced by a shell."""
  203. keys = (key for key in d.keys() if not key.startswith("__") and not d.getVarFlag(key, "func"))
  204. for key in keys:
  205. emit_var(key, o, d, False) and o.write('\n')
  206. emit_var(func, o, d, False) and o.write('\n')
  207. newdeps = bb.codeparser.ShellParser(func, logger).parse_shell(d.getVar(func, True))
  208. seen = set()
  209. while newdeps:
  210. deps = newdeps
  211. seen |= deps
  212. newdeps = set()
  213. for dep in deps:
  214. if d.getVarFlag(dep, "func"):
  215. emit_var(dep, o, d, False) and o.write('\n')
  216. newdeps |= bb.codeparser.ShellParser(dep, logger).parse_shell(d.getVar(dep, True))
  217. newdeps -= seen
  218. def update_data(d):
  219. """Performs final steps upon the datastore, including application of overrides"""
  220. d.finalize()
  221. def build_dependencies(key, keys, shelldeps, vardepvals, d):
  222. deps = set()
  223. vardeps = d.getVarFlag(key, "vardeps", True)
  224. try:
  225. if key[-1] == ']':
  226. vf = key[:-1].split('[')
  227. value = d.getVarFlag(vf[0], vf[1], False)
  228. else:
  229. value = d.getVar(key, False)
  230. if key in vardepvals:
  231. value = d.getVarFlag(key, "vardepvalue", True)
  232. elif d.getVarFlag(key, "func"):
  233. if d.getVarFlag(key, "python"):
  234. parsedvar = d.expandWithRefs(value, key)
  235. parser = bb.codeparser.PythonParser(key, logger)
  236. if parsedvar.value and "\t" in parsedvar.value:
  237. logger.warn("Variable %s contains tabs, please remove these (%s)" % (key, d.getVar("FILE", True)))
  238. parser.parse_python(parsedvar.value)
  239. deps = deps | parser.references
  240. else:
  241. parsedvar = d.expandWithRefs(value, key)
  242. parser = bb.codeparser.ShellParser(key, logger)
  243. parser.parse_shell(parsedvar.value)
  244. deps = deps | shelldeps
  245. if vardeps is None:
  246. parser.log.flush()
  247. deps = deps | parsedvar.references
  248. deps = deps | (keys & parser.execs) | (keys & parsedvar.execs)
  249. else:
  250. parser = d.expandWithRefs(value, key)
  251. deps |= parser.references
  252. deps = deps | (keys & parser.execs)
  253. # Add varflags, assuming an exclusion list is set
  254. varflagsexcl = d.getVar('BB_SIGNATURE_EXCLUDE_FLAGS', True)
  255. if varflagsexcl:
  256. varfdeps = []
  257. varflags = d.getVarFlags(key)
  258. if varflags:
  259. for f in varflags:
  260. if f not in varflagsexcl:
  261. varfdeps.append('%s[%s]' % (key, f))
  262. if varfdeps:
  263. deps |= set(varfdeps)
  264. deps |= set((vardeps or "").split())
  265. deps -= set((d.getVarFlag(key, "vardepsexclude", True) or "").split())
  266. except Exception as e:
  267. raise bb.data_smart.ExpansionError(key, None, e)
  268. return deps, value
  269. #bb.note("Variable %s references %s and calls %s" % (key, str(deps), str(execs)))
  270. #d.setVarFlag(key, "vardeps", deps)
  271. def generate_dependencies(d):
  272. keys = set(key for key in d.keys() if not key.startswith("__"))
  273. shelldeps = set(key for key in keys if d.getVarFlag(key, "export") and not d.getVarFlag(key, "unexport"))
  274. vardepvals = set(key for key in keys if d.getVarFlag(key, "vardepvalue"))
  275. deps = {}
  276. values = {}
  277. tasklist = d.getVar('__BBTASKS') or []
  278. for task in tasklist:
  279. deps[task], values[task] = build_dependencies(task, keys, shelldeps, vardepvals, d)
  280. newdeps = deps[task]
  281. seen = set()
  282. while newdeps:
  283. nextdeps = newdeps
  284. seen |= nextdeps
  285. newdeps = set()
  286. for dep in nextdeps:
  287. if dep not in deps:
  288. deps[dep], values[dep] = build_dependencies(dep, keys, shelldeps, vardepvals, d)
  289. newdeps |= deps[dep]
  290. newdeps -= seen
  291. #print "For %s: %s" % (task, str(deps[task]))
  292. return tasklist, deps, values
  293. def inherits_class(klass, d):
  294. val = getVar('__inherit_cache', d) or []
  295. if os.path.join('classes', '%s.bbclass' % klass) in val:
  296. return True
  297. return False