1 # Copyright (C) 2009 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 import re 30 31 from google.appengine.api import memcache 32 33 from model.queues import queues 34 from model.queuestatus import QueueStatus 35 36 37 class Attachment(object): 38 @classmethod 39 def dirty(cls, attachment_id): 40 memcache.delete(str(attachment_id), namespace="attachment-summary") 41 42 @classmethod 43 def recent(cls, limit=1): 44 statuses = QueueStatus.all().order("-date") 45 # Notice that we use both a set and a list here to keep the -date ordering. 46 ids = [] 47 visited_ids = set() 48 for status in statuses: 49 attachment_id = status.active_patch_id 50 if not attachment_id: 51 continue 52 if attachment_id in visited_ids: 53 continue 54 visited_ids.add(attachment_id) 55 ids.append(attachment_id) 56 if len(visited_ids) >= limit: 57 break 58 return map(cls, ids) 59 60 def __init__(self, attachment_id): 61 self.id = attachment_id 62 self._summary = None 63 64 def summary(self): 65 if self._summary: 66 return self._summary 67 self._summary = memcache.get(str(self.id), namespace="attachment-summary") 68 if self._summary: 69 return self._summary 70 self._summary = self._fetch_summary() 71 memcache.set(str(self.id), self._summary, namespace="attachment-summary") 72 return self._summary 73 74 def _dash_to_underscore(self, dashed_name): 75 regexp = re.compile("-") 76 return regexp.sub("_", dashed_name) 77 78 def _state_from_status(self, status): 79 table = { 80 "Pass" : "pass", 81 "Fail" : "fail", 82 } 83 state = table.get(status.message) 84 if state: 85 return state 86 if status.message.startswith("Error:"): 87 return "error" 88 if status: 89 return "pending" 90 return None 91 92 def _fetch_summary(self): 93 summary = { "attachment_id" : self.id } 94 95 first_status = QueueStatus.all().filter('active_patch_id =', self.id).get() 96 if not first_status: 97 # We don't have any record of this attachment. 98 return summary 99 summary["bug_id"] = first_status.active_bug_id 100 101 for queue in queues: 102 summary[queue] = None 103 status = QueueStatus.all().filter('queue_name =', queue).filter('active_patch_id =', self.id).order('-date').get() 104 if status: 105 summary[self._dash_to_underscore(queue)] = { 106 "state" : self._state_from_status(status), 107 "status" : status, 108 } 109 return summary 110