2 # Copyright (c) 2009, Google Inc. All rights reserved.
4 # Redistribution and use in source and binary forms, with or without
5 # modification, are permitted provided that the following conditions are
8 # * Redistributions of source code must retain the above copyright
9 # notice, this list of conditions and the following disclaimer.
10 # * Redistributions in binary form must reproduce the above
11 # copyright notice, this list of conditions and the following disclaimer
12 # in the documentation and/or other materials provided with the
14 # * Neither the name of Google Inc. nor the names of its
15 # contributors may be used to endorse or promote products derived from
16 # this software without specific prior written permission.
18 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 class PatchCollection:
31 def __init__(self, bugs, filter=None):
37 self.add_patches([patch])
39 def add_patches(self, patches):
43 if self._filter and not self._filter(patch):
45 self._patches.append(patch)
47 def add_patches_from_bug(self, bug_id):
48 self.add_patches(self._bugs.fetch_patches_from_bug(bug_id))
53 return self._patches.pop(0)
56 return map(lambda patch: patch['id'], self._patches)
59 return len(self._patches)
62 class PersistentPatchCollectionDelegate:
63 def collection_name(self):
64 raise NotImplementedError, "subclasses must implement"
66 def fetch_potential_patch_ids(self):
67 raise NotImplementedError, "subclasses must implement"
69 def status_server(self):
70 raise NotImplementedError, "subclasses must implement"
73 class PersistentPatchCollection:
74 _initial_status = "Attempted"
75 _terminal_status = "Done"
76 def __init__(self, delegate):
77 self._delegate = delegate
78 self._name = self._delegate.collection_name()
79 self._status = self._delegate.status_server()
80 self._status_cache = {}
82 def _cached_status(self, patch_id):
83 cached = self._status_cache.get(patch_id)
86 status = self._status.patch_status(self._name, patch_id)
88 self._status_cache[patch_id] = status
92 patch_ids = self._delegate.fetch_potential_patch_ids()
93 for patch_id in patch_ids:
94 status = self._cached_status(patch_id)
98 def done(self, patch):
99 self._status.update_status(self._name, self._terminal_status, patch)