BBHandler.py 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. #!/usr/bin/env python
  2. # ex:ts=4:sw=4:sts=4:et
  3. # -*- tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*-
  4. """
  5. class for handling .bb files
  6. Reads a .bb file and obtains its metadata
  7. """
  8. # Copyright (C) 2003, 2004 Chris Larson
  9. # Copyright (C) 2003, 2004 Phil Blundell
  10. #
  11. # This program is free software; you can redistribute it and/or modify
  12. # it under the terms of the GNU General Public License version 2 as
  13. # published by the Free Software Foundation.
  14. #
  15. # This program is distributed in the hope that it will be useful,
  16. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. # GNU General Public License for more details.
  19. #
  20. # You should have received a copy of the GNU General Public License along
  21. # with this program; if not, write to the Free Software Foundation, Inc.,
  22. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  23. import re, bb, os
  24. import logging
  25. import bb.build, bb.utils
  26. from bb import data
  27. from . import ConfHandler
  28. from .. import resolve_file, ast, logger, ParseError
  29. from .ConfHandler import include, init
  30. # For compatibility
  31. bb.deprecate_import(__name__, "bb.parse", ["vars_from_file"])
  32. __func_start_regexp__ = re.compile( r"(((?P<py>python)|(?P<fr>fakeroot))\s*)*(?P<func>[\w\.\-\+\{\}\$]+)?\s*\(\s*\)\s*{$" )
  33. __inherit_regexp__ = re.compile( r"inherit\s+(.+)" )
  34. __export_func_regexp__ = re.compile( r"EXPORT_FUNCTIONS\s+(.+)" )
  35. __addtask_regexp__ = re.compile("addtask\s+(?P<func>\w+)\s*((before\s*(?P<before>((.*(?=after))|(.*))))|(after\s*(?P<after>((.*(?=before))|(.*)))))*")
  36. __deltask_regexp__ = re.compile("deltask\s+(?P<func>\w+)")
  37. __addhandler_regexp__ = re.compile( r"addhandler\s+(.+)" )
  38. __def_regexp__ = re.compile( r"def\s+(\w+).*:" )
  39. __python_func_regexp__ = re.compile( r"(\s+.*)|(^$)" )
  40. __infunc__ = []
  41. __inpython__ = False
  42. __body__ = []
  43. __classname__ = ""
  44. cached_statements = {}
  45. def supports(fn, d):
  46. """Return True if fn has a supported extension"""
  47. return os.path.splitext(fn)[-1] in [".bb", ".bbclass", ".inc"]
  48. def inherit(files, fn, lineno, d):
  49. __inherit_cache = d.getVar('__inherit_cache', False) or []
  50. files = d.expand(files).split()
  51. for file in files:
  52. if not os.path.isabs(file) and not file.endswith(".bbclass"):
  53. file = os.path.join('classes', '%s.bbclass' % file)
  54. if not os.path.isabs(file):
  55. bbpath = d.getVar("BBPATH", True)
  56. abs_fn, attempts = bb.utils.which(bbpath, file, history=True)
  57. for af in attempts:
  58. if af != abs_fn:
  59. bb.parse.mark_dependency(d, af)
  60. if abs_fn:
  61. file = abs_fn
  62. if not file in __inherit_cache:
  63. logger.debug(1, "Inheriting %s (from %s:%d)" % (file, fn, lineno))
  64. __inherit_cache.append( file )
  65. d.setVar('__inherit_cache', __inherit_cache)
  66. include(fn, file, lineno, d, "inherit")
  67. __inherit_cache = d.getVar('__inherit_cache', False) or []
  68. def get_statements(filename, absolute_filename, base_name):
  69. global cached_statements
  70. try:
  71. return cached_statements[absolute_filename]
  72. except KeyError:
  73. file = open(absolute_filename, 'r')
  74. statements = ast.StatementGroup()
  75. lineno = 0
  76. while True:
  77. lineno = lineno + 1
  78. s = file.readline()
  79. if not s: break
  80. s = s.rstrip()
  81. feeder(lineno, s, filename, base_name, statements)
  82. file.close()
  83. if __inpython__:
  84. # add a blank line to close out any python definition
  85. feeder(lineno, "", filename, base_name, statements, eof=True)
  86. if filename.endswith(".bbclass") or filename.endswith(".inc"):
  87. cached_statements[absolute_filename] = statements
  88. return statements
  89. def handle(fn, d, include):
  90. global __func_start_regexp__, __inherit_regexp__, __export_func_regexp__, __addtask_regexp__, __addhandler_regexp__, __infunc__, __body__, __residue__, __classname__
  91. __body__ = []
  92. __infunc__ = []
  93. __classname__ = ""
  94. __residue__ = []
  95. base_name = os.path.basename(fn)
  96. (root, ext) = os.path.splitext(base_name)
  97. init(d)
  98. if ext == ".bbclass":
  99. __classname__ = root
  100. __inherit_cache = d.getVar('__inherit_cache', False) or []
  101. if not fn in __inherit_cache:
  102. __inherit_cache.append(fn)
  103. d.setVar('__inherit_cache', __inherit_cache)
  104. if include != 0:
  105. oldfile = d.getVar('FILE', False)
  106. else:
  107. oldfile = None
  108. abs_fn = resolve_file(fn, d)
  109. if include:
  110. bb.parse.mark_dependency(d, abs_fn)
  111. # actual loading
  112. statements = get_statements(fn, abs_fn, base_name)
  113. # DONE WITH PARSING... time to evaluate
  114. if ext != ".bbclass" and abs_fn != oldfile:
  115. d.setVar('FILE', abs_fn)
  116. try:
  117. statements.eval(d)
  118. except bb.parse.SkipRecipe:
  119. bb.data.setVar("__SKIPPED", True, d)
  120. if include == 0:
  121. return { "" : d }
  122. if __infunc__:
  123. raise ParseError("Shell function %s is never closed" % __infunc__[0], __infunc__[1], __infunc__[2])
  124. if __residue__:
  125. raise ParseError("Leftover unparsed (incomplete?) data %s from %s" % __residue__, fn)
  126. if ext != ".bbclass" and include == 0:
  127. return ast.multi_finalize(fn, d)
  128. if ext != ".bbclass" and oldfile and abs_fn != oldfile:
  129. d.setVar("FILE", oldfile)
  130. return d
  131. def feeder(lineno, s, fn, root, statements, eof=False):
  132. global __func_start_regexp__, __inherit_regexp__, __export_func_regexp__, __addtask_regexp__, __addhandler_regexp__, __def_regexp__, __python_func_regexp__, __inpython__, __infunc__, __body__, bb, __residue__, __classname__
  133. if __infunc__:
  134. if s == '}':
  135. __body__.append('')
  136. ast.handleMethod(statements, fn, lineno, __infunc__[0], __body__, __infunc__[3], __infunc__[4])
  137. __infunc__ = []
  138. __body__ = []
  139. else:
  140. __body__.append(s)
  141. return
  142. if __inpython__:
  143. m = __python_func_regexp__.match(s)
  144. if m and not eof:
  145. __body__.append(s)
  146. return
  147. else:
  148. ast.handlePythonMethod(statements, fn, lineno, __inpython__,
  149. root, __body__)
  150. __body__ = []
  151. __inpython__ = False
  152. if eof:
  153. return
  154. if s and s[0] == '#':
  155. if len(__residue__) != 0 and __residue__[0][0] != "#":
  156. bb.fatal("There is a comment on line %s of file %s (%s) which is in the middle of a multiline expression.\nBitbake used to ignore these but no longer does so, please fix your metadata as errors are likely as a result of this change." % (lineno, fn, s))
  157. if len(__residue__) != 0 and __residue__[0][0] == "#" and (not s or s[0] != "#"):
  158. bb.fatal("There is a confusing multiline, partially commented expression on line %s of file %s (%s).\nPlease clarify whether this is all a comment or should be parsed." % (lineno, fn, s))
  159. if s and s[-1] == '\\':
  160. __residue__.append(s[:-1])
  161. return
  162. s = "".join(__residue__) + s
  163. __residue__ = []
  164. # Skip empty lines
  165. if s == '':
  166. return
  167. # Skip comments
  168. if s[0] == '#':
  169. return
  170. m = __func_start_regexp__.match(s)
  171. if m:
  172. __infunc__ = [m.group("func") or "__anonymous", fn, lineno, m.group("py") is not None, m.group("fr") is not None]
  173. return
  174. m = __def_regexp__.match(s)
  175. if m:
  176. __body__.append(s)
  177. __inpython__ = m.group(1)
  178. return
  179. m = __export_func_regexp__.match(s)
  180. if m:
  181. ast.handleExportFuncs(statements, fn, lineno, m, __classname__)
  182. return
  183. m = __addtask_regexp__.match(s)
  184. if m:
  185. ast.handleAddTask(statements, fn, lineno, m)
  186. return
  187. m = __deltask_regexp__.match(s)
  188. if m:
  189. ast.handleDelTask(statements, fn, lineno, m)
  190. return
  191. m = __addhandler_regexp__.match(s)
  192. if m:
  193. ast.handleBBHandlers(statements, fn, lineno, m)
  194. return
  195. m = __inherit_regexp__.match(s)
  196. if m:
  197. ast.handleInherit(statements, fn, lineno, m)
  198. return
  199. return ConfHandler.feeder(lineno, s, fn, statements)
  200. # Add us to the handlers list
  201. from .. import handlers
  202. handlers.append({'supports': supports, 'handle': handle, 'init': init})
  203. del handlers