|
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 from webkitpy.common.net.networktransaction import NetworkTransaction |
|
30 from webkitpy.common.system.deprecated_logging import log |
|
31 from webkitpy.thirdparty.autoinstalled.mechanize import Browser |
|
32 from webkitpy.thirdparty.BeautifulSoup import BeautifulSoup |
|
33 |
|
34 import logging |
|
35 import urllib2 |
|
36 |
|
37 |
|
38 _log = logging.getLogger("webkitpy.common.net.statusserver") |
|
39 |
|
40 |
|
41 class StatusServer: |
|
42 default_host = "queues.webkit.org" |
|
43 |
|
44 def __init__(self, host=default_host): |
|
45 self.set_host(host) |
|
46 self.browser = Browser() |
|
47 |
|
48 def set_host(self, host): |
|
49 self.host = host |
|
50 self.url = "http://%s" % self.host |
|
51 |
|
52 def results_url_for_status(self, status_id): |
|
53 return "%s/results/%s" % (self.url, status_id) |
|
54 |
|
55 def _add_patch(self, patch): |
|
56 if not patch: |
|
57 return |
|
58 if patch.bug_id(): |
|
59 self.browser["bug_id"] = unicode(patch.bug_id()) |
|
60 if patch.id(): |
|
61 self.browser["patch_id"] = unicode(patch.id()) |
|
62 |
|
63 def _add_results_file(self, results_file): |
|
64 if not results_file: |
|
65 return |
|
66 self.browser.add_file(results_file, "text/plain", "results.txt", 'results_file') |
|
67 |
|
68 def _post_status_to_server(self, queue_name, status, patch, results_file): |
|
69 if results_file: |
|
70 # We might need to re-wind the file if we've already tried to post it. |
|
71 results_file.seek(0) |
|
72 |
|
73 update_status_url = "%s/update-status" % self.url |
|
74 self.browser.open(update_status_url) |
|
75 self.browser.select_form(name="update_status") |
|
76 self.browser["queue_name"] = queue_name |
|
77 self._add_patch(patch) |
|
78 self.browser["status"] = status |
|
79 self._add_results_file(results_file) |
|
80 return self.browser.submit().read() # This is the id of the newly created status object. |
|
81 |
|
82 def _post_svn_revision_to_server(self, svn_revision_number, broken_bot): |
|
83 update_svn_revision_url = "%s/update-svn-revision" % self.url |
|
84 self.browser.open(update_svn_revision_url) |
|
85 self.browser.select_form(name="update_svn_revision") |
|
86 self.browser["number"] = unicode(svn_revision_number) |
|
87 self.browser["broken_bot"] = broken_bot |
|
88 return self.browser.submit().read() |
|
89 |
|
90 def _post_work_items_to_server(self, queue_name, work_items): |
|
91 update_work_items_url = "%s/update-work-items" % self.url |
|
92 self.browser.open(update_work_items_url) |
|
93 self.browser.select_form(name="update_work_items") |
|
94 self.browser["queue_name"] = queue_name |
|
95 work_items = map(unicode, work_items) # .join expects strings |
|
96 self.browser["work_items"] = " ".join(work_items) |
|
97 return self.browser.submit().read() |
|
98 |
|
99 def update_work_items(self, queue_name, work_items): |
|
100 _log.debug("Recording work items: %s for %s" % (work_items, queue_name)) |
|
101 return NetworkTransaction().run(lambda: self._post_work_items_to_server(queue_name, work_items)) |
|
102 |
|
103 def update_status(self, queue_name, status, patch=None, results_file=None): |
|
104 log(status) |
|
105 return NetworkTransaction().run(lambda: self._post_status_to_server(queue_name, status, patch, results_file)) |
|
106 |
|
107 def update_svn_revision(self, svn_revision_number, broken_bot): |
|
108 log("SVN revision: %s broke %s" % (svn_revision_number, broken_bot)) |
|
109 return NetworkTransaction().run(lambda: self._post_svn_revision_to_server(svn_revision_number, broken_bot)) |
|
110 |
|
111 def _fetch_url(self, url): |
|
112 try: |
|
113 return urllib2.urlopen(url).read() |
|
114 except urllib2.HTTPError, e: |
|
115 if e.code == 404: |
|
116 return None |
|
117 raise e |
|
118 |
|
119 def patch_status(self, queue_name, patch_id): |
|
120 patch_status_url = "%s/patch-status/%s/%s" % (self.url, queue_name, patch_id) |
|
121 return self._fetch_url(patch_status_url) |
|
122 |
|
123 def svn_revision(self, svn_revision_number): |
|
124 svn_revision_url = "%s/svn-revision/%s" % (self.url, svn_revision_number) |
|
125 return self._fetch_url(svn_revision_url) |