[PATCH 1/3] cross-answers file - use internal dictionary

Uri Simchoni urisimchoni at gmail.com
Thu Apr 30 14:21:48 MDT 2015


This change refactors the cross-answers cross compile method, to load
the cross-answers file once, maintain an internal dictionary of tests
and their answers, and, if unknown answers are detected, write the
modified dictionary back when configure terminates.

There is no change of functionality, just different implementation
that would better accomodate adding the test program names for each
test with unknown results.
---
 buildtools/wafsamba/samba_cross.py | 70 ++++++++++++++++++++------------------
 1 file changed, 36 insertions(+), 34 deletions(-)

diff --git a/buildtools/wafsamba/samba_cross.py b/buildtools/wafsamba/samba_cross.py
index 3838e34..4d80dd5 100644
--- a/buildtools/wafsamba/samba_cross.py
+++ b/buildtools/wafsamba/samba_cross.py
@@ -10,34 +10,31 @@ ANSWER_FAIL    = (255, "")
 ANSWER_OK      = (0, "")
 
 cross_answers_incomplete = False
+cross_answers = None
 
+def write_cross_answers(ca_file):
+    '''write answers file'''
+    lines = []
+    for msg, (ans,anstext) in cross_answers.iteritems():
+        lines.append('%s: %s\n' % (msg, anstext))
 
-def add_answer(ca_file, msg, answer):
-    '''add an answer to a set of cross answers'''
     try:
-        f = open(ca_file, 'a')
+        f = open(ca_file, 'w')
     except:
         Logs.error("Unable to open cross-answers file %s" % ca_file)
         sys.exit(1)
-    if answer == ANSWER_OK:
-        f.write('%s: OK\n' % msg)
-    elif answer == ANSWER_UNKNOWN:
-        f.write('%s: UNKNOWN\n' % msg)
-    elif answer == ANSWER_FAIL:
-        f.write('%s: FAIL\n' % msg)
-    else:
-        (retcode, retstring) = answer
-        f.write('%s: (%d, "%s")' % (msg, retcode, retstring))
-    f.close()
 
+    f.write(''.join(sorted(lines)))
+    f.close()
 
-def cross_answer(ca_file, msg):
-    '''return a (retcode,retstring) tuple from a answers file'''
+def init_cross_answers(ca_file):
+    '''read answers file'''
+    global cross_answers
+    cross_answers = {}
     try:
         f = open(ca_file, 'r')
     except:
-        add_answer(ca_file, msg, ANSWER_UNKNOWN)
-        return ANSWER_UNKNOWN
+        return
     for line in f:
         line = line.strip()
         if line == '' or line[0] == '#':
@@ -45,31 +42,34 @@ def cross_answer(ca_file, msg):
         if line.find(':') != -1:
             a = line.split(':')
             thismsg = a[0].strip()
-            if thismsg != msg:
-                continue
             ans = a[1].strip()
             if ans == "OK" or ans == "YES":
-                f.close()
-                return ANSWER_OK
+                thisans = ANSWER_OK
             elif ans == "UNKNOWN":
-                f.close()
-                return ANSWER_UNKNOWN
+                continue
             elif ans == "FAIL" or ans == "NO":
-                f.close()
-                return ANSWER_FAIL
+                thisans = ANSWER_FAIL
             elif ans[0] == '"':
-                return (0, ans.strip('"'))
+                thisans = (0, ans.strip('"'))
             elif ans[0] == "'":
-                return (0, ans.strip("'"))
+                thisans = (0, ans.strip("'"))
             else:
                 m = re.match('\(\s*(-?\d+)\s*,\s*\"(.*)\"\s*\)', ans)
                 if m:
-                    f.close()
-                    return (int(m.group(1)), m.group(2))
+                    thisans = (int(m.group(1)), m.group(2))
                 else:
                     raise Utils.WafError("Bad answer format '%s' in %s" % (line, ca_file))
+            cross_answers[thismsg] = (thisans, ans)
+
     f.close()
-    add_answer(ca_file, msg, ANSWER_UNKNOWN)
+
+def cross_answer(msg):
+    '''retrieves (retcode, retstring) cross-answer, if unknown then update
+       the cross-answers dictionary'''
+    if msg in cross_answers:
+        ans, anstext = cross_answers[msg]
+        return ans
+    cross_answers[msg] = (ANSWER_UNKNOWN, "UNKNOWN")
     return ANSWER_UNKNOWN
 
 
@@ -89,9 +89,8 @@ class cross_Popen(Utils.pproc.Popen):
             # when --cross-answers is set, then change the arguments
             # to use the cross answers if available
             i = args.index('--cross-answers')
-            ca_file = args[i+1]
-            msg     = args[i+2]
-            ans = cross_answer(ca_file, msg)
+            msg = args[i+1]
+            ans = cross_answer(msg)
             if ans == ANSWER_UNKNOWN:
                 global cross_answers_incomplete
                 cross_answers_incomplete = True
@@ -118,7 +117,9 @@ def SAMBA_CROSS_ARGS(conf, msg=None):
     elif conf.env.CROSS_ANSWERS:
         if msg is None:
             raise Utils.WafError("Cannot have NULL msg in cross-answers")
-        ret.extend(['--cross-answers', os.path.join(Options.launch_dir, conf.env.CROSS_ANSWERS), msg])
+        if cross_answers is None:
+            init_cross_answers(os.path.join(Options.launch_dir, conf.env.CROSS_ANSWERS))
+        ret.extend(['--cross-answers', msg])
 
     if ret == []:
         raise Utils.WafError("Cannot cross-compile without either --cross-execute or --cross-answers")
@@ -130,5 +131,6 @@ def SAMBA_CROSS_CHECK_COMPLETE(conf):
     '''check if we have some unanswered questions'''
     global cross_answers_incomplete
     if conf.env.CROSS_COMPILE and cross_answers_incomplete:
+        write_cross_answers(os.path.join(Options.launch_dir, conf.env.CROSS_ANSWERS))
         raise Utils.WafError("Cross answers file %s is incomplete" % conf.env.CROSS_ANSWERS)
     return True
-- 
1.9.1



More information about the samba-technical mailing list