]> git.ipfire.org Git - thirdparty/openembedded/openembedded-core-contrib.git/commitdiff
bitbake: data_smart: support serialisation
authorPaul Eggleton <paul.eggleton@linux.intel.com>
Tue, 13 Dec 2016 07:07:11 +0000 (20:07 +1300)
committerRichard Purdie <richard.purdie@linuxfoundation.org>
Wed, 14 Dec 2016 12:25:07 +0000 (12:25 +0000)
The COW object used within VariableHistory can't be serialised itself,
but we can convert it to a dict when serializing and then back when
deserialising. This finally allows DataSmart objects to be serialized.
NOTE: "serialisation" here means pickling, not over XMLRPC or any other
transport.

(Bitbake rev: bbbb2a53d5decf3b613a92c4ff77c84bfc5d4903)

Signed-off-by: Paul Eggleton <paul.eggleton@linux.intel.com>
Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
bitbake/lib/bb/data_smart.py
bitbake/lib/bb/tests/data.py

index 4d0a771283cb0021c6295df3d07714d220c10a54..79d591a2378fb13a409943a111922c81c5e5284f 100644 (file)
@@ -230,6 +230,19 @@ class VariableHistory(object):
         new.variables = self.variables.copy()
         return new
 
+    def __getstate__(self):
+        vardict = {}
+        for k, v in self.variables.iteritems():
+            vardict[k] = v
+        return {'dataroot': self.dataroot,
+                'variables': vardict}
+
+    def __setstate__(self, state):
+        self.dataroot = state['dataroot']
+        self.variables = COWDictBase.copy()
+        for k, v in state['variables'].items():
+            self.variables[k] = v
+
     def record(self, *kwonly, **loginfo):
         if not self.dataroot._tracking:
             return
index a17245f90afa75e9118f1a501567bec7ba662295..ba30d1280652c0813e93697b417931f56573ea2a 100644 (file)
@@ -446,6 +446,29 @@ class Contains(unittest.TestCase):
         self.assertFalse(bb.utils.contains_any("SOMEFLAG", "x y z", True, False, self.d))
 
 
+class Serialize(unittest.TestCase):
+
+    def test_serialize(self):
+        import tempfile
+        import pickle
+        d = bb.data.init()
+        d.enableTracking()
+        d.setVar('HELLO', 'world')
+        d.setVarFlag('HELLO', 'other', 'planet')
+        with tempfile.NamedTemporaryFile(delete=False) as tmpfile:
+            tmpfilename = tmpfile.name
+            pickle.dump(d, tmpfile)
+
+        with open(tmpfilename, 'rb') as f:
+            newd = pickle.load(f)
+
+        os.remove(tmpfilename)
+
+        self.assertEqual(d, newd)
+        self.assertEqual(newd.getVar('HELLO', True), 'world')
+        self.assertEqual(newd.getVarFlag('HELLO', 'other'), 'planet')
+
+
 class Remote(unittest.TestCase):
     def test_remote(self):
         class TestConnector: