BBHandler.py 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. from __future__ import absolute_import
  24. import re, bb, os
  25. import logging
  26. import bb.build, bb.utils
  27. from bb import data
  28. from . import ConfHandler
  29. from .. import resolve_file, ast, logger
  30. from .ConfHandler import include, init
  31. # For compatibility
  32. bb.deprecate_import(__name__, "bb.parse", ["vars_from_file"])
  33. __func_start_regexp__ = re.compile( r"(((?P<py>python)|(?P<fr>fakeroot))\s*)*(?P<func>[\w\.\-\+\{\}\$]+)?\s*\(\s*\)\s*{$" )
  34. __inherit_regexp__ = re.compile( r"inherit\s+(.+)" )
  35. __export_func_regexp__ = re.compile( r"EXPORT_FUNCTIONS\s+(.+)" )
  36. __addtask_regexp__ = re.compile("addtask\s+(?P<func>\w+)\s*((before\s*(?P<before>((.*(?=after))|(.*))))|(after\s*(?P<after>((.*(?=before))|(.*)))))*")
  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. # We need to indicate EOF to the feeder. This code is so messy that
  46. # factoring it out to a close_parse_file method is out of question.
  47. # We will use the IN_PYTHON_EOF as an indicator to just close the method
  48. #
  49. # The two parts using it are tightly integrated anyway
  50. IN_PYTHON_EOF = -9999999999999
  51. def supports(fn, d):
  52. """Return True if fn has a supported extension"""
  53. return os.path.splitext(fn)[-1] in [".bb", ".bbclass", ".inc"]
  54. def inherit(files, fn, lineno, d):
  55. __inherit_cache = d.getVar('__inherit_cache') or []
  56. files = d.expand(files).split()
  57. for file in files:
  58. if not os.path.isabs(file) and not file.endswith(".bbclass"):
  59. file = os.path.join('classes', '%s.bbclass' % file)
  60. if not os.path.isabs(file):
  61. dname = os.path.dirname(fn)
  62. bbpath = "%s:%s" % (dname, d.getVar("BBPATH", True))
  63. abs_fn = bb.utils.which(bbpath, file)
  64. if abs_fn:
  65. file = abs_fn
  66. if not file in __inherit_cache:
  67. logger.log(logging.DEBUG -1, "BB %s:%d: inheriting %s", fn, lineno, file)
  68. __inherit_cache.append( file )
  69. data.setVar('__inherit_cache', __inherit_cache, d)
  70. include(fn, file, lineno, d, "inherit")
  71. __inherit_cache = d.getVar('__inherit_cache') or []
  72. def get_statements(filename, absolute_filename, base_name):
  73. global cached_statements
  74. try:
  75. return cached_statements[absolute_filename]
  76. except KeyError:
  77. file = open(absolute_filename, 'r')
  78. statements = ast.StatementGroup()
  79. lineno = 0
  80. while True:
  81. lineno = lineno + 1
  82. s = file.readline()
  83. if not s: break
  84. s = s.rstrip()
  85. feeder(lineno, s, filename, base_name, statements)
  86. if __inpython__:
  87. # add a blank line to close out any python definition
  88. feeder(IN_PYTHON_EOF, "", filename, base_name, statements)
  89. if filename.endswith(".bbclass") or filename.endswith(".inc"):
  90. cached_statements[absolute_filename] = statements
  91. return statements
  92. def handle(fn, d, include):
  93. global __func_start_regexp__, __inherit_regexp__, __export_func_regexp__, __addtask_regexp__, __addhandler_regexp__, __infunc__, __body__, __residue__, __classname__
  94. __body__ = []
  95. __infunc__ = ""
  96. __classname__ = ""
  97. __residue__ = []
  98. if include == 0:
  99. logger.debug(2, "BB %s: handle(data)", fn)
  100. else:
  101. logger.debug(2, "BB %s: handle(data, include)", fn)
  102. base_name = os.path.basename(fn)
  103. (root, ext) = os.path.splitext(base_name)
  104. init(d)
  105. if ext == ".bbclass":
  106. __classname__ = root
  107. __inherit_cache = d.getVar('__inherit_cache') or []
  108. if not fn in __inherit_cache:
  109. __inherit_cache.append(fn)
  110. data.setVar('__inherit_cache', __inherit_cache, d)
  111. if include != 0:
  112. oldfile = d.getVar('FILE')
  113. else:
  114. oldfile = None
  115. abs_fn = resolve_file(fn, d)
  116. if include:
  117. bb.parse.mark_dependency(d, abs_fn)
  118. # actual loading
  119. statements = get_statements(fn, abs_fn, base_name)
  120. # DONE WITH PARSING... time to evaluate
  121. if ext != ".bbclass":
  122. data.setVar('FILE', abs_fn, d)
  123. try:
  124. statements.eval(d)
  125. except bb.parse.SkipPackage:
  126. bb.data.setVar("__SKIPPED", True, d)
  127. if include == 0:
  128. return { "" : d }
  129. if ext != ".bbclass" and include == 0:
  130. return ast.multi_finalize(fn, d)
  131. if oldfile:
  132. d.setVar("FILE", oldfile)
  133. # we have parsed the bb class now
  134. if ext == ".bbclass" or ext == ".inc":
  135. bb.methodpool.set_parsed_module(base_name)
  136. return d
  137. def feeder(lineno, s, fn, root, statements):
  138. global __func_start_regexp__, __inherit_regexp__, __export_func_regexp__, __addtask_regexp__, __addhandler_regexp__, __def_regexp__, __python_func_regexp__, __inpython__, __infunc__, __body__, bb, __residue__, __classname__
  139. if __infunc__:
  140. if s == '}':
  141. __body__.append('')
  142. ast.handleMethod(statements, fn, lineno, __infunc__, __body__)
  143. __infunc__ = ""
  144. __body__ = []
  145. else:
  146. __body__.append(s)
  147. return
  148. if __inpython__:
  149. m = __python_func_regexp__.match(s)
  150. if m and lineno != IN_PYTHON_EOF:
  151. __body__.append(s)
  152. return
  153. else:
  154. ast.handlePythonMethod(statements, fn, lineno, __inpython__,
  155. root, __body__)
  156. __body__ = []
  157. __inpython__ = False
  158. if lineno == IN_PYTHON_EOF:
  159. return
  160. if s and s[0] == '#':
  161. if len(__residue__) != 0 and __residue__[0][0] != "#":
  162. bb.error("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))
  163. if s and s[-1] == '\\':
  164. __residue__.append(s[:-1])
  165. return
  166. s = "".join(__residue__) + s
  167. __residue__ = []
  168. # Skip empty lines
  169. if s == '':
  170. return
  171. # Skip comments
  172. if s[0] == '#':
  173. return
  174. m = __func_start_regexp__.match(s)
  175. if m:
  176. __infunc__ = m.group("func") or "__anonymous"
  177. ast.handleMethodFlags(statements, fn, lineno, __infunc__, m)
  178. return
  179. m = __def_regexp__.match(s)
  180. if m:
  181. __body__.append(s)
  182. __inpython__ = m.group(1)
  183. return
  184. m = __export_func_regexp__.match(s)
  185. if m:
  186. ast.handleExportFuncs(statements, fn, lineno, m, __classname__)
  187. return
  188. m = __addtask_regexp__.match(s)
  189. if m:
  190. ast.handleAddTask(statements, fn, lineno, m)
  191. return
  192. m = __addhandler_regexp__.match(s)
  193. if m:
  194. ast.handleBBHandlers(statements, fn, lineno, m)
  195. return
  196. m = __inherit_regexp__.match(s)
  197. if m:
  198. ast.handleInherit(statements, fn, lineno, m)
  199. return
  200. return ConfHandler.feeder(lineno, s, fn, statements)
  201. # Add us to the handlers list
  202. from .. import handlers
  203. handlers.append({'supports': supports, 'handle': handle, 'init': init})
  204. del handlers