WebKitTools/Scripts/webkitpy/tool/bot/patchcollection.py
changeset 0 4f2f89ce4247
equal deleted inserted replaced
-1:000000000000 0:4f2f89ce4247
       
     1 # Copyright (c) 2010 Google Inc. All rights reserved.
       
     2 #
       
     3 # Redistribution and use in source and binary forms, with or without
       
     4 # modification, are permitted provided that the following conditions are
       
     5 # met:
       
     6 # 
       
     7 #     * Redistributions of source code must retain the above copyright
       
     8 # notice, this list of conditions and the following disclaimer.
       
     9 #     * Redistributions in binary form must reproduce the above
       
    10 # copyright notice, this list of conditions and the following disclaimer
       
    11 # in the documentation and/or other materials provided with the
       
    12 # distribution.
       
    13 #     * Neither the name of Google Inc. nor the names of its
       
    14 # contributors may be used to endorse or promote products derived from
       
    15 # this software without specific prior written permission.
       
    16 # 
       
    17 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
       
    18 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
       
    19 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
       
    20 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
       
    21 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
       
    22 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
       
    23 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
       
    24 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
       
    25 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
       
    26 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
       
    27 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
       
    28 
       
    29 
       
    30 class PersistentPatchCollectionDelegate:
       
    31     def collection_name(self):
       
    32         raise NotImplementedError, "subclasses must implement"
       
    33 
       
    34     def fetch_potential_patch_ids(self):
       
    35         raise NotImplementedError, "subclasses must implement"
       
    36 
       
    37     def status_server(self):
       
    38         raise NotImplementedError, "subclasses must implement"
       
    39 
       
    40     def is_terminal_status(self, status):
       
    41         raise NotImplementedError, "subclasses must implement"
       
    42 
       
    43 
       
    44 class PersistentPatchCollection:
       
    45     def __init__(self, delegate):
       
    46         self._delegate = delegate
       
    47         self._name = self._delegate.collection_name()
       
    48         self._status = self._delegate.status_server()
       
    49         self._status_cache = {}
       
    50 
       
    51     def _cached_status(self, patch_id):
       
    52         cached = self._status_cache.get(patch_id)
       
    53         if cached:
       
    54             return cached
       
    55         status = self._status.patch_status(self._name, patch_id)
       
    56         if status and self._delegate.is_terminal_status(status):
       
    57             self._status_cache[patch_id] = status
       
    58         return status
       
    59 
       
    60     def _is_active_patch_id(self, patch_id):
       
    61         """Active patches are patches waiting to be processed from this collection."""
       
    62         status = self._cached_status(patch_id)
       
    63         return not status or not self._delegate.is_terminal_status(status)
       
    64 
       
    65     def _fetch_active_patch_ids(self):
       
    66         patch_ids = self._delegate.fetch_potential_patch_ids()
       
    67         return filter(lambda patch_id: self._is_active_patch_id(patch_id), patch_ids)
       
    68 
       
    69     def next(self):
       
    70         # Note: We only fetch all the ids so we can post them back to the server.
       
    71         # This will go away once we have a feeder queue and all other queues are
       
    72         # just pulling their next work item from the server.
       
    73         patch_ids = self._fetch_active_patch_ids()
       
    74         # FIXME: We're assuming self._name is a valid queue-name.
       
    75         self._status.update_work_items(self._name, patch_ids)
       
    76         if not patch_ids:
       
    77             return None
       
    78         return patch_ids[0]