| 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 10 matching lines...) Expand all Loading... |
| 21 # and for profiling the server. For example, | 21 # and for profiling the server. For example, |
| 22 # | 22 # |
| 23 # ./preview.py -r extensions/tabs.html | 23 # ./preview.py -r extensions/tabs.html |
| 24 # | 24 # |
| 25 # will output the documentation for the tabs API on stdout and exit immediately. | 25 # will output the documentation for the tabs API on stdout and exit immediately. |
| 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 from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer | 30 from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer |
| 31 import logging |
| 31 import optparse | 32 import optparse |
| 32 import os | 33 import os |
| 33 import shutil | 34 import shutil |
| 34 from StringIO import StringIO | 35 from StringIO import StringIO |
| 35 import sys | 36 import sys |
| 36 import urlparse | 37 import urlparse |
| 37 | 38 |
| 38 import build_server | 39 import build_server |
| 39 # Copy all the files necessary to run the server. These are cleaned up when the | 40 # Copy all the files necessary to run the server. These are cleaned up when the |
| 40 # server quits. | 41 # server quits. |
| 41 build_server.main() | 42 build_server.main() |
| 42 | 43 |
| 43 from fake_fetchers import ConfigureFakeFetchers | 44 from fake_fetchers import ConfigureFakeFetchers |
| 44 | 45 |
| 45 class _Response(object): | 46 class _Response(object): |
| 46 def __init__(self): | 47 def __init__(self): |
| 47 self.status = 200 | 48 self.status = 200 |
| 48 self.out = StringIO() | 49 self.out = StringIO() |
| 49 self.headers = {} | 50 self.headers = {} |
| 50 | 51 |
| 51 def set_status(self, status): | 52 def set_status(self, status): |
| 52 self.status = status | 53 self.status = status |
| 53 | 54 |
| 54 class _Request(object): | 55 class _Request(object): |
| 55 def __init__(self, path): | 56 def __init__(self, path): |
| 56 self.headers = {} | 57 self.headers = {} |
| 57 self.path = path | 58 self.path = path |
| 58 self.url = 'http://localhost' + path | 59 self.url = 'http://localhost' + path |
| 59 | 60 |
| 60 def _Render(path, local_path): | 61 def _Render(path): |
| 61 response = _Response() | 62 response = _Response() |
| 62 Handler(_Request(urlparse.urlparse(path).path), | 63 Handler(_Request(urlparse.urlparse(path).path), response).get() |
| 63 response, | |
| 64 local_path=local_path).get() | |
| 65 content = response.out.getvalue() | 64 content = response.out.getvalue() |
| 66 if isinstance(content, unicode): | 65 if isinstance(content, unicode): |
| 67 content = content.encode('utf-8', 'replace') | 66 content = content.encode('utf-8', 'replace') |
| 68 return (content, response.status, response.headers) | 67 return (content, response.status, response.headers) |
| 69 | 68 |
| 70 def _GetLocalPath(): | 69 def _GetLocalPath(): |
| 71 if os.sep in sys.argv[0]: | 70 if os.sep in sys.argv[0]: |
| 72 return os.path.join(sys.argv[0].rsplit(os.sep, 1)[0], os.pardir, os.pardir) | 71 return os.path.join(sys.argv[0].rsplit(os.sep, 1)[0], os.pardir, os.pardir) |
| 73 return os.path.join(os.pardir, os.pardir) | 72 return os.path.join(os.pardir, os.pardir) |
| 74 | 73 |
| 75 class RequestHandler(BaseHTTPRequestHandler): | 74 class RequestHandler(BaseHTTPRequestHandler): |
| 76 """A HTTPRequestHandler that outputs the docs page generated by Handler. | 75 """A HTTPRequestHandler that outputs the docs page generated by Handler. |
| 77 """ | 76 """ |
| 78 def do_GET(self): | 77 def do_GET(self): |
| 79 content, status, headers = _Render(self.path, RequestHandler.local_path) | 78 content, status, headers = _Render(self.path) |
| 80 self.send_response(status) | 79 self.send_response(status) |
| 81 for k, v in headers.iteritems(): | 80 for k, v in headers.iteritems(): |
| 82 self.send_header(k, v) | 81 self.send_header(k, v) |
| 83 self.end_headers() | 82 self.end_headers() |
| 84 self.wfile.write(content) | 83 self.wfile.write(content) |
| 85 | 84 |
| 86 if __name__ == '__main__': | 85 if __name__ == '__main__': |
| 87 parser = optparse.OptionParser( | 86 parser = optparse.OptionParser( |
| 88 description='Runs a server to preview the extension documentation.', | 87 description='Runs a server to preview the extension documentation.', |
| 89 usage='usage: %prog [option]...') | 88 usage='usage: %prog [option]...') |
| (...skipping 21 matching lines...) Expand all Loading... |
| 111 from handler import Handler | 110 from handler import Handler |
| 112 | 111 |
| 113 if opts.render: | 112 if opts.render: |
| 114 if opts.render.find('#') >= 0: | 113 if opts.render.find('#') >= 0: |
| 115 (path, iterations) = opts.render.rsplit('#', 1) | 114 (path, iterations) = opts.render.rsplit('#', 1) |
| 116 extra_iterations = int(iterations) - 1 | 115 extra_iterations = int(iterations) - 1 |
| 117 else: | 116 else: |
| 118 path = opts.render | 117 path = opts.render |
| 119 extra_iterations = 0 | 118 extra_iterations = 0 |
| 120 | 119 |
| 121 content, status, headers = _Render(path, opts.directory) | 120 content, status, headers = _Render(path) |
| 122 if status in [301, 302]: | 121 if status in [301, 302]: |
| 123 # Handle a single level of redirection. | 122 # Handle a single level of redirection. |
| 124 redirect_path = headers['Location'].lstrip('/') | 123 redirect_path = headers['Location'].lstrip('/') |
| 125 sys.stderr.write('<!-- Redirected %s to %s -->\n' % (path, redirect_path)) | 124 sys.stderr.write('<!-- Redirected %s to %s -->\n' % (path, redirect_path)) |
| 126 content, status, headers = _Render(redirect_path, opts.directory) | 125 content, status, headers = _Render(redirect_path) |
| 127 if status != 200: | 126 if status != 200: |
| 128 print('Error status: %s' % status) | 127 print('Error status: %s' % status) |
| 129 exit(1) | 128 exit(1) |
| 130 | 129 |
| 131 for _ in range(extra_iterations): | 130 for _ in range(extra_iterations): |
| 132 _Render(path, opts.directory) | 131 _Render(path) |
| 133 | 132 |
| 134 # Static paths will show up as /stable/static/foo but this only makes sense | 133 # Static paths will show up as /stable/static/foo but this only makes sense |
| 135 # from a webserver. | 134 # from a webserver. |
| 136 print(content.replace('/stable/static', 'static')) | 135 print(content.replace('/stable/static', 'static')) |
| 137 exit() | 136 exit() |
| 138 | 137 |
| 139 print('Starting previewserver on port %s' % opts.port) | 138 print('Starting previewserver on port %s' % opts.port) |
| 140 print('Reading from %s' % opts.directory) | |
| 141 print('') | 139 print('') |
| 142 print('The extension documentation can be found at:') | 140 print('The extension documentation can be found at:') |
| 143 print('') | 141 print('') |
| 144 print(' http://localhost:%s/extensions/' % opts.port) | 142 print(' http://localhost:%s/extensions/' % opts.port) |
| 145 print('') | 143 print('') |
| 146 print('The apps documentation can be found at:') | 144 print('The apps documentation can be found at:') |
| 147 print('') | 145 print('') |
| 148 print(' http://localhost:%s/apps/' % opts.port) | 146 print(' http://localhost:%s/apps/' % opts.port) |
| 149 print('') | 147 print('') |
| 150 | 148 |
| 151 RequestHandler.local_path = opts.directory | 149 logging.getLogger().setLevel(logging.INFO) |
| 152 server = HTTPServer(('', int(opts.port)), RequestHandler) | 150 server = HTTPServer(('', int(opts.port)), RequestHandler) |
| 153 try: | 151 try: |
| 154 server.serve_forever() | 152 server.serve_forever() |
| 155 finally: | 153 finally: |
| 156 server.socket.close() | 154 server.socket.close() |
| OLD | NEW |