Index: chrome/common/extensions/docs/server2/preview.py |
diff --git a/chrome/common/extensions/docs/server2/preview.py b/chrome/common/extensions/docs/server2/preview.py |
index a1d0e64d44ce24de8b1a55bbeeeefd987094d256..009acc3830513eb01de4bfcf6c82fdf34622c502 100755 |
--- a/chrome/common/extensions/docs/server2/preview.py |
+++ b/chrome/common/extensions/docs/server2/preview.py |
@@ -28,55 +28,48 @@ |
# relative paths (e.g. static/css/site.css) for convenient sandboxing. |
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer |
+from local_renderer import LocalRenderer |
import logging |
import optparse |
import os |
-import shutil |
-from StringIO import StringIO |
import sys |
import time |
-import urlparse |
import build_server |
# Copy all the files necessary to run the server. These are cleaned up when the |
# server quits. |
build_server.main() |
-from fake_fetchers import ConfigureFakeFetchers |
- |
-class _Response(object): |
- def __init__(self): |
- self.status = 200 |
- self.out = StringIO() |
- self.headers = {} |
- |
- def set_status(self, status): |
- self.status = status |
- |
-class _Request(object): |
- def __init__(self, path): |
- self.headers = {} |
- self.path = path |
- self.url = 'http://localhost' + path |
- |
-def _Render(path): |
- response = _Response() |
- Handler(_Request(urlparse.urlparse(path).path), response).get() |
- content = response.out.getvalue() |
- if isinstance(content, unicode): |
- content = content.encode('utf-8', 'replace') |
- return (content, response.status, response.headers) |
- |
def _GetLocalPath(): |
if os.sep in sys.argv[0]: |
return os.path.join(sys.argv[0].rsplit(os.sep, 1)[0], os.pardir, os.pardir) |
return os.path.join(os.pardir, os.pardir) |
+def _Render(base_dir, path): |
+ renderer = LocalRenderer(base_dir) |
+ content, status, headers = renderer.Render(path, always_online=True) |
+ while status in [301, 302]: |
+ redirect = headers['Location'].lstrip('/') |
+ sys.stderr.write('<!-- Redirected %s to %s -->\n' % (path, redirect)) |
+ content, status, headers = renderer.Render(redirect, always_online=True) |
+ return (content, status, headers) |
+ |
class RequestHandler(BaseHTTPRequestHandler): |
+ class Factory(object): |
+ def __init__(self, base_dir): |
+ self._base_dir = base_dir |
+ |
+ def Create(self, *args): |
+ return RequestHandler(self._base_dir, *args) |
+ |
+ def __init__(self, base_dir, *args): |
+ self._base_dir = base_dir |
+ BaseHTTPRequestHandler.__init__(self, *args) |
+ |
"""A HTTPRequestHandler that outputs the docs page generated by Handler. |
""" |
def do_GET(self): |
- content, status, headers = _Render(self.path) |
+ content, status, headers = _Render(self._base_dir, self.path) |
self.send_response(status) |
for k, v in headers.iteritems(): |
self.send_header(k, v) |
@@ -109,9 +102,6 @@ if __name__ == '__main__': |
'docs.') |
exit() |
- ConfigureFakeFetchers(os.path.join(opts.directory, 'docs')) |
- from handler import Handler |
- |
if opts.render: |
if opts.render.find('#') >= 0: |
(path, iterations) = opts.render.rsplit('#', 1) |
@@ -123,18 +113,13 @@ if __name__ == '__main__': |
if opts.time: |
start_time = time.time() |
- content, status, headers = _Render(path) |
- if status in [301, 302]: |
- # Handle a single level of redirection. |
- redirect_path = headers['Location'].lstrip('/') |
- sys.stderr.write('<!-- Redirected %s to %s -->\n' % (path, redirect_path)) |
- content, status, headers = _Render(redirect_path) |
+ content, status, headers = _Render(opts.directory, path) |
if status != 200: |
print('Error status: %s' % status) |
exit(1) |
for _ in range(extra_iterations): |
- _Render(path) |
+ _Render(opts.directory, path) |
if opts.time: |
print('Took %s seconds' % (time.time() - start_time)) |
@@ -156,7 +141,8 @@ if __name__ == '__main__': |
print('') |
logging.getLogger().setLevel(logging.INFO) |
- server = HTTPServer(('', int(opts.port)), RequestHandler) |
+ server = HTTPServer(('', int(opts.port)), |
+ RequestHandler.Factory(opts.directory).Create) |
try: |
server.serve_forever() |
finally: |