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 | |
37 from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer | 36 from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer |
38 from local_renderer import LocalRenderer | 37 from local_renderer import LocalRenderer |
39 import logging | 38 import logging |
40 import optparse | 39 import optparse |
41 import os | 40 import os |
42 import sys | 41 import sys |
43 import time | 42 import time |
44 | 43 |
45 def _GetLocalPath(): | 44 def _GetLocalPath(): |
46 if os.sep in sys.argv[0]: | 45 if os.sep in sys.argv[0]: |
47 return os.path.join(sys.argv[0].rsplit(os.sep, 1)[0], os.pardir, os.pardir) | 46 return os.path.join(sys.argv[0].rsplit(os.sep, 1)[0], os.pardir, os.pardir) |
48 return os.path.join(os.pardir, os.pardir) | 47 return os.path.join(os.pardir, os.pardir) |
49 | 48 |
50 @AlwaysOnline | |
51 def _Render(base_dir, path): | 49 def _Render(base_dir, path): |
52 renderer = LocalRenderer(base_dir) | 50 renderer = LocalRenderer(base_dir) |
53 response = renderer.Render(path) | 51 response = renderer.Render(path) |
54 while response.status in [301, 302]: | 52 while response.status in [301, 302]: |
55 redirect = response.headers['Location'].lstrip('/') | 53 redirect = response.headers['Location'].lstrip('/') |
56 sys.stderr.write('<!-- Redirected %s to %s -->\n' % (path, redirect)) | 54 sys.stderr.write('<!-- Redirected %s to %s -->\n' % (path, redirect)) |
57 response = renderer.Render(redirect) | 55 response = renderer.Render(redirect) |
58 return response | 56 return response |
59 | 57 |
60 class RequestHandler(BaseHTTPRequestHandler): | 58 class RequestHandler(BaseHTTPRequestHandler): |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
143 print(' http://localhost:%s/apps/' % opts.port) | 141 print(' http://localhost:%s/apps/' % opts.port) |
144 print('') | 142 print('') |
145 | 143 |
146 logging.getLogger().setLevel(logging.INFO) | 144 logging.getLogger().setLevel(logging.INFO) |
147 server = HTTPServer(('', int(opts.port)), | 145 server = HTTPServer(('', int(opts.port)), |
148 RequestHandler.Factory(opts.directory).Create) | 146 RequestHandler.Factory(opts.directory).Create) |
149 try: | 147 try: |
150 server.serve_forever() | 148 server.serve_forever() |
151 finally: | 149 finally: |
152 server.socket.close() | 150 server.socket.close() |
OLD | NEW |