From 99d326a818a49faf457c707ceeec6163bf8c8e16 Mon Sep 17 00:00:00 2001 From: Paul Eggleton Date: Thu, 23 Feb 2012 17:38:08 +0000 Subject: bitbake: add file and line number to ParseError Ensure that a file and line number are reported for ParseError where possible. This helps particularly in the case of inherit and require which previously did not report either of these upon failure. (Bitbake rev: f588ba69622a2df35417ced184e56c79ac1b40d5) Signed-off-by: Paul Eggleton Signed-off-by: Richard Purdie --- bitbake/lib/bb/cooker.py | 2 +- bitbake/lib/bb/parse/__init__.py | 15 +++++++++++++-- bitbake/lib/bb/parse/ast.py | 8 ++++---- bitbake/lib/bb/parse/parse_py/BBHandler.py | 6 ++---- bitbake/lib/bb/parse/parse_py/ConfHandler.py | 11 ++++++----- 5 files changed, 26 insertions(+), 16 deletions(-) (limited to 'bitbake') diff --git a/bitbake/lib/bb/cooker.py b/bitbake/lib/bb/cooker.py index 308805a712..bf25a8320e 100644 --- a/bitbake/lib/bb/cooker.py +++ b/bitbake/lib/bb/cooker.py @@ -1398,7 +1398,7 @@ def _parse(fn, data, include=True): @catch_parse_error def _inherit(bbclass, data): - bb.parse.BBHandler.inherit([bbclass], data) + bb.parse.BBHandler.inherit([bbclass], "configuration INHERITs", 0, data) return data class ParsingFailure(Exception): diff --git a/bitbake/lib/bb/parse/__init__.py b/bitbake/lib/bb/parse/__init__.py index c5005aec9a..8b7ec73c57 100644 --- a/bitbake/lib/bb/parse/__init__.py +++ b/bitbake/lib/bb/parse/__init__.py @@ -37,6 +37,17 @@ logger = logging.getLogger("BitBake.Parsing") class ParseError(Exception): """Exception raised when parsing fails""" + def __init__(self, msg, filename, lineno=0): + self.msg = msg + self.filename = filename + self.lineno = lineno + Exception.__init__(self, msg, filename, lineno) + + def __str__(self): + if self.lineno: + return "ParseError at %s:%d: %s" % (self.filename, self.lineno, self.msg) + else: + return "ParseError in %s: %s" % (self.filename, self.msg) class SkipPackage(Exception): """Exception raised to skip this package""" @@ -78,7 +89,7 @@ def handle(fn, data, include = 0): for h in handlers: if h['supports'](fn, data): return h['handle'](fn, data, include) - raise ParseError("%s is not a BitBake file" % fn) + raise ParseError("not a BitBake file", fn) def init(fn, data): for h in handlers: @@ -111,7 +122,7 @@ def vars_from_file(mypkg, d): parts = myfile[0].split('_') __pkgsplit_cache__[mypkg] = parts if len(parts) > 3: - raise ParseError("Unable to generate default variables from the filename: %s (too many underscores)" % mypkg) + raise ParseError("Unable to generate default variables from filename (too many underscores)", mypkg) exp = 3 - len(parts) tmplist = [] while exp != 0: diff --git a/bitbake/lib/bb/parse/ast.py b/bitbake/lib/bb/parse/ast.py index 31c930d9cf..94fa175bba 100644 --- a/bitbake/lib/bb/parse/ast.py +++ b/bitbake/lib/bb/parse/ast.py @@ -59,9 +59,9 @@ class IncludeNode(AstNode): # TODO: Cache those includes... maybe not here though if self.force: - bb.parse.ConfHandler.include(self.filename, s, data, "include required") + bb.parse.ConfHandler.include(self.filename, s, self.lineno, data, "include required") else: - bb.parse.ConfHandler.include(self.filename, s, data, False) + bb.parse.ConfHandler.include(self.filename, s, self.lineno, data, False) class ExportNode(AstNode): def __init__(self, filename, lineno, var): @@ -267,7 +267,7 @@ class InheritNode(AstNode): self.classes = classes def eval(self, data): - bb.parse.BBHandler.inherit(self.classes, data) + bb.parse.BBHandler.inherit(self.classes, self.filename, self.lineno, data) def handleInclude(statements, filename, lineno, m, force): statements.append(IncludeNode(filename, lineno, m.group(1), force)) @@ -450,7 +450,7 @@ def multi_finalize(fn, d): d.setVar("BBEXTENDVARIANT", variantmap[name]) else: d.setVar("PN", "%s-%s" % (pn, name)) - bb.parse.BBHandler.inherit([extendedmap[name]], d) + bb.parse.BBHandler.inherit([extendedmap[name]], fn, 0, d) safe_d.setVar("BBCLASSEXTEND", extended) _create_variants(datastores, extendedmap.keys(), extendfunc) diff --git a/bitbake/lib/bb/parse/parse_py/BBHandler.py b/bitbake/lib/bb/parse/parse_py/BBHandler.py index 2d6e331a1d..125f458de7 100644 --- a/bitbake/lib/bb/parse/parse_py/BBHandler.py +++ b/bitbake/lib/bb/parse/parse_py/BBHandler.py @@ -68,10 +68,8 @@ def supports(fn, d): """Return True if fn has a supported extension""" return os.path.splitext(fn)[-1] in [".bb", ".bbclass", ".inc"] -def inherit(files, d): +def inherit(files, fn, lineno, d): __inherit_cache = data.getVar('__inherit_cache', d) or [] - fn = "" - lineno = 0 for file in files: file = data.expand(file, d) if not os.path.isabs(file) and not file.endswith(".bbclass"): @@ -81,7 +79,7 @@ def inherit(files, d): logger.log(logging.DEBUG -1, "BB %s:%d: inheriting %s", fn, lineno, file) __inherit_cache.append( file ) data.setVar('__inherit_cache', __inherit_cache, d) - include(fn, file, d, "inherit") + include(fn, file, lineno, d, "inherit") __inherit_cache = data.getVar('__inherit_cache', d) or [] def get_statements(filename, absolute_filename, base_name): diff --git a/bitbake/lib/bb/parse/parse_py/ConfHandler.py b/bitbake/lib/bb/parse/parse_py/ConfHandler.py index 6ae9d973e7..9242632c50 100644 --- a/bitbake/lib/bb/parse/parse_py/ConfHandler.py +++ b/bitbake/lib/bb/parse/parse_py/ConfHandler.py @@ -44,10 +44,11 @@ def init(data): def supports(fn, d): return fn[-5:] == ".conf" -def include(oldfn, fn, data, error_out): +def include(oldfn, fn, lineno, data, error_out): """ - error_out If True a ParseError will be raised if the to be included - config-files could not be included. + error_out: A string indicating the verb (e.g. "include", "inherit") to be + used in a ParseError that will be raised if the file to be included could + not be included. Specify False to avoid raising an error in this case. """ if oldfn == fn: # prevent infinite recursion return None @@ -68,7 +69,7 @@ def include(oldfn, fn, data, error_out): ret = handle(fn, data, True) except IOError: if error_out: - raise ParseError("Could not %(error_out)s file %(fn)s" % vars() ) + raise ParseError("Could not %(error_out)s file %(fn)s" % vars(), oldfn, lineno) logger.debug(2, "CONF file '%s' not found", fn) def handle(fn, data, include): @@ -131,7 +132,7 @@ def feeder(lineno, s, fn, statements): ast.handleExport(statements, fn, lineno, m) return - raise ParseError("%s:%d: unparsed line: '%s'" % (fn, lineno, s)); + raise ParseError("unparsed line: '%s'" % s, fn, lineno); # Add us to the handlers list from bb.parse import handlers -- cgit v1.2.3-54-g00ecf