abstractlocalservercommand.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. # Copyright (C) 2011 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. # 1. Redistributions of source code must retain the above copyright
  8. # notice, this list of conditions and the following disclaimer.
  9. # 2. Redistributions in binary form must reproduce the above copyright
  10. # notice, this list of conditions and the following disclaimer in the
  11. # documentation and/or other materials provided with the distribution.
  12. #
  13. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  14. # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  15. # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  16. # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  17. # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  18. # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  19. # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  20. # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  21. # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  23. # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. from optparse import make_option
  25. import threading
  26. from webkitpy.tool.multicommandtool import Command
  27. class AbstractLocalServerCommand(Command):
  28. server = None
  29. launch_path = "/"
  30. def __init__(self):
  31. options = [
  32. make_option("--httpd-port", action="store", type="int", default=8127, help="Port to use for the HTTP server"),
  33. make_option("--no-show-results", action="store_false", default=True, dest="show_results", help="Don't launch a browser with the rebaseline server"),
  34. ]
  35. Command.__init__(self, options=options)
  36. def _prepare_config(self, options, args, tool):
  37. return None
  38. def execute(self, options, args, tool):
  39. config = self._prepare_config(options, args, tool)
  40. server_url = "http://localhost:%d%s" % (options.httpd_port, self.launch_path)
  41. print "Starting server at %s" % server_url
  42. print "Use the 'Exit' link in the UI, %squitquitquit or Ctrl-C to stop" % server_url
  43. if options.show_results:
  44. # FIXME: This seems racy.
  45. threading.Timer(0.1, lambda: self._tool.user.open_url(server_url)).start()
  46. httpd = self.server(httpd_port=options.httpd_port, config=config) # pylint: disable=E1102
  47. httpd.serve_forever()