summaryrefslogtreecommitdiffstats
path: root/bitbake/lib/bb/parse/parse_py
diff options
context:
space:
mode:
authorRichard Purdie <richard.purdie@linuxfoundation.org>2025-11-07 13:31:53 +0000
committerRichard Purdie <richard.purdie@linuxfoundation.org>2025-11-07 13:31:53 +0000
commit8c22ff0d8b70d9b12f0487ef696a7e915b9e3173 (patch)
treeefdc32587159d0050a69009bdf2330a531727d95 /bitbake/lib/bb/parse/parse_py
parentd412d2747595c1cc4a5e3ca975e3adc31b2f7891 (diff)
downloadpoky-8c22ff0d8b70d9b12f0487ef696a7e915b9e3173.tar.gz
The poky repository master branch is no longer being updated.
You can either: a) switch to individual clones of bitbake, openembedded-core, meta-yocto and yocto-docs b) use the new bitbake-setup You can find information about either approach in our documentation: https://docs.yoctoproject.org/ Note that "poky" the distro setting is still available in meta-yocto as before and we continue to use and maintain that. Long live Poky! Some further information on the background of this change can be found in: https://lists.openembedded.org/g/openembedded-architecture/message/2179 Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
Diffstat (limited to 'bitbake/lib/bb/parse/parse_py')
-rw-r--r--bitbake/lib/bb/parse/parse_py/BBHandler.py306
-rw-r--r--bitbake/lib/bb/parse/parse_py/ConfHandler.py221
-rw-r--r--bitbake/lib/bb/parse/parse_py/__init__.py20
3 files changed, 0 insertions, 547 deletions
diff --git a/bitbake/lib/bb/parse/parse_py/BBHandler.py b/bitbake/lib/bb/parse/parse_py/BBHandler.py
deleted file mode 100644
index 008fec2308..0000000000
--- a/bitbake/lib/bb/parse/parse_py/BBHandler.py
+++ /dev/null
@@ -1,306 +0,0 @@
1"""
2 class for handling .bb files
3
4 Reads a .bb file and obtains its metadata
5
6"""
7
8
9# Copyright (C) 2003, 2004 Chris Larson
10# Copyright (C) 2003, 2004 Phil Blundell
11#
12# SPDX-License-Identifier: GPL-2.0-only
13#
14
15import re, bb, os
16import bb.build, bb.utils, bb.data_smart
17
18from . import ConfHandler
19from .. import resolve_file, ast, logger, ParseError
20from .ConfHandler import include, init
21
22__func_start_regexp__ = re.compile(r"(((?P<py>python(?=(\s|\()))|(?P<fr>fakeroot(?=\s)))\s*)*(?P<func>[\w\.\-\+\{\}\$:]+)?\s*\(\s*\)\s*{$" )
23__inherit_regexp__ = re.compile(r"inherit\s+(.+)" )
24__inherit_def_regexp__ = re.compile(r"inherit_defer\s+(.+)" )
25__export_func_regexp__ = re.compile(r"EXPORT_FUNCTIONS\s+(.+)" )
26__addtask_regexp__ = re.compile(r"addtask\s+([^#\n]+)(?P<comment>#.*|.*?)")
27__deltask_regexp__ = re.compile(r"deltask\s+([^#\n]+)(?P<comment>#.*|.*?)")
28__addhandler_regexp__ = re.compile(r"addhandler\s+(.+)" )
29__def_regexp__ = re.compile(r"def\s+(\w+).*:" )
30__python_func_regexp__ = re.compile(r"(\s+.*)|(^$)|(^#)" )
31__python_tab_regexp__ = re.compile(r" *\t")
32
33__infunc__ = []
34__inpython__ = False
35__body__ = []
36__classname__ = ""
37__residue__ = []
38
39cached_statements = {}
40
41def supports(fn, d):
42 """Return True if fn has a supported extension"""
43 return os.path.splitext(fn)[-1] in [".bb", ".bbclass", ".inc"]
44
45def inherit_defer(expression, fn, lineno, d):
46 inherit = (expression, fn, lineno)
47 inherits = d.getVar('__BBDEFINHERITS', False) or []
48 inherits.append(inherit)
49 d.setVar('__BBDEFINHERITS', inherits)
50
51def inherit(files, fn, lineno, d, deferred=False):
52 __inherit_cache = d.getVar('__inherit_cache', False) or []
53 #if "${" in files and not deferred:
54 # bb.warn("%s:%s has non deferred conditional inherit" % (fn, lineno))
55 files = d.expand(files).split()
56 for file in files:
57 defer = (d.getVar("BB_DEFER_BBCLASSES") or "").split()
58 if not deferred and file in defer:
59 inherit_defer(file, fn, lineno, d)
60 continue
61 classtype = d.getVar("__bbclasstype", False)
62 origfile = file
63 for t in ["classes-" + classtype, "classes"]:
64 file = origfile
65 if not os.path.isabs(file) and not file.endswith(".bbclass"):
66 file = os.path.join(t, '%s.bbclass' % file)
67
68 if not os.path.isabs(file):
69 bbpath = d.getVar("BBPATH")
70 abs_fn, attempts = bb.utils.which(bbpath, file, history=True)
71 for af in attempts:
72 if af != abs_fn:
73 bb.parse.mark_dependency(d, af)
74 if abs_fn:
75 file = abs_fn
76
77 if os.path.exists(file):
78 break
79
80 if not os.path.exists(file):
81 raise ParseError("Could not inherit file %s" % (file), fn, lineno)
82
83 if not file in __inherit_cache:
84 logger.debug("Inheriting %s (from %s:%d)" % (file, fn, lineno))
85 __inherit_cache.append( file )
86 d.setVar('__inherit_cache', __inherit_cache)
87 try:
88 bb.parse.handle(file, d, True)
89 except (IOError, OSError) as exc:
90 raise ParseError("Could not inherit file %s: %s" % (fn, exc.strerror), fn, lineno)
91 __inherit_cache = d.getVar('__inherit_cache', False) or []
92
93def get_statements(filename, absolute_filename, base_name):
94 global cached_statements, __residue__, __body__
95
96 try:
97 return cached_statements[absolute_filename]
98 except KeyError:
99 with open(absolute_filename, 'r') as f:
100 statements = ast.StatementGroup()
101
102 lineno = 0
103 while True:
104 lineno = lineno + 1
105 s = f.readline()
106 if not s: break
107 s = s.rstrip()
108 feeder(lineno, s, filename, base_name, statements)
109
110 if __inpython__:
111 # add a blank line to close out any python definition
112 feeder(lineno, "", filename, base_name, statements, eof=True)
113
114 if __residue__:
115 raise ParseError("Unparsed lines %s: %s" % (filename, str(__residue__)), filename, lineno)
116 if __body__:
117 raise ParseError("Unparsed lines from unclosed function %s: %s" % (filename, str(__body__)), filename, lineno)
118
119 if filename.endswith(".bbclass") or filename.endswith(".inc"):
120 cached_statements[absolute_filename] = statements
121 return statements
122
123def handle(fn, d, include, baseconfig=False):
124 global __infunc__, __body__, __residue__, __classname__
125 __body__ = []
126 __infunc__ = []
127 __classname__ = ""
128 __residue__ = []
129
130 base_name = os.path.basename(fn)
131 (root, ext) = os.path.splitext(base_name)
132 init(d)
133
134 if ext == ".bbclass":
135 __classname__ = root
136 __inherit_cache = d.getVar('__inherit_cache', False) or []
137 if not fn in __inherit_cache:
138 __inherit_cache.append(fn)
139 d.setVar('__inherit_cache', __inherit_cache)
140
141 if include != 0:
142 oldfile = d.getVar('FILE', False)
143 else:
144 oldfile = None
145
146 abs_fn = resolve_file(fn, d)
147
148 # actual loading
149 statements = get_statements(fn, abs_fn, base_name)
150
151 # DONE WITH PARSING... time to evaluate
152 if ext != ".bbclass" and abs_fn != oldfile:
153 d.setVar('FILE', abs_fn)
154
155 try:
156 statements.eval(d)
157 except bb.parse.SkipRecipe:
158 d.setVar("__SKIPPED", True)
159 if include == 0:
160 return { "" : d }
161
162 if __infunc__:
163 raise ParseError("Shell function %s is never closed" % __infunc__[0], __infunc__[1], __infunc__[2])
164 if __residue__:
165 raise ParseError("Leftover unparsed (incomplete?) data %s from %s" % __residue__, fn)
166
167 if ext != ".bbclass" and include == 0:
168 return ast.multi_finalize(fn, d)
169
170 if ext != ".bbclass" and oldfile and abs_fn != oldfile:
171 d.setVar("FILE", oldfile)
172
173 return d
174
175def feeder(lineno, s, fn, root, statements, eof=False):
176 global __inpython__, __infunc__, __body__, __residue__, __classname__
177
178 # Check tabs in python functions:
179 # - def py_funcname(): covered by __inpython__
180 # - python(): covered by '__anonymous' == __infunc__[0]
181 # - python funcname(): covered by __infunc__[3]
182 if __inpython__ or (__infunc__ and ('__anonymous' == __infunc__[0] or __infunc__[3])):
183 tab = __python_tab_regexp__.match(s)
184 if tab:
185 bb.warn('python should use 4 spaces indentation, but found tabs in %s, line %s' % (root, lineno))
186
187 if __infunc__:
188 if s == '}':
189 __body__.append('')
190 ast.handleMethod(statements, fn, lineno, __infunc__[0], __body__, __infunc__[3], __infunc__[4])
191 __infunc__ = []
192 __body__ = []
193 else:
194 __body__.append(s)
195 return
196
197 if __inpython__:
198 m = __python_func_regexp__.match(s)
199 if m and not eof:
200 __body__.append(s)
201 return
202 else:
203 ast.handlePythonMethod(statements, fn, lineno, __inpython__,
204 root, __body__)
205 __body__ = []
206 __inpython__ = False
207
208 if eof:
209 return
210
211 if s and s[0] == '#':
212 if len(__residue__) != 0 and __residue__[0][0] != "#":
213 bb.fatal("There is a comment on line %s of file %s:\n'''\n%s\n'''\nwhich is in the middle of a multiline expression. This syntax is invalid, please correct it." % (lineno, fn, s))
214
215 if len(__residue__) != 0 and __residue__[0][0] == "#" and (not s or s[0] != "#"):
216 bb.fatal("There is a confusing multiline partially commented expression on line %s of file %s:\n%s\nPlease clarify whether this is all a comment or should be parsed." % (lineno - len(__residue__), fn, "\n".join(__residue__)))
217
218 if s and s[-1] == '\\':
219 __residue__.append(s[:-1])
220 return
221
222 s = "".join(__residue__) + s
223 __residue__ = []
224
225 # Skip empty lines
226 if s == '':
227 return
228
229 # Skip comments
230 if s[0] == '#':
231 return
232
233 m = __func_start_regexp__.match(s)
234 if m:
235 __infunc__ = [m.group("func") or "__anonymous", fn, lineno, m.group("py") is not None, m.group("fr") is not None]
236 return
237
238 m = __def_regexp__.match(s)
239 if m:
240 __body__.append(s)
241 __inpython__ = m.group(1)
242
243 return
244
245 m = __export_func_regexp__.match(s)
246 if m:
247 ast.handleExportFuncs(statements, fn, lineno, m, __classname__)
248 return
249
250 m = __addtask_regexp__.match(s)
251 if m:
252 after = ""
253 before = ""
254
255 # This code splits on 'before' and 'after' instead of on whitespace so we can defer
256 # evaluation to as late as possible.
257 tasks = m.group(1).split(" before ")[0].split(" after ")[0]
258
259 for exp in m.group(1).split(" before "):
260 exp2 = exp.split(" after ")
261 if len(exp2) > 1:
262 after = after + " ".join(exp2[1:])
263
264 for exp in m.group(1).split(" after "):
265 exp2 = exp.split(" before ")
266 if len(exp2) > 1:
267 before = before + " ".join(exp2[1:])
268
269 # Check and warn for having task with a keyword as part of task name
270 taskexpression = s.split()
271 for te in taskexpression:
272 if any( ( "%s_" % keyword ) in te for keyword in bb.data_smart.__setvar_keyword__ ):
273 raise ParseError("Task name '%s' contains a keyword which is not recommended/supported.\nPlease rename the task not to include the keyword.\n%s" % (te, ("\n".join(map(str, bb.data_smart.__setvar_keyword__)))), fn)
274
275 if tasks is not None:
276 ast.handleAddTask(statements, fn, lineno, tasks, before, after)
277 return
278
279 m = __deltask_regexp__.match(s)
280 if m:
281 task = m.group(1)
282 if task is not None:
283 ast.handleDelTask(statements, fn, lineno, task)
284 return
285
286 m = __addhandler_regexp__.match(s)
287 if m:
288 ast.handleBBHandlers(statements, fn, lineno, m)
289 return
290
291 m = __inherit_regexp__.match(s)
292 if m:
293 ast.handleInherit(statements, fn, lineno, m)
294 return
295
296 m = __inherit_def_regexp__.match(s)
297 if m:
298 ast.handleInheritDeferred(statements, fn, lineno, m)
299 return
300
301 return ConfHandler.feeder(lineno, s, fn, statements, conffile=False)
302
303# Add us to the handlers list
304from .. import handlers
305handlers.append({'supports': supports, 'handle': handle, 'init': init})
306del handlers
diff --git a/bitbake/lib/bb/parse/parse_py/ConfHandler.py b/bitbake/lib/bb/parse/parse_py/ConfHandler.py
deleted file mode 100644
index 9ddbae123d..0000000000
--- a/bitbake/lib/bb/parse/parse_py/ConfHandler.py
+++ /dev/null
@@ -1,221 +0,0 @@
1"""
2 class for handling configuration data files
3
4 Reads a .conf file and obtains its metadata
5
6"""
7
8# Copyright (C) 2003, 2004 Chris Larson
9# Copyright (C) 2003, 2004 Phil Blundell
10#
11# SPDX-License-Identifier: GPL-2.0-only
12#
13
14import errno
15import re
16import os
17import bb.utils
18from bb.parse import ParseError, resolve_file, ast, logger, handle
19
20__config_regexp__ = re.compile( r"""
21 ^
22 (?P<exp>export\s+)?
23 (?P<var>[a-zA-Z0-9\-_+.${}/~:]*?)
24 (\[(?P<flag>[a-zA-Z0-9\-_+.][a-zA-Z0-9\-_+.@/]*)\])?
25
26 (?P<whitespace>\s*) (
27 (?P<colon>:=) |
28 (?P<lazyques>\?\?=) |
29 (?P<ques>\?=) |
30 (?P<append>\+=) |
31 (?P<prepend>=\+) |
32 (?P<predot>=\.) |
33 (?P<postdot>\.=) |
34 =
35 ) (?P<whitespace2>\s*)
36
37 (?!'[^']*'[^']*'$)
38 (?!\"[^\"]*\"[^\"]*\"$)
39 (?P<apo>['\"])
40 (?P<value>.*)
41 (?P=apo)
42 $
43 """, re.X)
44__include_regexp__ = re.compile( r"include\s+(.+)" )
45__require_regexp__ = re.compile( r"require\s+(.+)" )
46__includeall_regexp__ = re.compile( r"include_all\s+(.+)" )
47__export_regexp__ = re.compile( r"export\s+([a-zA-Z0-9\-_+.${}/~]+)$" )
48__unset_regexp__ = re.compile( r"unset\s+([a-zA-Z0-9\-_+.${}/~]+)$" )
49__unset_flag_regexp__ = re.compile( r"unset\s+([a-zA-Z0-9\-_+.${}/~]+)\[([a-zA-Z0-9\-_+.][a-zA-Z0-9\-_+.@]+)\]$" )
50__addpylib_regexp__ = re.compile(r"addpylib\s+(.+)\s+(.+)" )
51__addfragments_regexp__ = re.compile(r"addfragments\s+(.+)\s+(.+)\s+(.+)\s+(.+)" )
52
53def init(data):
54 return
55
56def supports(fn, d):
57 return fn[-5:] == ".conf"
58
59def include(parentfn, fns, lineno, data, error_out):
60 """
61 error_out: A string indicating the verb (e.g. "include", "inherit") to be
62 used in a ParseError that will be raised if the file to be included could
63 not be included. Specify False to avoid raising an error in this case.
64 """
65 fns = data.expand(fns)
66 parentfn = data.expand(parentfn)
67
68 # "include" or "require" accept zero to n space-separated file names to include.
69 for fn in fns.split():
70 include_single_file(parentfn, fn, lineno, data, error_out)
71
72def include_single_file(parentfn, fn, lineno, data, error_out):
73 """
74 Helper function for include() which does not expand or split its parameters.
75 """
76 if parentfn == fn: # prevent infinite recursion
77 return None
78
79 if not os.path.isabs(fn):
80 dname = os.path.dirname(parentfn)
81 bbpath = "%s:%s" % (dname, data.getVar("BBPATH"))
82 abs_fn, attempts = bb.utils.which(bbpath, fn, history=True)
83 if abs_fn and bb.parse.check_dependency(data, abs_fn):
84 logger.warning("Duplicate inclusion for %s in %s" % (abs_fn, data.getVar('FILE')))
85 for af in attempts:
86 bb.parse.mark_dependency(data, af)
87 if abs_fn:
88 fn = abs_fn
89 elif bb.parse.check_dependency(data, fn):
90 logger.warning("Duplicate inclusion for %s in %s" % (fn, data.getVar('FILE')))
91
92 try:
93 bb.parse.handle(fn, data, True)
94 except (IOError, OSError) as exc:
95 if exc.errno == errno.ENOENT:
96 if error_out:
97 raise ParseError("Could not %s file %s" % (error_out, fn), parentfn, lineno)
98 logger.debug2("CONF file '%s' not found", fn)
99 else:
100 if error_out:
101 raise ParseError("Could not %s file %s: %s" % (error_out, fn, exc.strerror), parentfn, lineno)
102 else:
103 raise ParseError("Error parsing %s: %s" % (fn, exc.strerror), parentfn, lineno)
104
105# We have an issue where a UI might want to enforce particular settings such as
106# an empty DISTRO variable. If configuration files do something like assigning
107# a weak default, it turns out to be very difficult to filter out these changes,
108# particularly when the weak default might appear half way though parsing a chain
109# of configuration files. We therefore let the UIs hook into configuration file
110# parsing. This turns out to be a hard problem to solve any other way.
111confFilters = []
112
113def handle(fn, data, include, baseconfig=False):
114 init(data)
115
116 if include == 0:
117 oldfile = None
118 else:
119 oldfile = data.getVar('FILE', False)
120
121 abs_fn = resolve_file(fn, data)
122 with open(abs_fn, 'r') as f:
123
124 statements = ast.StatementGroup()
125 lineno = 0
126 while True:
127 lineno = lineno + 1
128 s = f.readline()
129 if not s:
130 break
131 origlineno = lineno
132 origline = s
133 w = s.strip()
134 # skip empty lines
135 if not w:
136 continue
137 s = s.rstrip()
138 while s[-1] == '\\':
139 line = f.readline()
140 origline += line
141 s2 = line.rstrip()
142 lineno = lineno + 1
143 if (not s2 or s2 and s2[0] != "#") and s[0] == "#" :
144 bb.fatal("There is a confusing multiline, partially commented expression starting on line %s of file %s:\n%s\nPlease clarify whether this is all a comment or should be parsed." % (origlineno, fn, origline))
145
146 s = s[:-1] + s2
147 # skip comments
148 if s[0] == '#':
149 continue
150 feeder(lineno, s, abs_fn, statements, baseconfig=baseconfig)
151
152 # DONE WITH PARSING... time to evaluate
153 data.setVar('FILE', abs_fn)
154 statements.eval(data)
155 if oldfile:
156 data.setVar('FILE', oldfile)
157
158 for f in confFilters:
159 f(fn, data)
160
161 return data
162
163# baseconfig is set for the bblayers/layer.conf cookerdata config parsing
164# The function is also used by BBHandler, conffile would be False
165def feeder(lineno, s, fn, statements, baseconfig=False, conffile=True):
166 m = __config_regexp__.match(s)
167 if m:
168 groupd = m.groupdict()
169 if groupd['var'] == "":
170 raise ParseError("Empty variable name in assignment: '%s'" % s, fn, lineno);
171 if not groupd['whitespace'] or not groupd['whitespace2']:
172 logger.warning("%s:%s has a lack of whitespace around the assignment: '%s'" % (fn, lineno, s))
173 ast.handleData(statements, fn, lineno, groupd)
174 return
175
176 m = __include_regexp__.match(s)
177 if m:
178 ast.handleInclude(statements, fn, lineno, m, False)
179 return
180
181 m = __require_regexp__.match(s)
182 if m:
183 ast.handleInclude(statements, fn, lineno, m, True)
184 return
185
186 m = __includeall_regexp__.match(s)
187 if m:
188 ast.handleIncludeAll(statements, fn, lineno, m)
189 return
190
191 m = __export_regexp__.match(s)
192 if m:
193 ast.handleExport(statements, fn, lineno, m)
194 return
195
196 m = __unset_regexp__.match(s)
197 if m:
198 ast.handleUnset(statements, fn, lineno, m)
199 return
200
201 m = __unset_flag_regexp__.match(s)
202 if m:
203 ast.handleUnsetFlag(statements, fn, lineno, m)
204 return
205
206 m = __addpylib_regexp__.match(s)
207 if baseconfig and conffile and m:
208 ast.handlePyLib(statements, fn, lineno, m)
209 return
210
211 m = __addfragments_regexp__.match(s)
212 if m:
213 ast.handleAddFragments(statements, fn, lineno, m)
214 return
215
216 raise ParseError("unparsed line: '%s'" % s, fn, lineno);
217
218# Add us to the handlers list
219from bb.parse import handlers
220handlers.append({'supports': supports, 'handle': handle, 'init': init})
221del handlers
diff --git a/bitbake/lib/bb/parse/parse_py/__init__.py b/bitbake/lib/bb/parse/parse_py/__init__.py
deleted file mode 100644
index f508afa14e..0000000000
--- a/bitbake/lib/bb/parse/parse_py/__init__.py
+++ /dev/null
@@ -1,20 +0,0 @@
1"""
2BitBake Parsers
3
4File parsers for the BitBake build tools.
5
6"""
7
8# Copyright (C) 2003, 2004 Chris Larson
9# Copyright (C) 2003, 2004 Phil Blundell
10#
11# SPDX-License-Identifier: GPL-2.0-only
12#
13# Based on functions from the base bb module, Copyright 2003 Holger Schurig
14#
15
16from __future__ import absolute_import
17from . import ConfHandler
18from . import BBHandler
19
20__version__ = '1.0'