133e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org# Copyright 2012 the V8 project authors. All rights reserved.
233e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org# Redistribution and use in source and binary forms, with or without
333e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org# modification, are permitted provided that the following conditions are
433e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org# met:
533e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org#
633e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org#     * Redistributions of source code must retain the above copyright
733e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org#       notice, this list of conditions and the following disclaimer.
833e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org#     * Redistributions in binary form must reproduce the above
933e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org#       copyright notice, this list of conditions and the following
1033e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org#       disclaimer in the documentation and/or other materials provided
1133e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org#       with the distribution.
1233e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org#     * Neither the name of Google Inc. nor the names of its
1333e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org#       contributors may be used to endorse or promote products derived
1433e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org#       from this software without specific prior written permission.
1533e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org#
1633e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1733e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1833e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
1933e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2033e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2133e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2233e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2333e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2433e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2533e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2633e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2733e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org
2833e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org
2933e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.orgfrom . import context
3033e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.orgfrom . import testcase
3133e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org
3233e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.orgclass WorkPacket(object):
3333e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org  def __init__(self, peer=None, context=None, tests=None, binaries=None,
3433e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org               base_revision=None, patch=None, pubkey=None):
3533e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org    self.peer = peer
3633e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org    self.context = context
3733e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org    self.tests = tests
3833e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org    self.binaries = binaries
3933e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org    self.base_revision = base_revision
4033e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org    self.patch = patch
4133e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org    self.pubkey_fingerprint = pubkey
4233e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org
4333e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org  def Pack(self, binaries_dict):
4433e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org    """
4533e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org    Creates a JSON serializable object containing the data of this
4633e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org    work packet.
4733e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org    """
4833e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org    need_libv8 = False
4933e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org    binaries = []
5033e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org    for shell in self.peer.shells:
5133e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org      prefetched_binary = binaries_dict[shell]
5233e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org      binaries.append({"name": shell,
5333e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org                       "blob": prefetched_binary[0],
5433e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org                       "sign": prefetched_binary[1]})
5533e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org      if prefetched_binary[2]:
5633e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org        need_libv8 = True
5733e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org    if need_libv8:
5833e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org      libv8 = binaries_dict["libv8.so"]
5933e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org      binaries.append({"name": "libv8.so",
6033e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org                       "blob": libv8[0],
6133e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org                       "sign": libv8[1]})
6233e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org    tests = []
6333e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org    test_map = {}
6433e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org    for t in self.peer.tests:
6533e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org      test_map[t.id] = t
6633e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org      tests.append(t.PackTask())
6733e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org    result = {
6833e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org      "binaries": binaries,
6933e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org      "pubkey": self.pubkey_fingerprint,
7033e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org      "context": self.context.Pack(),
7133e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org      "base_revision": self.base_revision,
7233e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org      "patch": self.patch,
7333e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org      "tests": tests
7433e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org    }
7533e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org    return result, test_map
7633e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org
7733e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org  @staticmethod
7833e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org  def Unpack(packed):
7933e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org    """
8033e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org    Creates a WorkPacket object from the given packed representation.
8133e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org    """
8233e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org    binaries = packed["binaries"]
8333e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org    pubkey_fingerprint = packed["pubkey"]
8433e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org    ctx = context.Context.Unpack(packed["context"])
8533e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org    base_revision = packed["base_revision"]
8633e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org    patch = packed["patch"]
8733e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org    tests = [ testcase.TestCase.UnpackTask(t) for t in packed["tests"] ]
8833e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org    return WorkPacket(context=ctx, tests=tests, binaries=binaries,
8933e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org                      base_revision=base_revision, patch=patch,
9033e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org                      pubkey=pubkey_fingerprint)
91