Interface between tko parse and whatever the other client is for shipping out data.
OK to submit to non-main branch.

Signed-off-by: Darren Kuo <darrenkuo@google.com>



git-svn-id: http://test.kernel.org/svn/autotest/trunk@4598 592f7852-d20e-0410-864c-8624ca9c26a4
diff --git a/tko/job_serializer.py b/tko/job_serializer.py
new file mode 100644
index 0000000..55c1169
--- /dev/null
+++ b/tko/job_serializer.py
@@ -0,0 +1,451 @@
+#!/usr/bin/python
+
+"""A script that provides convertion between models.job and a protocol
+buffer object.
+
+This script contains only one class that takes an job instance and
+convert it into a protocol buffer object. The class will also be
+responsible for serializing the job instance via protocol buffers.
+
+"""
+
+# import python libraries
+import os
+import datetime
+import time
+import random
+
+# import autotest libraries
+from autotest_lib.tko import models
+from autotest_lib.tko import tko_pb2
+
+__author__ = 'darrenkuo@google.com (Darren Kuo)'
+
+mktime = time.mktime
+datetime = datetime.datetime
+
+class JobSerializer(object):
+    """A class that takes a job object of the tko module and package
+    it with a protocol buffer.
+
+    This class will take a model.job object as input and create a
+    protocol buffer to include all the content of the job object. This
+    protocol buffer object will be serialized into a binary file.
+    """
+
+    def __init__(self):
+
+        self.job_type_dict = {'dir':str, 'tests':list, 'user':str,
+                              'label':str, 'machine':str,
+                              'queued_time':datetime,
+                              'started_time':datetime,
+                              'finished_time':datetime,
+                              'machine_owner':str,
+                              'machine_group':str, 'aborted_by':str,
+                              'aborted_on':datetime,
+                              'keyval_dict':dict}
+
+        self.test_type_dict = {'subdir':str, 'testname':str,
+                               'status':str, 'reason':str,
+                               'kernel':models.kernel, 'machine':str,
+                               'started_time':datetime,
+                               'finished_time':datetime,
+                               'iterations':list, 'attributes':dict,
+                               'labels':list}
+
+        self.kernel_type_dict = {'base':str, 'patches':list,
+                                 'kernel_hash':str }
+
+        self.iteration_type_dict = {'index':int, 'attr_keyval':dict,
+                                    'perf_keyval':dict }
+
+        self.patch_type_dict = {'spec':str, 'reference':str,
+                                'hash':int}
+
+
+    def deserialize_from_binary(self, infile):
+        """Takes in a binary file name and returns a tko job object.
+
+        The method first deserialize the binary into a protocol buffer
+        job object and then converts the job object into a tko job
+        object.
+
+        @param
+        infile: the name of the binary file that will be deserialized.
+
+        @return a tko job that is represented by the binary file will
+        be returned.
+        """
+
+        job_pb = tko_pb2.job()
+
+        binary = open(infile, 'r')
+        try:
+            job_pb.ParseFromString(binary.read())
+        finally:
+            binary.close()
+
+        return self.get_tko_job(job_pb)
+
+
+    def serialize_to_binary(self, the_job, binaryfilename):
+        """Serializes the tko job object into a binary by using a
+        protocol buffer.
+
+        The method takes a tko job object and constructs a protocol
+        buffer job object. Then invokes the native serializing
+        function on the object to get a binary string. The string is
+        then written to outfile.
+
+        Precondition: Assumes that all the information about the job
+        is already in the job object. Any fields that is None will be
+        provided a default value.
+
+        @param
+        the_job: the tko job object that will be serialized.
+        binaryfilename: the name of the file that will be written to
+
+        @return the filename of the file that contains the
+        binary of the serialized object.
+        """
+
+        job_pb = tko_pb2.job()
+        self.set_pb_job(the_job, job_pb)
+
+        out = open(binaryfilename, 'wb')
+        try:
+            out.write(job_pb.SerializeToString())
+        finally:
+            out.close()
+
+
+    # getter setter methods
+    def get_tko_job(self, job):
+        """Creates a a new tko job object from the pb job object.
+
+        Uses getter methods on the pb objects to extract all the
+        attributes and finally constructs a tko job object using the
+        models.job constructor.
+
+        @param
+        job: a pb job where data is being extracted from.
+
+        @return a tko job object.
+        """
+
+        fields_dict = self.get_trivial_attr(job, self.job_type_dict)
+
+        fields_dict['tests'] = [self.get_tko_test(test) for test in job.tests]
+
+        fields_dict['keyval_dict'] = dict((keyval.name, keyval.value)
+                                          for keyval in job.keyval_dict)
+
+        newjob = models.job(fields_dict['dir'], fields_dict['user'],
+                            fields_dict['label'],
+                            fields_dict['machine'],
+                            fields_dict['queued_time'],
+                            fields_dict['started_time'],
+                            fields_dict['finished_time'],
+                            fields_dict['machine_owner'],
+                            fields_dict['machine_group'],
+                            fields_dict['aborted_by'],
+                            fields_dict['aborted_on'],
+                            fields_dict['keyval_dict'])
+
+        newjob.tests.extend(fields_dict['tests'])
+
+        return newjob
+
+
+    def set_pb_job(self, tko_job, pb_job):
+        """Set the fields for the new job object.
+
+        Method takes in a tko job and an empty protocol buffer job
+        object.  Then safely sets all the appropriate field by first
+        testing if the value in the original object is None.
+
+        @param
+        tko_job: a tko job instance that will have it's values
+        transfered to the new job
+        pb_job: a new instance of the job class provided in the
+        protocol buffer.
+
+        """
+
+        self.set_trivial_attr(tko_job, pb_job, self.job_type_dict)
+
+        for test in tko_job.tests:
+            newtest = pb_job.tests.add()
+            self.set_pb_test(test, newtest)
+
+        for key, val in tko_job.keyval_dict.iteritems():
+            newkeyval = pb_job.keyval_dict.add()
+            newkeyval.name = key
+            newkeyval.value = str(val)
+
+
+    def get_tko_test(self, test):
+        fields_dict = self.get_trivial_attr(test, self.test_type_dict)
+
+        fields_dict['kernel'] = self.get_tko_kernel(test.kernel)
+
+        fields_dict['iterations'] = [self.get_tko_iteration(iteration)
+                                     for iteration in test.iterations]
+
+        fields_dict['attributes'] = dict((keyval.name, keyval.value)
+                                         for keyval in test.attributes)
+
+        fields_dict['labels'] = list(test.labels)
+
+        return models.test(fields_dict['subdir'],
+                           fields_dict['testname'],
+                           fields_dict['status'],
+                           fields_dict['reason'],
+                           fields_dict['kernel'],
+                           fields_dict['machine'],
+                           fields_dict['started_time'],
+                           fields_dict['finished_time'],
+                           fields_dict['iterations'],
+                           fields_dict['attributes'],
+                           fields_dict['labels'])
+
+
+    def set_pb_test(self, tko_test, pb_test):
+        """Sets the various fields of test object of the tko protocol.
+
+        Method takes a tko test and a new test of the protocol buffer and
+        transfers the values in the tko test to the new test.
+
+        @param
+        tko_test: a tko test instance.
+        pb_test: an empty protocol buffer test instance.
+
+        """
+
+        self.set_trivial_attr(tko_test, pb_test, self.test_type_dict)
+
+        self.set_pb_kernel(tko_test.kernel, pb_test.kernel)
+
+        for current_iteration in tko_test.iterations:
+            pb_iteration = pb_test.iterations.add()
+            self.set_pb_iteration(current_iteration, pb_iteration)
+
+        for key, val in tko_test.attributes.iteritems():
+            newkeyval = pb_test.attributes.add()
+            newkeyval.name = key
+            newkeyval.value = str(val)
+
+        for current_label in tko_test.labels:
+            pb_test.labels.append(current_label)
+
+
+    def get_tko_kernel(self, kernel):
+        """Constructs a new tko kernel object from a pb kernel object.
+
+        Uses all the getter methods on the pb kernel object to extract
+        the attributes and constructs a new tko kernel object using
+        the model.kernel constructor.
+
+        @param
+        kernel: a pb kernel object where data will be extracted.
+
+        @return a new tko kernel object.
+        """
+
+        fields_dict = self.get_trivial_attr(kernel, self.kernel_type_dict)
+
+        fields_dict['patches'] = [self.get_tko_patch(patch) for patch
+                                  in kernel.patches]
+
+        return models.kernel(fields_dict['base'], fields_dict['patches'],
+                             fields_dict['kernel_hash'])
+
+
+    def set_pb_kernel(self, tko_kernel, pb_kernel):
+        """Set a specific kernel of a test.
+
+        Takes the same form of all the other setting methods.  It
+        seperates the string variables from the int variables and set
+        them safely.
+
+        @param
+        tko_kernel: a tko kernel.
+        pb_kernel: an empty protocol buffer kernel.
+
+        """
+
+        self.set_trivial_attr(tko_kernel, pb_kernel, self.kernel_type_dict)
+
+        for patch in tko_kernel.patches:
+            newpatch = pb_kernel.patches.add()
+            self.set_pb_patch(patch, newpatch)
+
+
+    def get_tko_patch(self, patch):
+        """Constructs a new tko patch object from the provided pb
+        patch instance.
+
+        Extracts data from the provided pb patch and creates a new tko
+        patch using the models.patch constructor.
+
+        @param
+        patch: a pb patch that contains the data for the new tko patch
+
+        @return a new tko patch with the same data as in the pb patch.
+        """
+
+        fields_dict = self.get_trivial_attr(patch, self.patch_type_dict)
+        return models.patch(fields_dict['spec'],
+                            fields_dict['reference'],
+                            fields_dict['hash'])
+
+
+    def set_pb_patch(self, tko_patch, pb_patch):
+        """Set a specific patch of a kernel.
+
+        Takes the same form of all the other setting methods.  It
+        seperates the string variables from the int variables and set
+        them safely.
+
+        @param
+        tko_patch: a tko patch.
+        pb_patch: an empty protocol buffer patch.
+
+        """
+
+        self.set_trivial_attr(tko_patch, pb_patch, self.patch_type_dict)
+
+
+    def get_tko_iteration(self, iteration):
+        """Creates a new tko iteration with the data in the provided
+        pb iteration.
+
+        Uses the data in the pb iteration and the models.iteration
+        constructor to create a new tko iterations
+
+        @param
+        iteration: a pb iteration instance
+
+        @return a tko iteration instance with the same data.
+        """
+
+        fields_dict = self.get_trivial_attr(iteration,
+                                            self.iteration_type_dict)
+
+        fields_dict['attr_keyval'] = dict((keyval.name, keyval.value)
+                                          for keyval in iteration.attr_keyval)
+
+        fields_dict['perf_keyval'] = dict((keyval.name, keyval.value)
+                                          for keyval in iteration.perf_keyval)
+
+        return models.iteration(fields_dict['index'],
+                                fields_dict['attr_keyval'],
+                                fields_dict['perf_keyval'])
+
+
+    def set_pb_iteration(self, tko_iteration, pb_iteration):
+        """Sets all fields for a particular iteration.
+
+        Takes same form as all the other setting methods. Sets int,
+        str and datetime variables safely.
+
+        @param
+        tko_iteration: a tko test iteration.
+        pb_iteration: an empty pb test iteration.
+
+        """
+
+        self.set_trivial_attr(tko_iteration, pb_iteration,
+                              self.iteration_type_dict)
+
+        for key, val in tko_iteration.attr_keyval.iteritems():
+            newkeyval = pb_iteration.attr_keyval.add()
+            newkeyval.name = key
+            newkeyval.value = str(val)
+
+        for key, val in tko_iteration.perf_keyval.iteritems():
+            newkeyval = pb_iteration.perf_keyval.add()
+            newkeyval.name = key
+            newkeyval.value = str(val)
+
+
+    def get_trivial_attr(self, obj, objdict):
+        """Get all trivial attributes from the object.
+
+        This function is used to extract attributes from a pb job. The
+        dictionary specifies the types of each attribute in each tko
+        class.
+
+        @param
+        obj: the pb object that is being extracted.
+        objdict: the dict that specifies the type.
+
+        @return a dict of each attr name and it's corresponding value.
+        """
+
+        resultdict = {}
+        for field, field_type in objdict.items():
+            value = getattr(obj, field)
+            if field_type in (str, int, long):
+                resultdict[field] = field_type(value)
+            elif field_type == datetime:
+                resultdict[field] = (
+                            datetime.fromtimestamp(value/1000.0))
+
+        return resultdict
+
+
+    def set_trivial_attr(self, tko_obj, pb_obj, objdict):
+        """Sets all the easy attributes appropriately according to the
+        type.
+
+        This function is used to set all the trivial attributes
+        provided by objdict, the dictionary that specifies the types
+        of each attribute in each tko class.
+
+        @param
+        tko_obj: the original object that has the data being copied.
+        pb_obj: the new pb object that is being copied into.
+        objdict: specifies the type of each attribute in the class we
+        are working with.
+
+        """
+        for attr, attr_type in objdict.iteritems():
+            if attr_type == datetime:
+                t = getattr(tko_obj, attr)
+                if not t:
+                    self.set_attr_safely(pb_obj, attr, t, int)
+                else:
+                    t = mktime(t.timetuple()) + 1e-6 * t.microsecond
+                    setattr(pb_obj, attr, long(t*1000))
+            else:
+                value = getattr(tko_obj, attr)
+                self.set_attr_safely(pb_obj, attr, value, attr_type)
+
+
+    def set_attr_safely(self, var, attr, value, vartype):
+        """Sets a particular attribute of var if the provided value is
+        not None.
+
+        Checks if value is None. If not, set the attribute of the var
+        to be the default value. This is necessary for the special
+        required fields of the protocol buffer.
+
+        @param
+        var: the variable of which one of the attribute is being set.
+        attr: the attribute that is being set.
+        value: the value that is being checked
+        vartype: the expected type of the attr
+
+        """
+
+        supported_types = [int, long, str]
+        if vartype in supported_types:
+            if value is None:
+                value = vartype()
+            else:
+                assert isinstance(value, vartype), (
+                'Unexpected type %s for attr %s, should be %s' %
+                (type(value), attr, vartype))
+
+            setattr(var, attr, value)
diff --git a/tko/job_serializer_unittest.py b/tko/job_serializer_unittest.py
new file mode 100644
index 0000000..368e584
--- /dev/null
+++ b/tko/job_serializer_unittest.py
@@ -0,0 +1,331 @@
+#!/usr/bin/python
+
+"""Unittests for the JobSerializer class.
+
+Mostly test if the serialized object has the expected content.
+
+"""
+
+import common
+import time
+import os
+import datetime
+import tempfile
+
+from autotest_lib.tko import tko_pb2
+from autotest_lib.tko import job_serializer
+from autotest_lib.tko import models
+from autotest_lib.client.common_lib.test_utils import unittest
+
+NamedTemporaryFile = tempfile.NamedTemporaryFile
+datetime = datetime.datetime
+mktime = time.mktime
+
+class JobSerializerUnittest(unittest.TestCase):
+    """Base class as a job serializer unittest"""
+
+    def setUp(self):
+        tko_patches = []
+        tko_patches.append(models.patch('New spec!', 'Reference?',
+                                        123456))
+
+        tko_kernel = models.kernel('tubes', tko_patches, '1234567')
+        tko_time = datetime.now()
+
+        tko_job = models.job('/tmp/', 'root', 'test', 'My Computer',
+                             tko_time, tko_time, tko_time, 'root',
+                             'www', 'No one', tko_time, {'1+1':2})
+
+        tko_iteration = models.iteration(0, {'2+2':4, '3+3':6},
+                                   {'4+4':8, '5+5':10, '6+6':12})
+
+        tko_labels = ['unittest', 'dummy test', 'autotest']
+
+        tko_test = models.test('/tmp/', 'mocktest', 'PASS', 'N/A',
+                               tko_kernel, 'My Computer', tko_time,
+                               tko_time, [tko_iteration,
+                               tko_iteration, tko_iteration],
+                               {'abc':'def'}, tko_labels)
+
+        self.tko_job = tko_job
+        self.tko_job.tests = [tko_test, tko_test, tko_test]
+
+        self.pb_job = tko_pb2.job()
+        js = job_serializer.JobSerializer()
+        js.set_pb_job(self.tko_job, self.pb_job)
+
+
+    def test_job_dir(self):
+        """Check if the dir field are the same.
+        """
+        self.assertEqual(self.tko_job.dir, self.pb_job.dir)
+
+
+    def test_number_of_test(self):
+        """Check if the number of test are the same.
+        """
+        self.assertEqual(len(self.tko_job.tests),
+                         len(self.pb_job.tests))
+
+
+    def test_user(self):
+        """Check if the user field are the same.
+        """
+        self.assertEqual(self.tko_job.user, self.pb_job.user)
+
+
+    def test_machine(self):
+        """Check if the machine fields are the same.
+        """
+        self.assertEqual(self.tko_job.machine, self.pb_job.machine)
+
+
+    def test_queued_time(self):
+        """Check if queued_time are the same.
+        """
+        self.check_time(self.tko_job.queued_time,
+                        self.pb_job.queued_time)
+
+
+    def test_started_time(self):
+        """Check if the started_time are the same.
+        """
+        self.check_time(self.tko_job.started_time,
+                        self.pb_job.started_time)
+
+
+    def test_finished_time(self):
+        """Check if the finished_time are the same.
+        """
+        self.check_time(self.tko_job.finished_time,
+                        self.pb_job.finished_time)
+
+
+    def test_machine_owner(self):
+        """Check if the machine owners are the same.
+        """
+        self.assertEqual(self.tko_job.machine_owner,
+                         self.pb_job.machine_owner)
+
+
+    def test_machine_group(self):
+        """Check if the machine groups are the same.
+        """
+        self.assertEqual(self.tko_job.machine_group,
+                         self.pb_job.machine_group)
+
+    def test_aborted_by(self):
+        """Check if the jobs are aborted by the same person.
+        """
+        self.assertEqual(self.tko_job.aborted_by,
+                         self.pb_job.aborted_by)
+
+
+    def test_aborted_on(self):
+        self.check_time(self.tko_job.aborted_on,
+                        self.pb_job.aborted_on)
+
+
+    def test_keyval_dict(self):
+        """Check if the contents of the dictionary are the same.
+        """
+        self.assertEqual(len(self.tko_job.keyval_dict),
+                         len(self.pb_job.keyval_dict))
+        self.check_dict(self.tko_job.keyval_dict,
+                        self.convert_keyval_to_dict(self.pb_job,
+                        'keyval_dict'))
+
+
+    def test_tests(self):
+        """Check if all the test are the same.
+        """
+
+        for test, newtest in zip(self.tko_job.tests,
+                                 self.pb_job.tests):
+
+            self.assertEqual(test.subdir, newtest.subdir)
+            self.assertEqual(test.testname, newtest.testname)
+            self.assertEqual(test.status, newtest.status)
+            self.assertEqual(test.reason, newtest.reason)
+            self.assertEqual(test.machine, newtest.machine)
+            self.assertEqual(test.labels, newtest.labels)
+
+            self.check_time(test.started_time, newtest.started_time)
+            self.check_time(test.finished_time, newtest.finished_time)
+
+            self.check_iteration(test.iterations, newtest.iterations)
+
+            self.check_dict(test.attributes,
+                            self.convert_keyval_to_dict(newtest,
+                            'attributes'))
+
+            self.check_kernel(test.kernel, newtest.kernel)
+
+
+    def check_time(self, dTime, stime):
+        """Check if the datetime object contains the same time value
+        in microseconds.
+        """
+        t = mktime(dTime.timetuple()) + 1e-6 * dTime.microsecond
+        self.assertEqual(long(t), stime/1000)
+
+
+    def check_iteration(self, tko_iterations, pb_iterations):
+        """Check if the iteration objects are the same.
+        """
+        for tko_iteration, pb_iteration in zip(tko_iterations,
+                                               pb_iterations):
+
+            self.assertEqual(tko_iteration.index, pb_iteration.index)
+
+            self.check_dict(tko_iteration.attr_keyval,
+                            self.convert_keyval_to_dict(pb_iteration,
+                                                        'attr_keyval'))
+
+            self.check_dict(tko_iteration.perf_keyval,
+                            self.convert_keyval_to_dict(pb_iteration,
+                                                        'perf_keyval'))
+
+
+    def convert_keyval_to_dict(self, var, attr):
+        """Convert a protocol buffer repeated keyval object into a
+        python dict.
+        """
+
+        return dict((keyval.name, keyval.value) for keyval in
+                    getattr(var,attr))
+
+
+    def check_dict(self, dictionary, keyval):
+        """Check if the contents of the dictionary are the same as a
+        repeated keyval pair.
+        """
+        for key, value in dictionary.iteritems():
+            self.assertTrue(key in keyval);
+            self.assertEqual(str(value), keyval[key])
+
+
+    def check_kernel(self, kernel, newkernel):
+        """Check if the kernels are the same.
+        """
+
+        self.assertEqual(kernel.base, newkernel.base)
+        self.assertEqual(kernel.kernel_hash, newkernel.kernel_hash)
+
+        self.check_patches(kernel.patches, newkernel.patches)
+
+
+    def check_patches(self, patches, newpatches):
+        for patch, newpatch in zip(patches, newpatches):
+            self.assertEqual(patch.spec, newpatch.spec)
+            self.assertEqual(patch.reference, newpatch.reference)
+            self.assertEqual(patch.hash, newpatch.hash)
+
+
+class ReadBackTest(JobSerializerUnittest):
+    """Check if convert between models.job and pb job is correct even
+    after being written to binary and read by manually
+    """
+
+    def setUp(self):
+        super(ReadBackTest, self).setUp()
+
+        out_binary = NamedTemporaryFile(mode='wb')
+        try:
+            out_binary.write(self.pb_job.SerializeToString())
+            out_binary.flush()
+
+            binary = open(out_binary.name, 'rb')
+            try:
+                self.pb_job = tko_pb2.job()
+                self.pb_job.ParseFromString(binary.read())
+            finally:
+                binary.close()
+        finally:
+            out_binary.close()
+
+
+class ReadBackGetterTest(JobSerializerUnittest):
+    """Check if convert between models.job and pb job is correct after
+    using the getter methods in JobSerializer to read back the
+    data.
+    """
+
+    def setUp(self):
+        super(ReadBackGetterTest, self).setUp()
+
+        temp_binary = NamedTemporaryFile(mode='wb')
+        try:
+            temp_binary.write(self.pb_job.SerializeToString())
+            temp_binary.flush()
+
+            js = job_serializer.JobSerializer()
+            self.from_pb_job = js.deserialize_from_binary(temp_binary.name)
+        finally:
+            temp_binary.close()
+
+
+    def test_keyval_dict(self):
+        """Check if the contents of the dictionary are the same.  """
+
+        self.assertEqual(len(self.tko_job.keyval_dict),
+                         len(self.from_pb_job.keyval_dict))
+
+        self.check_dict(self.tko_job.keyval_dict,
+                        self.from_pb_job.keyval_dict)
+
+
+    def test_tests(self):
+        """Check if all the test are the same.
+        """
+        for test, newtest in zip(self.tko_job.tests,
+                                 self.from_pb_job.tests):
+
+            self.assertEqual(test.subdir, newtest.subdir)
+            self.assertEqual(test.testname, newtest.testname)
+            self.assertEqual(test.status, newtest.status)
+            self.assertEqual(test.reason, newtest.reason)
+            self.assertEqual(test.machine, newtest.machine)
+            self.assertEqual(test.labels, newtest.labels)
+
+            self.check_time(test.started_time, newtest.started_time)
+            self.check_time(test.finished_time, newtest.finished_time)
+
+            self.check_iteration(test.iterations, newtest.iterations)
+
+            self.check_dict(test.attributes, newtest.attributes)
+
+            self.check_kernel(test.kernel, newtest.kernel)
+
+
+    def check_time(self, dTime, sTime):
+        """Check if the datetime object contains the same time value
+        in microseconds.
+
+        If sTime is type int or type long, then only convert dTime to
+        microseconds. Else, convert both dTime and sTime to
+        microseconds. Then, compare the two after casting them to
+        long.
+        """
+
+        t = mktime(dTime.timetuple()) + 1e-6 * dTime.microsecond
+        if isinstance(sTime, (int, long)):
+            self.assertEqual(long(t*1000), sTime)
+        else:
+            t1 = mktime(sTime.timetuple()) + 1e-6 * sTime.microsecond
+            self.assertEqual(long(t*1000), long(t1*1000))
+
+
+    def check_iteration(self, iterations, newiterations):
+        """Check if the iteration objects are the same.
+        """
+        for iteration, newiteration in zip(iterations, newiterations):
+            self.assertEqual(iteration.index, newiteration.index)
+            self.check_dict(iteration.attr_keyval,
+                            newiteration.attr_keyval)
+            self.check_dict(iteration.perf_keyval,
+                            newiteration.perf_keyval)
+
+
+if __name__ == '__main__':
+    unittest.main()
diff --git a/tko/parse.py b/tko/parse.py
index 640c02e..6f8bba9 100755
--- a/tko/parse.py
+++ b/tko/parse.py
@@ -157,6 +157,18 @@
 
     # write the job into the database
     db.insert_job(jobname, job)
