summaryrefslogtreecommitdiffstats
path: root/bitbake/lib/bb/parse/parse_py/BBHandler.py
diff options
context:
space:
mode:
Diffstat (limited to 'bitbake/lib/bb/parse/parse_py/BBHandler.py')
-rw-r--r--bitbake/lib/bb/parse/parse_py/BBHandler.py306
1 files changed, 0 insertions, 306 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