1# Copyright (c) 2009 Google Inc. All rights reserved.
2# Copyright (c) 2009 Apple Inc. All rights reserved.
3# Copyright (c) 2010 Research In Motion Limited. All rights reserved.
4#
5# Redistribution and use in source and binary forms, with or without
6# modification, are permitted provided that the following conditions are
7# met:
8#
9#     * Redistributions of source code must retain the above copyright
10# notice, this list of conditions and the following disclaimer.
11#     * Redistributions in binary form must reproduce the above
12# copyright notice, this list of conditions and the following disclaimer
13# in the documentation and/or other materials provided with the
14# distribution.
15#     * Neither the name of Google Inc. nor the names of its
16# contributors may be used to endorse or promote products derived from
17# this software without specific prior written permission.
18#
19# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
31from webkitpy.common.system.deprecated_logging import log
32
33
34class Attachment(object):
35
36    rollout_preamble = "ROLLOUT of r"
37
38    def __init__(self, attachment_dictionary, bug):
39        self._attachment_dictionary = attachment_dictionary
40        self._bug = bug
41        self._reviewer = None
42        self._committer = None
43
44    def _bugzilla(self):
45        return self._bug._bugzilla
46
47    def id(self):
48        return int(self._attachment_dictionary.get("id"))
49
50    def attacher_is_committer(self):
51        return self._bugzilla.committers.committer_by_email(
52            patch.attacher_email())
53
54    def attacher_email(self):
55        return self._attachment_dictionary.get("attacher_email")
56
57    def bug(self):
58        return self._bug
59
60    def bug_id(self):
61        return int(self._attachment_dictionary.get("bug_id"))
62
63    def is_patch(self):
64        return not not self._attachment_dictionary.get("is_patch")
65
66    def is_obsolete(self):
67        return not not self._attachment_dictionary.get("is_obsolete")
68
69    def is_rollout(self):
70        return self.name().startswith(self.rollout_preamble)
71
72    def name(self):
73        return self._attachment_dictionary.get("name")
74
75    def attach_date(self):
76        return self._attachment_dictionary.get("attach_date")
77
78    def review(self):
79        return self._attachment_dictionary.get("review")
80
81    def commit_queue(self):
82        return self._attachment_dictionary.get("commit-queue")
83
84    def url(self):
85        # FIXME: This should just return
86        # self._bugzilla().attachment_url_for_id(self.id()). scm_unittest.py
87        # depends on the current behavior.
88        return self._attachment_dictionary.get("url")
89
90    def contents(self):
91        # FIXME: We shouldn't be grabbing at _bugzilla.
92        return self._bug._bugzilla.fetch_attachment_contents(self.id())
93
94    def _validate_flag_value(self, flag):
95        email = self._attachment_dictionary.get("%s_email" % flag)
96        if not email:
97            return None
98        committer = getattr(self._bugzilla().committers,
99                            "%s_by_email" % flag)(email)
100        if committer:
101            return committer
102        log("Warning, attachment %s on bug %s has invalid %s (%s)" % (
103                 self._attachment_dictionary['id'],
104                 self._attachment_dictionary['bug_id'], flag, email))
105
106    def reviewer(self):
107        if not self._reviewer:
108            self._reviewer = self._validate_flag_value("reviewer")
109        return self._reviewer
110
111    def committer(self):
112        if not self._committer:
113            self._committer = self._validate_flag_value("committer")
114        return self._committer
115