+
+    # Serializing job into a binary file
+    try:
+        from autotest_lib.tko import tko_pb2
+        from autotest_lib.tko import job_serializer
+
+        serializer = job_serializer.JobSerializer()
+        serializer.serialize_to_binary(job, os.path.join(path, "job.serialize"))
+    except ImportError:
+        tko_utils.dprint("DEBUG: tko_pb2.py doesn't exist. Create by "
+                         "compiling tko/tko.proto.")
+
     db.commit()
 
 
diff --git a/tko/tko.proto b/tko/tko.proto
new file mode 100644
index 0000000..7f1af37
--- /dev/null
+++ b/tko/tko.proto
@@ -0,0 +1,52 @@
+message keyval {
+    required string name = 1;
+    required string value = 2;
+}
+
+message job {
+    required string dir = 1;
+    repeated test tests = 2;
+    required string user = 3;
+    required string label = 4;
+    required string machine = 5;
+    required int64 queued_time = 6;
+    required int64 started_time = 7;
+    required int64 finished_time = 8;
+    required string machine_owner = 9;
+    required string machine_group = 10;
+    required string aborted_by = 11;
+    required int64 aborted_on = 12;
+    repeated keyval keyval_dict = 13;
+}
+
+message kernel {
+    required string base = 1;
+    repeated patch patches = 2;
+    required string kernel_hash = 3;
+}
+
+message patch {
+    required string spec = 1;
+    required string reference = 2;
+    required int64 hash = 3;
+}
+
+message iteration {
+    required int64 index = 1;
+    repeated keyval attr_keyval = 2;
+    repeated keyval perf_keyval = 3;
+}
+
+message test {
+    required string subdir = 1;
+    required string testname = 2;
+    required string status = 3;
+    required string reason = 4;
+    required kernel kernel = 5;
+    required string machine = 6;
+    required int64 started_time = 7;
+    required int64 finished_time = 8;
+    repeated iteration iterations = 9;
+    repeated keyval attributes = 10;
+    repeated string labels = 11;
+}