Skip to content

Commit

Permalink
bitbake/lib:backport addpylib function
Browse files Browse the repository at this point in the history
Backported from:

commit 42e8b26
Author: Richard Purdie <richard.purdie@linuxfoundation.org>
Date:   Sun Nov 27 17:08:04 2022 +0000

    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.
  • Loading branch information
acostach committed May 8, 2023
1 parent d64bef1 commit debd42b
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 8 deletions.
2 changes: 1 addition & 1 deletion bitbake/lib/bb/cookerdata.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ def wrapped(fn, *args):

@catch_parse_error
def parse_config_file(fn, data, include=True):
return bb.parse.handle(fn, data, include)
return bb.parse.handle(fn, data, include, baseconfig=True)

@catch_parse_error
def _inherit(bbclass, data):
Expand Down
4 changes: 2 additions & 2 deletions bitbake/lib/bb/parse/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,12 @@ def supports(fn, data):
return 1
return 0

def handle(fn, data, include = 0):
def handle(fn, data, include=0, baseconfig=False):
"""Call the handler that is appropriate for this file"""
for h in handlers:
if h['supports'](fn, data):
with data.inchistory.include(fn):
return h['handle'](fn, data, include)
return h['handle'](fn, data, include, baseconfig)
raise ParseError("not a BitBake file", fn)

def init(fn, data):
Expand Down
27 changes: 27 additions & 0 deletions bitbake/lib/bb/parse/ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
# SPDX-License-Identifier: GPL-2.0-only
#

import sys
import bb
from bb import methodpool
from bb.parse import logger
Expand Down Expand Up @@ -269,6 +270,29 @@ def eval(self, data):
data.setVarFlag(h, "handler", 1)
data.setVar('__BBHANDLERS', bbhands)

class PyLibNode(AstNode):
def __init__(self, filename, lineno, libdir, namespace):
AstNode.__init__(self, filename, lineno)
self.libdir = libdir
self.namespace = namespace

def eval(self, data):
global_mods = (data.getVar("BB_GLOBAL_PYMODULES") or "").split()
for m in global_mods:
if m not in bb.utils._context:
bb.utils._context[m] = __import__(m)

libdir = data.expand(self.libdir)
if libdir not in sys.path:
sys.path.append(libdir)
try:
bb.utils._context[self.namespace] = __import__(self.namespace)
toimport = getattr(bb.utils._context[self.namespace], "BBIMPORTS", [])
for i in toimport:
bb.utils._context[self.namespace] = __import__(self.namespace + "." + i)
except AttributeError as e:
bb.error("Error importing OE modules: %s" % str(e))

class InheritNode(AstNode):
def __init__(self, filename, lineno, classes):
AstNode.__init__(self, filename, lineno)
Expand Down Expand Up @@ -320,6 +344,9 @@ def handleDelTask(statements, filename, lineno, m):
def handleBBHandlers(statements, filename, lineno, m):
statements.append(BBHandlerNode(filename, lineno, m.group(1)))

def handlePyLib(statements, filename, lineno, m):
statements.append(PyLibNode(filename, lineno, m.group(1), m.group(2)))

def handleInherit(statements, filename, lineno, m):
classes = m.group(1)
statements.append(InheritNode(filename, lineno, classes))
Expand Down
4 changes: 2 additions & 2 deletions bitbake/lib/bb/parse/parse_py/BBHandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def get_statements(filename, absolute_filename, base_name):
cached_statements[absolute_filename] = statements
return statements

def handle(fn, d, include):
def handle(fn, d, include, baseconfig=False):
global __func_start_regexp__, __inherit_regexp__, __export_func_regexp__, __addtask_regexp__, __addhandler_regexp__, __infunc__, __body__, __residue__, __classname__
__body__ = []
__infunc__ = []
Expand Down Expand Up @@ -252,7 +252,7 @@ def feeder(lineno, s, fn, root, statements, eof=False):
ast.handleInherit(statements, fn, lineno, m)
return

return ConfHandler.feeder(lineno, s, fn, statements)
return ConfHandler.feeder(lineno, s, fn, statements, conffile=False)

# Add us to the handlers list
from .. import handlers
Expand Down
14 changes: 11 additions & 3 deletions bitbake/lib/bb/parse/parse_py/ConfHandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class for handling configuration data files
__export_regexp__ = re.compile( r"export\s+([a-zA-Z0-9\-_+.${}/~]+)$" )
__unset_regexp__ = re.compile( r"unset\s+([a-zA-Z0-9\-_+.${}/~]+)$" )
__unset_flag_regexp__ = re.compile( r"unset\s+([a-zA-Z0-9\-_+.${}/~]+)\[([a-zA-Z0-9\-_+.]+)\]$" )
__addpylib_regexp__ = re.compile(r"addpylib\s+(.+)\s+(.+)" )

def init(data):
return
Expand Down Expand Up @@ -107,7 +108,7 @@ def include_single_file(parentfn, fn, lineno, data, error_out):
# parsing. This turns out to be a hard problem to solve any other way.
confFilters = []

def handle(fn, data, include):
def handle(fn, data, include, baseconfig=False):
init(data)

if include == 0:
Expand Down Expand Up @@ -144,7 +145,7 @@ def handle(fn, data, include):
# skip comments
if s[0] == '#':
continue
feeder(lineno, s, abs_fn, statements)
feeder(lineno, s, abs_fn, statements, baseconfig=baseconfig)

# DONE WITH PARSING... time to evaluate
data.setVar('FILE', abs_fn)
Expand All @@ -157,7 +158,9 @@ def handle(fn, data, include):

return data

def feeder(lineno, s, fn, statements):
# baseconfig is set for the bblayers/layer.conf cookerdata config parsing
# The function is also used by BBHandler, conffile would be False
def feeder(lineno, s, fn, statements, baseconfig=False, conffile=True):
m = __config_regexp__.match(s)
if m:
groupd = m.groupdict()
Expand Down Expand Up @@ -189,6 +192,11 @@ def feeder(lineno, s, fn, statements):
ast.handleUnsetFlag(statements, fn, lineno, m)
return

m = __addpylib_regexp__.match(s)
if baseconfig and conffile and m:
ast.handlePyLib(statements, fn, lineno, m)
return

raise ParseError("unparsed line: '%s'" % s, fn, lineno);

# Add us to the handlers list
Expand Down

0 comments on commit debd42b

Please sign in to comment.