Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / common / extensions / docs / server2 / preview.py
blob1d1020563d87ed230a0e278e437e7a2e3b6e34a7
1 #!/usr/bin/env python
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
4 # found in the LICENSE file.
6 # This helps you preview the apps and extensions docs.
8 # ./preview.py --help
10 # There are two modes: server- and render- mode. The default is server, in which
11 # a webserver is started on a port (default 8000). Navigating to paths on
12 # http://localhost:8000, for example
14 # http://localhost:8000/extensions/tabs.html
16 # will render the documentation for the extension tabs API.
18 # On the other hand, render mode statically renders docs to stdout. Use this
19 # to save the output (more convenient than needing to save the page in a
20 # browser), handy when uploading the docs somewhere (e.g. for a review),
21 # and for profiling the server. For example,
23 # ./preview.py -r extensions/tabs.html
25 # will output the documentation for the tabs API on stdout and exit immediately.
27 # NOTE: RUN THIS FIRST. Or all third_party imports will fail.
28 import build_server
29 # Copy all the files necessary to run the server. These are cleaned up when the
30 # server quits.
31 build_server.main()
33 from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
34 import logging
35 import optparse
36 import time
38 from local_renderer import LocalRenderer
40 class _RequestHandler(BaseHTTPRequestHandler):
41 '''A HTTPRequestHandler that outputs the docs page generated by Handler.
42 '''
43 def do_GET(self):
44 # Rewrite paths that would otherwise be served from app.yaml.
45 self.path = {
46 '/robots.txt': '../../server2/robots.txt',
47 '/favicon.ico': '../../server2/chrome-32.ico',
48 '/apple-touch-icon-precomposed.png': '../../server2/chrome-128.png'
49 }.get(self.path, self.path)
50 response = LocalRenderer.Render(self.path)
51 self.send_response(response.status)
52 for k, v in response.headers.iteritems():
53 self.send_header(k, v)
54 self.end_headers()
55 self.wfile.write(response.content.ToString())
57 if __name__ == '__main__':
58 parser = optparse.OptionParser(
59 description='Runs a server to preview the extension documentation.',
60 usage='usage: %prog [option]...')
61 parser.add_option('-p', '--port', default='8000',
62 help='port to run the server on')
63 parser.add_option('-r', '--render', default='',
64 help='statically render a page and print to stdout rather than starting '
65 'the server, e.g. apps/storage.html. The path may optionally end '
66 'with #n where n is the number of times to render the page before '
67 'printing it, e.g. apps/storage.html#50, to use for profiling.')
68 parser.add_option('-t', '--time', action='store_true',
69 help='Print the time taken rendering rather than the result.')
71 (opts, argv) = parser.parse_args()
73 if opts.render:
74 if opts.render.find('#') >= 0:
75 (path, iterations) = opts.render.rsplit('#', 1)
76 extra_iterations = int(iterations) - 1
77 else:
78 path = opts.render
79 extra_iterations = 0
81 if opts.time:
82 start_time = time.time()
84 response = LocalRenderer.Render(path)
85 if response.status != 200:
86 print('Error status: %s' % response.status)
87 exit(1)
89 for _ in range(extra_iterations):
90 LocalRenderer.Render(path)
92 if opts.time:
93 print('Took %s seconds' % (time.time() - start_time))
94 else:
95 print(response.content.ToString())
96 exit()
98 print('Starting previewserver on port %s' % opts.port)
99 print('')
100 print('The extension documentation can be found at:')
101 print('')
102 print(' http://localhost:%s/extensions/' % opts.port)
103 print('')
104 print('The apps documentation can be found at:')
105 print('')
106 print(' http://localhost:%s/apps/' % opts.port)
107 print('')
109 logging.getLogger().setLevel(logging.INFO)
110 server = HTTPServer(('', int(opts.port)), _RequestHandler)
111 try:
112 server.serve_forever()
113 finally:
114 server.socket.close()