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 16 matching lines...) Expand all Loading... |
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 logging |
32 import optparse | 32 import optparse |
33 import os | 33 import os |
34 import shutil | 34 import shutil |
35 from StringIO import StringIO | 35 from StringIO import StringIO |
36 import sys | 36 import sys |
| 37 import time |
37 import urlparse | 38 import urlparse |
38 | 39 |
39 import build_server | 40 import build_server |
40 # Copy all the files necessary to run the server. These are cleaned up when the | 41 # Copy all the files necessary to run the server. These are cleaned up when the |
41 # server quits. | 42 # server quits. |
42 build_server.main() | 43 build_server.main() |
43 | 44 |
44 from fake_fetchers import ConfigureFakeFetchers | 45 from fake_fetchers import ConfigureFakeFetchers |
45 | 46 |
46 class _Response(object): | 47 class _Response(object): |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
89 parser.add_option('-p', '--port', default='8000', | 90 parser.add_option('-p', '--port', default='8000', |
90 help='port to run the server on') | 91 help='port to run the server on') |
91 parser.add_option('-d', '--directory', default=_GetLocalPath(), | 92 parser.add_option('-d', '--directory', default=_GetLocalPath(), |
92 help='extensions directory to serve from - ' | 93 help='extensions directory to serve from - ' |
93 'should be chrome/common/extensions within a Chromium checkout') | 94 'should be chrome/common/extensions within a Chromium checkout') |
94 parser.add_option('-r', '--render', default='', | 95 parser.add_option('-r', '--render', default='', |
95 help='statically render a page and print to stdout rather than starting ' | 96 help='statically render a page and print to stdout rather than starting ' |
96 'the server, e.g. apps/storage.html. The path may optionally end ' | 97 'the server, e.g. apps/storage.html. The path may optionally end ' |
97 'with #n where n is the number of times to render the page before ' | 98 'with #n where n is the number of times to render the page before ' |
98 'printing it, e.g. apps/storage.html#50, to use for profiling.') | 99 'printing it, e.g. apps/storage.html#50, to use for profiling.') |
| 100 parser.add_option('-t', '--time', action='store_true', |
| 101 help='Print the time taken rendering rather than the result.') |
99 | 102 |
100 (opts, argv) = parser.parse_args() | 103 (opts, argv) = parser.parse_args() |
101 | 104 |
102 if (not os.path.isdir(opts.directory) or | 105 if (not os.path.isdir(opts.directory) or |
103 not os.path.isdir(os.path.join(opts.directory, 'docs')) or | 106 not os.path.isdir(os.path.join(opts.directory, 'docs')) or |
104 not os.path.isdir(os.path.join(opts.directory, 'api'))): | 107 not os.path.isdir(os.path.join(opts.directory, 'api'))): |
105 print('Specified directory does not exist or does not contain extension ' | 108 print('Specified directory does not exist or does not contain extension ' |
106 'docs.') | 109 'docs.') |
107 exit() | 110 exit() |
108 | 111 |
109 ConfigureFakeFetchers(os.path.join(opts.directory, 'docs')) | 112 ConfigureFakeFetchers(os.path.join(opts.directory, 'docs')) |
110 from handler import Handler | 113 from handler import Handler |
111 | 114 |
112 if opts.render: | 115 if opts.render: |
113 if opts.render.find('#') >= 0: | 116 if opts.render.find('#') >= 0: |
114 (path, iterations) = opts.render.rsplit('#', 1) | 117 (path, iterations) = opts.render.rsplit('#', 1) |
115 extra_iterations = int(iterations) - 1 | 118 extra_iterations = int(iterations) - 1 |
116 else: | 119 else: |
117 path = opts.render | 120 path = opts.render |
118 extra_iterations = 0 | 121 extra_iterations = 0 |
119 | 122 |
| 123 if opts.time: |
| 124 start_time = time.time() |
| 125 |
120 content, status, headers = _Render(path) | 126 content, status, headers = _Render(path) |
121 if status in [301, 302]: | 127 if status in [301, 302]: |
122 # Handle a single level of redirection. | 128 # Handle a single level of redirection. |
123 redirect_path = headers['Location'].lstrip('/') | 129 redirect_path = headers['Location'].lstrip('/') |
124 sys.stderr.write('<!-- Redirected %s to %s -->\n' % (path, redirect_path)) | 130 sys.stderr.write('<!-- Redirected %s to %s -->\n' % (path, redirect_path)) |
125 content, status, headers = _Render(redirect_path) | 131 content, status, headers = _Render(redirect_path) |
126 if status != 200: | 132 if status != 200: |
127 print('Error status: %s' % status) | 133 print('Error status: %s' % status) |
128 exit(1) | 134 exit(1) |
129 | 135 |
130 for _ in range(extra_iterations): | 136 for _ in range(extra_iterations): |
131 _Render(path) | 137 _Render(path) |
132 | 138 |
133 # Static paths will show up as /stable/static/foo but this only makes sense | 139 if opts.time: |
134 # from a webserver. | 140 print('Took %s seconds' % (time.time() - start_time)) |
135 print(content.replace('/stable/static', 'static')) | 141 else: |
| 142 # Static paths will show up as /stable/static/foo but this only makes |
| 143 # sense from a webserver. |
| 144 print(content.replace('/stable/static', 'static')) |
136 exit() | 145 exit() |
137 | 146 |
138 print('Starting previewserver on port %s' % opts.port) | 147 print('Starting previewserver on port %s' % opts.port) |
139 print('') | 148 print('') |
140 print('The extension documentation can be found at:') | 149 print('The extension documentation can be found at:') |
141 print('') | 150 print('') |
142 print(' http://localhost:%s/extensions/' % opts.port) | 151 print(' http://localhost:%s/extensions/' % opts.port) |
143 print('') | 152 print('') |
144 print('The apps documentation can be found at:') | 153 print('The apps documentation can be found at:') |
145 print('') | 154 print('') |
146 print(' http://localhost:%s/apps/' % opts.port) | 155 print(' http://localhost:%s/apps/' % opts.port) |
147 print('') | 156 print('') |
148 | 157 |
149 logging.getLogger().setLevel(logging.INFO) | 158 logging.getLogger().setLevel(logging.INFO) |
150 server = HTTPServer(('', int(opts.port)), RequestHandler) | 159 server = HTTPServer(('', int(opts.port)), RequestHandler) |
151 try: | 160 try: |
152 server.serve_forever() | 161 server.serve_forever() |
153 finally: | 162 finally: |
154 server.socket.close() | 163 server.socket.close() |
OLD | NEW |