Add new certificateProvider extension API.
[chromium-blink-merge.git] / chrome / common / extensions / docs / server2 / preview.py
blobe19e13e346868ce09788a50035935b536d35335d
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 posixpath
37 import time
39 from local_renderer import LocalRenderer
41 class _RequestHandler(BaseHTTPRequestHandler):
42 '''A HTTPRequestHandler that outputs the docs page generated by Handler.
43 '''
44 def do_GET(self):
45 # Sanitize path to guarantee that it stays within the server.
46 if not posixpath.abspath(self.path.lstrip('/')).startswith(
47 posixpath.abspath('')):
48 return
50 # Rewrite paths that would otherwise be served from app.yaml.
51 self.path = {
52 '/robots.txt': '../../server2/robots.txt',
53 '/favicon.ico': '../../server2/chrome-32.ico',
54 '/apple-touch-icon-precomposed.png': '../../server2/chrome-128.png'
55 }.get(self.path, self.path)
56 response = LocalRenderer.Render(self.path, headers=dict(self.headers))
57 self.protocol_version = 'HTTP/1.1'
58 self.send_response(response.status)
59 for k, v in response.headers.iteritems():
60 self.send_header(k, v)
61 self.end_headers()
62 self.wfile.write(response.content.ToString())
64 if __name__ == '__main__':
65 parser = optparse.OptionParser(
66 description='Runs a server to preview the extension documentation.',
67 usage='usage: %prog [option]...')
68 parser.add_option('-a', '--address', default='127.0.0.1',
69 help='the local interface address to bind the server to')
70 parser.add_option('-p', '--port', default='8000',
71 help='port to run the server on')
72 parser.add_option('-r', '--render', default='',
73 help='statically render a page and print to stdout rather than starting '
74 'the server, e.g. apps/storage.html. The path may optionally end '
75 'with #n where n is the number of times to render the page before '
76 'printing it, e.g. apps/storage.html#50, to use for profiling.')
77 parser.add_option('-s', '--stat',
78 help='Print profile stats at the end of the run using the given '
79 'profiling option (like "tottime"). -t is ignored if this is set.')
80 parser.add_option('-t', '--time', action='store_true',
81 help='Print the time taken rendering rather than the result.')
83 (opts, argv) = parser.parse_args()
85 if opts.render:
86 if opts.render.find('#') >= 0:
87 (path, iterations) = opts.render.rsplit('#', 1)
88 extra_iterations = int(iterations) - 1
89 else:
90 path = opts.render
91 extra_iterations = 0
93 if opts.stat:
94 import cProfile, pstats, StringIO
95 pr = cProfile.Profile()
96 pr.enable()
97 elif opts.time:
98 start_time = time.time()
100 response = LocalRenderer.Render(path)
101 if response.status != 200:
102 print('Error status: %s' % response.status)
103 exit(1)
105 for _ in range(extra_iterations):
106 LocalRenderer.Render(path)
108 if opts.stat:
109 pr.disable()
110 s = StringIO.StringIO()
111 pstats.Stats(pr, stream=s).sort_stats(opts.stat).print_stats()
112 print(s.getvalue())
113 elif opts.time:
114 print('Took %s seconds' % (time.time() - start_time))
115 else:
116 print(response.content.ToString())
117 exit()
119 print('Starting previewserver on port %s' % opts.port)
120 print('')
121 print('The extension documentation can be found at:')
122 print('')
123 print(' http://localhost:%s/extensions/' % opts.port)
124 print('')
125 print('The apps documentation can be found at:')
126 print('')
127 print(' http://localhost:%s/apps/' % opts.port)
128 print('')
130 logging.getLogger().setLevel(logging.INFO)
131 server = HTTPServer((opts.address, int(opts.port)), _RequestHandler)
132 try:
133 server.serve_forever()
134 finally:
135 server.socket.close()