1# Copyright 2012 the V8 project authors. All rights reserved.
2# Redistribution and use in source and binary forms, with or without
3# modification, are permitted provided that the following conditions are
4# met:
5#
6#     * Redistributions of source code must retain the above copyright
7#       notice, this list of conditions and the following disclaimer.
8#     * Redistributions in binary form must reproduce the above
9#       copyright notice, this list of conditions and the following
10#       disclaimer in the documentation and/or other materials provided
11#       with the distribution.
12#     * Neither the name of Google Inc. nor the names of its
13#       contributors may be used to endorse or promote products derived
14#       from this software without specific prior written permission.
15#
16# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28
29from . import context
30from . import testcase
31
32class WorkPacket(object):
33  def __init__(self, peer=None, context=None, tests=None, binaries=None,
34               base_revision=None, patch=None, pubkey=None):
35    self.peer = peer
36    self.context = context
37    self.tests = tests
38    self.binaries = binaries
39    self.base_revision = base_revision
40    self.patch = patch
41    self.pubkey_fingerprint = pubkey
42
43  def Pack(self, binaries_dict):
44    """
45    Creates a JSON serializable object containing the data of this
46    work packet.
47    """
48    need_libv8 = False
49    binaries = []
50    for shell in self.peer.shells:
51      prefetched_binary = binaries_dict[shell]
52      binaries.append({"name": shell,
53                       "blob": prefetched_binary[0],
54                       "sign": prefetched_binary[1]})
55      if prefetched_binary[2]:
56        need_libv8 = True
57    if need_libv8:
58      libv8 = binaries_dict["libv8.so"]
59      binaries.append({"name": "libv8.so",
60                       "blob": libv8[0],
61                       "sign": libv8[1]})
62    tests = []
63    test_map = {}
64    for t in self.peer.tests:
65      test_map[t.id] = t
66      tests.append(t.PackTask())
67    result = {
68      "binaries": binaries,
69      "pubkey": self.pubkey_fingerprint,
70      "context": self.context.Pack(),
71      "base_revision": self.base_revision,
72      "patch": self.patch,
73      "tests": tests
74    }
75    return result, test_map
76
77  @staticmethod
78  def Unpack(packed):
79    """
80    Creates a WorkPacket object from the given packed representation.
81    """
82    binaries = packed["binaries"]
83    pubkey_fingerprint = packed["pubkey"]
84    ctx = context.Context.Unpack(packed["context"])
85    base_revision = packed["base_revision"]
86    patch = packed["patch"]
87    tests = [ testcase.TestCase.UnpackTask(t) for t in packed["tests"] ]
88    return WorkPacket(context=ctx, tests=tests, binaries=binaries,
89                      base_revision=base_revision, patch=patch,
90                      pubkey=pubkey_fingerprint)
91