[oe-commits] [bitbake] 12/15: data_smart: support serialisation

git at git.openembedded.org git at git.openembedded.org
Wed Dec 14 09:57:53 UTC 2016


rpurdie pushed a commit to branch master-next
in repository bitbake.

commit bbbb2a53d5decf3b613a92c4ff77c84bfc5d4903
Author: Paul Eggleton <paul.eggleton at linux.intel.com>
AuthorDate: Tue Dec 13 20:07:11 2016 +1300

    data_smart: support serialisation
    
    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.
    
    Signed-off-by: Paul Eggleton <paul.eggleton at linux.intel.com>
    Signed-off-by: Richard Purdie <richard.purdie at linuxfoundation.org>
---
 lib/bb/data_smart.py | 13 +++++++++++++
 lib/bb/tests/data.py | 23 +++++++++++++++++++++++
 2 files changed, 36 insertions(+)

diff --git a/lib/bb/data_smart.py b/lib/bb/data_smart.py
index 4d0a771..79d591a 100644
--- a/lib/bb/data_smart.py
+++ b/lib/bb/data_smart.py
@@ -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
diff --git a/lib/bb/tests/data.py b/lib/bb/tests/data.py
index a17245f..ba30d12 100644
--- a/lib/bb/tests/data.py
+++ b/lib/bb/tests/data.py
@@ -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:

-- 
To stop receiving notification emails like this one, please contact
the administrator of this repository.


More information about the Openembedded-commits mailing list