lib/bb/parse/parse_c: Throw Parse Exceptions, crash less often
authorHolger Hans Peter Freyther <zecke@selfish.org>
Sun, 23 Jul 2006 21:33:21 +0000 (21:33 +0000)
committerHolger Hans Peter Freyther <zecke@selfish.org>
Sun, 23 Jul 2006 21:33:21 +0000 (21:33 +0000)
    -Make sure TOPDIR is set (do not declare this as an error here)
    -Do not crash on 'NULL' strings
    -Throws exceptions properly using 'except -1' from within our
     cdef.

lib/bb/parse/parse_c/BBHandler.py
lib/bb/parse/parse_c/Makefile
lib/bb/parse/parse_c/bitbakec.pyx
lib/bb/parse/parse_c/bitbakescanner.cc
lib/bb/parse/parse_c/bitbakescanner.l
lib/bb/parse/parse_c/lexer.h
lib/bb/parse/parse_c/lexerc.h

index e15efa9..984ba39 100644 (file)
@@ -31,7 +31,7 @@
        THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 """
 
-import os
+import os, sys
 
 # The Module we will use here
 import bb
@@ -61,10 +61,10 @@ def supports(fn, data):
     return fn[-3:] == ".bb" or fn[-8:] == ".bbclass" or fn[-4:] == ".inc" or fn[-5:] == ".conf"
 
 def init(fn, data):
-    if not data.getVar('TOPDIR', False):
-        bb.error('TOPDIR is not set')
-    if not data.getVar('BBPATH', False):
-        bb.error('BBPATH is not set')
+    if not bb.data.getVar('TOPDIR', data):
+        bb.data.setVar('TOPDIR', os.getcwd(), data)
+    if not bb.data.getVar('BBPATH', data):
+        bb.data.setVar('BBPATH', os.path.join(sys.prefix, 'share', 'bitbake'), data)
 
 
 def handle(fn, d, include):
@@ -75,7 +75,10 @@ def handle(fn, d, include):
     #print "data: %s" % d
     #print dir(d)
     #print d.getVar.__doc__
-    print "include: %s" % fn
+    #print "include: %s" % fn
+
+    # initialize with some data
+    init(fn,d)
 
     # check if we include or are the beginning
     if include:
@@ -108,6 +111,29 @@ def handle(fn, d, include):
 
     return d
 
+
+# Needed for BitBake files...
+__pkgsplit_cache__={}
+def vars_from_file(mypkg, d):
+    if not mypkg:
+        return (None, None, None)
+    if mypkg in __pkgsplit_cache__:
+        return __pkgsplit_cache__[mypkg]
+
+    myfile = os.path.splitext(os.path.basename(mypkg))
+    parts = myfile[0].split('_')
+    __pkgsplit_cache__[mypkg] = parts
+    exp = 3 - len(parts)
+    tmplist = []
+    while exp != 0:
+        exp -= 1
+        tmplist.append(None)
+    parts.extend(tmplist)
+    return parts
+
+
+
+
 # Inform bitbake that we are a parser
 # We need to define all three
 from bb.parse import handlers
index 741f74c..77daccb 100644 (file)
@@ -1,6 +1,6 @@
 
-test: bitbakec.so
-       python test.py
+buil: bitbakec.so
+       echo "Done"
 
 bitbakescanner.cc: bitbakescanner.l
        flex -t bitbakescanner.l > bitbakescanner.cc
index b26ebce..1070e27 100644 (file)
@@ -13,13 +13,14 @@ cdef extern from "lexerc.h":
     ctypedef struct lex_t:
         void* parser
         void* scanner
+        char* name
         FILE* file
         void* data
 
     int lineError
     int errorParse
 
-    cdef extern void parse(FILE*, object)
+    cdef extern int parse(FILE*, char*, object)
 
 def parsefile(object file, object data):
     #print "parsefile: 1", file, data
@@ -33,15 +34,18 @@ def parsefile(object file, object data):
         raise IOError("No such file %s." % file)
 
     #print "parsefile: 3 parse"
-    parse(f, data)
+    parse(f, file, data)
 
     # Close the file
-    #print "parsefile: 4 closing"
     fclose(f)
 
 
 cdef public void e_assign(lex_t* container, char* key, char* what):
     #print "e_assign", key, what
+    if what == NULL:
+        print "FUTURE Warning empty string: use \"\""
+        what = ""
+
     d = <object>container.data
     d.setVar(key, what)
 
@@ -57,7 +61,7 @@ cdef public void e_immediate(lex_t* c, char* key, char* what):
     #colon:
     # val = bb.data.expand(groupd["value"], data)
     d = <object>c.data
-    d.setVar(key, d.expand(what,None))
+    d.setVar(key, d.expand(what,d))
 
 cdef public void e_cond(lex_t* c, char* key, char* what):
     #print "e_cond", key, what
@@ -65,8 +69,12 @@ cdef public void e_cond(lex_t* c, char* key, char* what):
     # val = bb.data.getVar(key, data)
     # if val == None:    
     #    val = groupd["value"]
+    if what == NULL:
+        print "FUTURE warning: Use \"\" for", key
+        what = ""
+
     d = <object>c.data
-    d.setVar(key, (d.getVar(key,0) or what))
+    d.setVar(key, (d.getVar(key,False) or what))
 
 cdef public void e_prepend(lex_t* c, char* key, char* what):
     #print "e_prepend", key, what
@@ -151,7 +159,7 @@ cdef public void e_include(lex_t* c, char* file):
         print "Could not include file", file
 
 
-cdef public void e_require(lex_t* c, char* file):
+cdef public int e_require(lex_t* c, char* file) except -1:
     #print "e_require", file
     from bb.parse import handle
     d = <object>c.data
@@ -162,6 +170,7 @@ cdef public void e_require(lex_t* c, char* file):
         print "ParseError", file
         from bb.parse import ParseError
         raise ParseError("Could not include required file %s" % file)
+        return -1
 
 cdef public void e_proc(lex_t* c, char* key, char* what):
     #print "e_proc", key, what
@@ -185,10 +194,11 @@ cdef public void e_def(lex_t* c, char* a, char* b, char* d):
     #print "e_def", a, b, d
     pass
 
-cdef public void e_parse_error(lex_t* c):
-    print "e_parse_error", "line:", lineError, "parse:", errorParse
+cdef public int e_parse_error(lex_t* c) except -1:
+    print "e_parse_error", c.name, "line:", lineError, "parse:", errorParse
 
 
     from bb.parse import ParseError
-    raise ParseError("There was an parse error, sorry unable to give more information at the current time.")
+    raise ParseError("There was an parse error, sorry unable to give more information at the current time. File: %s Line: %d" % (c.name,lineError) )
+    return -1
 
index 1aeca06..b350152 100644 (file)
@@ -3162,7 +3162,7 @@ int lex_t::line ()const
 
 extern "C" {
 
-    void parse (FILE* file, PyObject* data)
+    void parse (FILE* file, char* name, PyObject* data)
     {
         /* printf("parse bbparseAlloc\n"); */
         void* parser = bbparseAlloc (malloc);
@@ -3175,6 +3175,7 @@ extern "C" {
         lex.parser = parser;
         lex.scanner = scanner;
         lex.file = file;
+        lex.name = name;
         lex.data = data;
         lex.parse = bbparse;
         /*printf("parse yyset_extra\n"); */
index 667f260..aadfb2e 100644 (file)
@@ -279,7 +279,7 @@ int lex_t::line ()const
 
 extern "C" {
 
-    void parse (FILE* file, PyObject* data)
+    void parse (FILE* file, char* name, PyObject* data)
     {
         /* printf("parse bbparseAlloc\n"); */
         void* parser = bbparseAlloc (malloc);
@@ -292,6 +292,7 @@ extern "C" {
         lex.parser = parser;
         lex.scanner = scanner;
         lex.file = file;
+        lex.name = name;
         lex.data = data;
         lex.parse = bbparse;
         /*printf("parse yyset_extra\n"); */
index 651f3a8..91cd1c0 100644 (file)
@@ -27,13 +27,14 @@ THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 #include "Python.h"
 
 extern "C" {
-       
+
 struct lex_t {
     void* parser;
     void* scanner;
-    FILE* file;    
+    FILE* file;
+    char *name;
     PyObject *data;
-    
+
     void* (*parse)(void*, int, token_t, lex_t*);
 
     void accept(int token, const char* sz = NULL);
index 0163a7d..de91f51 100644 (file)
@@ -11,6 +11,7 @@ typedef struct {
     void *parser;
     void *scanner;
     FILE *file;
+    char *name;
     PyObject *data;
 } lex_t;