Home | History | Annotate | Download | only in webkitpy
      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.networktransaction import NetworkTransaction
     30 from webkitpy.webkit_logging import log
     31 from mechanize import Browser
     32 
     33 # WebKit includes a built copy of BeautifulSoup in Scripts/webkitpy
     34 # so this import should always succeed.
     35 from .BeautifulSoup import BeautifulSoup
     36 
     37 import urllib2
     38 
     39 
     40 class StatusServer:
     41     default_host = "webkit-commit-queue.appspot.com"
     42 
     43     def __init__(self, host=default_host):
     44         self.set_host(host)
     45         self.browser = Browser()
     46 
     47     def set_host(self, host):
     48         self.host = host
     49         self.url = "http://%s" % self.host
     50 
     51     def results_url_for_status(self, status_id):
     52         return "%s/results/%s" % (self.url, status_id)
     53 
     54     def _add_patch(self, patch):
     55         if not patch:
     56             return
     57         if patch.bug_id():
     58             self.browser["bug_id"] = str(patch.bug_id())
     59         if patch.id():
     60             self.browser["patch_id"] = str(patch.id())
     61 
     62     def _add_results_file(self, results_file):
     63         if not results_file:
     64             return
     65         self.browser.add_file(results_file, "text/plain", "results.txt", 'results_file')
     66 
     67     def _post_to_server(self, queue_name, status, patch, results_file):
     68         if results_file:
     69             # We might need to re-wind the file if we've already tried to post it.
     70             results_file.seek(0)
     71 
     72         update_status_url = "%s/update-status" % self.url
     73         self.browser.open(update_status_url)
     74         self.browser.select_form(name="update_status")
     75         self.browser['queue_name'] = queue_name
     76         self._add_patch(patch)
     77         self.browser['status'] = status
     78         self._add_results_file(results_file)
     79         return self.browser.submit().read() # This is the id of the newly created status object.
     80 
     81     def update_status(self, queue_name, status, patch=None, results_file=None):
     82         # During unit testing, host is None
     83         if not self.host:
     84             return
     85 
     86         log(status)
     87         return NetworkTransaction().run(lambda: self._post_to_server(queue_name, status, patch, results_file))
     88 
     89     def patch_status(self, queue_name, patch_id):
     90         update_status_url = "%s/patch-status/%s/%s" % (self.url, queue_name, patch_id)
     91         try:
     92             return urllib2.urlopen(update_status_url).read()
     93         except urllib2.HTTPError, e:
     94             if e.code == 404:
     95                 return None
     96             raise e
     97