summaryrefslogtreecommitdiffstats
path: root/bitbake/lib/bb/parse/ast.py
diff options
context:
space:
mode:
authorRichard Purdie <richard.purdie@linuxfoundation.org>2022-11-27 17:08:04 +0000
committerRichard Purdie <richard.purdie@linuxfoundation.org>2022-12-08 11:15:47 +0000
commit42e8b2682f56f450c1258956213198052d6bcb96 (patch)
tree6e1e223a179f5df5651fe3742b0e8afa55339597 /bitbake/lib/bb/parse/ast.py
parentf4ffba353eb7877060c25d264fa10cd60f797f0b (diff)
downloadpoky-42e8b2682f56f450c1258956213198052d6bcb96.tar.gz
bitbake: parse: Add support for addpylib conf file directive and BB_GLOBAL_PYMODULES
For many years OE-Core has injected it's own python modules into the python namespace using an immediate expansion of a variable in base.bbclass. It also added all entries from BBPATH to sys.path. We really need this to become a first class citizen of the langauge, this new addpylib directive allows that. Usage is of the form: addpylib <directory> <namespace> The namespace is imported and if there is an attribute BBIMPORT, that list of names is iterated and imported too. This mirrors what OE-Core has done for a long time with one difference in implmentation, sys.path is only appended to. This means later imported namespaces can't overwrite an earlier one and can't overwrite the main python module space. In practice we've never done that and it isn't something we should encourage or support. The new directive is only applied for .conf files and not other filetypes as it only makes sense in that context. It is also only allowed in the "base configuration" context of cookerdata since adding it at the recipe level wouldn't work properly due to the way it changes the global namespace. At the same time, move the list of modules to place in the global namespace into a BB_GLOBAL_PYMODULES variable. It is intended that only the core layer should touch this and it is meant to be a very small list, usually os and sys. BB_GLOBAL_PYMODULES is expected to be set before the first addpylib directive. Layers adding a lib directory will now need to use this directive as BBPATH is not going to be added automatically by OE-Core in future. The directives are immediate operations so it does make modules available sooner than the current OE-Core approach. The new code appends to sys.path rather than prepends as core did, as overwriting python standard library modules would be a bad idea and naturally encouraging people to collaborate around our own core modules is desireable. (Bitbake rev: afb8478d3853f6edf3669b93588314627d617d6b) Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
Diffstat (limited to 'bitbake/lib/bb/parse/ast.py')
-rw-r--r--bitbake/lib/bb/parse/ast.py27
1 files changed, 27 insertions, 0 deletions
diff --git a/bitbake/lib/bb/parse/ast.py b/bitbake/lib/bb/parse/ast.py
index 9e0a0f5c98..862087c77d 100644
--- a/bitbake/lib/bb/parse/ast.py
+++ b/bitbake/lib/bb/parse/ast.py
@@ -9,6 +9,7 @@
9# SPDX-License-Identifier: GPL-2.0-only 9# SPDX-License-Identifier: GPL-2.0-only
10# 10#
11 11
12import sys
12import bb 13import bb
13from bb import methodpool 14from bb import methodpool
14from bb.parse import logger 15from bb.parse import logger
@@ -269,6 +270,29 @@ class BBHandlerNode(AstNode):
269 data.setVarFlag(h, "handler", 1) 270 data.setVarFlag(h, "handler", 1)
270 data.setVar('__BBHANDLERS', bbhands) 271 data.setVar('__BBHANDLERS', bbhands)
271 272
273class PyLibNode(AstNode):
274 def __init__(self, filename, lineno, libdir, namespace):
275 AstNode.__init__(self, filename, lineno)
276 self.libdir = libdir
277 self.namespace = namespace
278
279 def eval(self, data):
280 global_mods = (data.getVar("BB_GLOBAL_PYMODULES") or "").split()
281 for m in global_mods:
282 if m not in bb.utils._context:
283 bb.utils._context[m] = __import__(m)
284
285 libdir = data.expand(self.libdir)
286 if libdir not in sys.path:
287 sys.path.append(libdir)
288 try:
289 bb.utils._context[self.namespace] = __import__(self.namespace)
290 toimport = getattr(bb.utils._context[self.namespace], "BBIMPORTS", [])
291 for i in toimport:
292 bb.utils._context[self.namespace] = __import__(self.namespace + "." + i)
293 except AttributeError as e:
294 bb.error("Error importing OE modules: %s" % str(e))
295
272class InheritNode(AstNode): 296class InheritNode(AstNode):
273 def __init__(self, filename, lineno, classes): 297 def __init__(self, filename, lineno, classes):
274 AstNode.__init__(self, filename, lineno) 298 AstNode.__init__(self, filename, lineno)
@@ -320,6 +344,9 @@ def handleDelTask(statements, filename, lineno, m):
320def handleBBHandlers(statements, filename, lineno, m): 344def handleBBHandlers(statements, filename, lineno, m):
321 statements.append(BBHandlerNode(filename, lineno, m.group(1))) 345 statements.append(BBHandlerNode(filename, lineno, m.group(1)))
322 346
347def handlePyLib(statements, filename, lineno, m):
348 statements.append(PyLibNode(filename, lineno, m.group(1), m.group(2)))
349
323def handleInherit(statements, filename, lineno, m): 350def handleInherit(statements, filename, lineno, m):
324 classes = m.group(1) 351 classes = m.group(1)
325 statements.append(InheritNode(filename, lineno, classes)) 352 statements.append(InheritNode(filename, lineno, classes))