| OLD | NEW |
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
| 5 | 5 |
| 6 # This helps you preview the apps and extensions docs. | 6 # This helps you preview the apps and extensions docs. |
| 7 # | 7 # |
| 8 # ./preview.py --help | 8 # ./preview.py --help |
| 9 # | 9 # |
| 10 # There are two modes: server- and render- mode. The default is server, in which | 10 # There are two modes: server- and render- mode. The default is server, in which |
| (...skipping 15 matching lines...) Expand all Loading... |
| 26 # | 26 # |
| 27 # Note: absolute paths into static content (e.g. /static/css/site.css) will be | 27 # Note: absolute paths into static content (e.g. /static/css/site.css) will be |
| 28 # relative paths (e.g. static/css/site.css) for convenient sandboxing. | 28 # relative paths (e.g. static/css/site.css) for convenient sandboxing. |
| 29 | 29 |
| 30 # NOTE: RUN THIS FIRST. Or all third_party imports will fail. | 30 # NOTE: RUN THIS FIRST. Or all third_party imports will fail. |
| 31 import build_server | 31 import build_server |
| 32 # Copy all the files necessary to run the server. These are cleaned up when the | 32 # Copy all the files necessary to run the server. These are cleaned up when the |
| 33 # server quits. | 33 # server quits. |
| 34 build_server.main() | 34 build_server.main() |
| 35 | 35 |
| 36 from render_servlet import AlwaysOnline |
| 36 from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer | 37 from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer |
| 37 from local_renderer import LocalRenderer | 38 from local_renderer import LocalRenderer |
| 38 import logging | 39 import logging |
| 39 import optparse | 40 import optparse |
| 40 import os | 41 import os |
| 41 import sys | 42 import sys |
| 42 import time | 43 import time |
| 43 | 44 |
| 44 def _GetLocalPath(): | 45 def _GetLocalPath(): |
| 45 if os.sep in sys.argv[0]: | 46 if os.sep in sys.argv[0]: |
| 46 return os.path.join(sys.argv[0].rsplit(os.sep, 1)[0], os.pardir, os.pardir) | 47 return os.path.join(sys.argv[0].rsplit(os.sep, 1)[0], os.pardir, os.pardir) |
| 47 return os.path.join(os.pardir, os.pardir) | 48 return os.path.join(os.pardir, os.pardir) |
| 48 | 49 |
| 50 @AlwaysOnline |
| 49 def _Render(base_dir, path): | 51 def _Render(base_dir, path): |
| 50 renderer = LocalRenderer(base_dir) | 52 renderer = LocalRenderer(base_dir) |
| 51 content, status, headers = renderer.Render(path, always_online=True) | 53 response = renderer.Render(path) |
| 52 while status in [301, 302]: | 54 while response.status in [301, 302]: |
| 53 redirect = headers['Location'].lstrip('/') | 55 redirect = response.headers['Location'].lstrip('/') |
| 54 sys.stderr.write('<!-- Redirected %s to %s -->\n' % (path, redirect)) | 56 sys.stderr.write('<!-- Redirected %s to %s -->\n' % (path, redirect)) |
| 55 content, status, headers = renderer.Render(redirect, always_online=True) | 57 response = renderer.Render(redirect) |
| 56 return (content, status, headers) | 58 return response |
| 57 | 59 |
| 58 class RequestHandler(BaseHTTPRequestHandler): | 60 class RequestHandler(BaseHTTPRequestHandler): |
| 59 class Factory(object): | 61 class Factory(object): |
| 60 def __init__(self, base_dir): | 62 def __init__(self, base_dir): |
| 61 self._base_dir = base_dir | 63 self._base_dir = base_dir |
| 62 | 64 |
| 63 def Create(self, *args): | 65 def Create(self, *args): |
| 64 return RequestHandler(self._base_dir, *args) | 66 return RequestHandler(self._base_dir, *args) |
| 65 | 67 |
| 66 def __init__(self, base_dir, *args): | 68 def __init__(self, base_dir, *args): |
| 67 self._base_dir = base_dir | 69 self._base_dir = base_dir |
| 68 BaseHTTPRequestHandler.__init__(self, *args) | 70 BaseHTTPRequestHandler.__init__(self, *args) |
| 69 | 71 |
| 70 """A HTTPRequestHandler that outputs the docs page generated by Handler. | 72 """A HTTPRequestHandler that outputs the docs page generated by Handler. |
| 71 """ | 73 """ |
| 72 def do_GET(self): | 74 def do_GET(self): |
| 73 content, status, headers = _Render(self._base_dir, self.path) | 75 response = _Render(self._base_dir, self.path) |
| 74 self.send_response(status) | 76 self.send_response(response.status) |
| 75 for k, v in headers.iteritems(): | 77 for k, v in response.headers.iteritems(): |
| 76 self.send_header(k, v) | 78 self.send_header(k, v) |
| 77 self.end_headers() | 79 self.end_headers() |
| 78 self.wfile.write(content) | 80 self.wfile.write(response.content) |
| 79 | 81 |
| 80 if __name__ == '__main__': | 82 if __name__ == '__main__': |
| 81 parser = optparse.OptionParser( | 83 parser = optparse.OptionParser( |
| 82 description='Runs a server to preview the extension documentation.', | 84 description='Runs a server to preview the extension documentation.', |
| 83 usage='usage: %prog [option]...') | 85 usage='usage: %prog [option]...') |
| 84 parser.add_option('-p', '--port', default='8000', | 86 parser.add_option('-p', '--port', default='8000', |
| 85 help='port to run the server on') | 87 help='port to run the server on') |
| 86 parser.add_option('-d', '--directory', default=_GetLocalPath(), | 88 parser.add_option('-d', '--directory', default=_GetLocalPath(), |
| 87 help='extensions directory to serve from - ' | 89 help='extensions directory to serve from - ' |
| 88 'should be chrome/common/extensions within a Chromium checkout') | 90 'should be chrome/common/extensions within a Chromium checkout') |
| (...skipping 18 matching lines...) Expand all Loading... |
| 107 if opts.render.find('#') >= 0: | 109 if opts.render.find('#') >= 0: |
| 108 (path, iterations) = opts.render.rsplit('#', 1) | 110 (path, iterations) = opts.render.rsplit('#', 1) |
| 109 extra_iterations = int(iterations) - 1 | 111 extra_iterations = int(iterations) - 1 |
| 110 else: | 112 else: |
| 111 path = opts.render | 113 path = opts.render |
| 112 extra_iterations = 0 | 114 extra_iterations = 0 |
| 113 | 115 |
| 114 if opts.time: | 116 if opts.time: |
| 115 start_time = time.time() | 117 start_time = time.time() |
| 116 | 118 |
| 117 content, status, headers = _Render(opts.directory, path) | 119 response = _Render(opts.directory, path) |
| 118 if status != 200: | 120 if response.status != 200: |
| 119 print('Error status: %s' % status) | 121 print('Error status: %s' % response.status) |
| 120 exit(1) | 122 exit(1) |
| 121 | 123 |
| 122 for _ in range(extra_iterations): | 124 for _ in range(extra_iterations): |
| 123 _Render(opts.directory, path) | 125 _Render(opts.directory, path) |
| 124 | 126 |
| 125 if opts.time: | 127 if opts.time: |
| 126 print('Took %s seconds' % (time.time() - start_time)) | 128 print('Took %s seconds' % (time.time() - start_time)) |
| 127 else: | 129 else: |
| 128 # Static paths will show up as /stable/static/foo but this only makes | 130 # Static paths will show up as /stable/static/foo but this only makes |
| 129 # sense from a webserver. | 131 # sense from a webserver. |
| (...skipping 11 matching lines...) Expand all Loading... |
| 141 print(' http://localhost:%s/apps/' % opts.port) | 143 print(' http://localhost:%s/apps/' % opts.port) |
| 142 print('') | 144 print('') |
| 143 | 145 |
| 144 logging.getLogger().setLevel(logging.INFO) | 146 logging.getLogger().setLevel(logging.INFO) |
| 145 server = HTTPServer(('', int(opts.port)), | 147 server = HTTPServer(('', int(opts.port)), |
| 146 RequestHandler.Factory(opts.directory).Create) | 148 RequestHandler.Factory(opts.directory).Create) |
| 147 try: | 149 try: |
| 148 server.serve_forever() | 150 server.serve_forever() |
| 149 finally: | 151 finally: |
| 150 server.socket.close() | 152 server.socket.close() |
| OLD | NEW |