Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(180)

Side by Side Diff: chrome/common/extensions/docs/server2/preview.py

Issue 14218004: Devserver: only populate data during cron jobs, meaning all data from instances (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: . Created 7 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
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 from local_renderer import LocalRenderer
31 import logging 32 import logging
32 import optparse 33 import optparse
33 import os 34 import os
34 import shutil
35 from StringIO import StringIO
36 import sys 35 import sys
37 import time 36 import time
38 import urlparse
39 37
40 import build_server 38 import build_server
41 # Copy all the files necessary to run the server. These are cleaned up when the 39 # Copy all the files necessary to run the server. These are cleaned up when the
42 # server quits. 40 # server quits.
43 build_server.main() 41 build_server.main()
44 42
45 from fake_fetchers import ConfigureFakeFetchers
46
47 class _Response(object):
48 def __init__(self):
49 self.status = 200
50 self.out = StringIO()
51 self.headers = {}
52
53 def set_status(self, status):
54 self.status = status
55
56 class _Request(object):
57 def __init__(self, path):
58 self.headers = {}
59 self.path = path
60 self.url = 'http://localhost' + path
61
62 def _Render(path):
63 response = _Response()
64 Handler(_Request(urlparse.urlparse(path).path), response).get()
65 content = response.out.getvalue()
66 if isinstance(content, unicode):
67 content = content.encode('utf-8', 'replace')
68 return (content, response.status, response.headers)
69
70 def _GetLocalPath(): 43 def _GetLocalPath():
71 if os.sep in sys.argv[0]: 44 if os.sep in sys.argv[0]:
72 return os.path.join(sys.argv[0].rsplit(os.sep, 1)[0], os.pardir, os.pardir) 45 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) 46 return os.path.join(os.pardir, os.pardir)
74 47
48 def _Render(base_dir, path):
49 return LocalRenderer(base_dir).Render(path, always_online=True)
50
75 class RequestHandler(BaseHTTPRequestHandler): 51 class RequestHandler(BaseHTTPRequestHandler):
52 class Factory(object):
53 def __init__(self, base_dir):
54 self._base_dir = base_dir
55
56 def Create(self, *args):
57 return RequestHandler(self._base_dir, *args)
58
59 def __init__(self, base_dir, *args):
60 self._base_dir = base_dir
61 BaseHTTPRequestHandler.__init__(self, *args)
62
76 """A HTTPRequestHandler that outputs the docs page generated by Handler. 63 """A HTTPRequestHandler that outputs the docs page generated by Handler.
77 """ 64 """
78 def do_GET(self): 65 def do_GET(self):
79 content, status, headers = _Render(self.path) 66 content, status, headers = _Render(self._base_dir, self.path)
80 self.send_response(status) 67 self.send_response(status)
81 for k, v in headers.iteritems(): 68 for k, v in headers.iteritems():
82 self.send_header(k, v) 69 self.send_header(k, v)
83 self.end_headers() 70 self.end_headers()
84 self.wfile.write(content) 71 self.wfile.write(content)
85 72
86 if __name__ == '__main__': 73 if __name__ == '__main__':
87 parser = optparse.OptionParser( 74 parser = optparse.OptionParser(
88 description='Runs a server to preview the extension documentation.', 75 description='Runs a server to preview the extension documentation.',
89 usage='usage: %prog [option]...') 76 usage='usage: %prog [option]...')
(...skipping 12 matching lines...) Expand all
102 89
103 (opts, argv) = parser.parse_args() 90 (opts, argv) = parser.parse_args()
104 91
105 if (not os.path.isdir(opts.directory) or 92 if (not os.path.isdir(opts.directory) or
106 not os.path.isdir(os.path.join(opts.directory, 'docs')) or 93 not os.path.isdir(os.path.join(opts.directory, 'docs')) or
107 not os.path.isdir(os.path.join(opts.directory, 'api'))): 94 not os.path.isdir(os.path.join(opts.directory, 'api'))):
108 print('Specified directory does not exist or does not contain extension ' 95 print('Specified directory does not exist or does not contain extension '
109 'docs.') 96 'docs.')
110 exit() 97 exit()
111 98
112 ConfigureFakeFetchers(os.path.join(opts.directory, 'docs'))
113 from handler import Handler
114
115 if opts.render: 99 if opts.render:
116 if opts.render.find('#') >= 0: 100 if opts.render.find('#') >= 0:
117 (path, iterations) = opts.render.rsplit('#', 1) 101 (path, iterations) = opts.render.rsplit('#', 1)
118 extra_iterations = int(iterations) - 1 102 extra_iterations = int(iterations) - 1
119 else: 103 else:
120 path = opts.render 104 path = opts.render
121 extra_iterations = 0 105 extra_iterations = 0
122 106
123 if opts.time: 107 if opts.time:
124 start_time = time.time() 108 start_time = time.time()
125 109
126 content, status, headers = _Render(path) 110 content, status, headers = _Render(opts.directory, path)
127 if status in [301, 302]: 111 if status in [301, 302]:
128 # Handle a single level of redirection. 112 # Handle a single level of redirection.
129 redirect_path = headers['Location'].lstrip('/') 113 redirect_path = headers['Location'].lstrip('/')
130 sys.stderr.write('<!-- Redirected %s to %s -->\n' % (path, redirect_path)) 114 sys.stderr.write('<!-- Redirected %s to %s -->\n' % (path, redirect_path))
131 content, status, headers = _Render(redirect_path) 115 content, status, headers = _Render(opts.directory, redirect_path)
132 if status != 200: 116 if status != 200:
133 print('Error status: %s' % status) 117 print('Error status: %s' % status)
134 exit(1) 118 exit(1)
135 119
136 for _ in range(extra_iterations): 120 for _ in range(extra_iterations):
137 _Render(path) 121 _Render(opts.directory, path)
138 122
139 if opts.time: 123 if opts.time:
140 print('Took %s seconds' % (time.time() - start_time)) 124 print('Took %s seconds' % (time.time() - start_time))
141 else: 125 else:
142 # Static paths will show up as /stable/static/foo but this only makes 126 # Static paths will show up as /stable/static/foo but this only makes
143 # sense from a webserver. 127 # sense from a webserver.
144 print(content.replace('/stable/static', 'static')) 128 print(content.replace('/stable/static', 'static'))
145 exit() 129 exit()
146 130
147 print('Starting previewserver on port %s' % opts.port) 131 print('Starting previewserver on port %s' % opts.port)
148 print('') 132 print('')
149 print('The extension documentation can be found at:') 133 print('The extension documentation can be found at:')
150 print('') 134 print('')
151 print(' http://localhost:%s/extensions/' % opts.port) 135 print(' http://localhost:%s/extensions/' % opts.port)
152 print('') 136 print('')
153 print('The apps documentation can be found at:') 137 print('The apps documentation can be found at:')
154 print('') 138 print('')
155 print(' http://localhost:%s/apps/' % opts.port) 139 print(' http://localhost:%s/apps/' % opts.port)
156 print('') 140 print('')
157 141
158 logging.getLogger().setLevel(logging.INFO) 142 logging.getLogger().setLevel(logging.INFO)
159 server = HTTPServer(('', int(opts.port)), RequestHandler) 143 server = HTTPServer(('', int(opts.port)),
144 RequestHandler.Factory(opts.directory).Create)
160 try: 145 try:
161 server.serve_forever() 146 server.serve_forever()
162 finally: 147 finally:
163 server.socket.close() 148 server.socket.close()
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698