OLD | NEW |
(Empty) | |
| 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. |
| 5 |
| 6 """Start an HTTP server which serves Chrome Endure graphs. |
| 7 |
| 8 Usage: |
| 9 python endure_server.py [options] |
| 10 |
| 11 To view Chrome Endure graphs from a browser, |
| 12 run this script to start a local HTTP server that serves the directory |
| 13 where graph code and test results are located. A port will be automatically |
| 14 picked. You can then view the graphs via http://localhost:<GIVEN_PORT>. |
| 15 |
| 16 Examples: |
| 17 >python endure_server.py |
| 18 Start a server which serves the default location |
| 19 <CURRENT_WORKING_DIR>/chrome_graph. |
| 20 |
| 21 >python endure_server.py --graph-dir=/home/user/Document/graph_dir |
| 22 Start a server which serves /home/user/Document/graph_dir which |
| 23 is where your graph code and test results are. |
| 24 """ |
| 25 |
| 26 import BaseHTTPServer |
| 27 import logging |
| 28 import optparse |
| 29 import os |
| 30 import SimpleHTTPServer |
| 31 import sys |
| 32 |
| 33 |
| 34 class HelpFormatter(optparse.IndentedHelpFormatter): |
| 35 """Format the help message of this script.""" |
| 36 |
| 37 def format_description(self, description): |
| 38 """Override to keep original format of the description.""" |
| 39 return description + '\n' if description else '' |
| 40 |
| 41 |
| 42 def _ParseArgs(argv): |
| 43 parser = optparse.OptionParser( |
| 44 usage='%prog [options]', |
| 45 formatter=HelpFormatter(), |
| 46 description=__doc__) |
| 47 parser.add_option( |
| 48 '-g', '--graph-dir', type='string', |
| 49 default=os.path.join(os.getcwd(), 'chrome_graph'), |
| 50 help='The directory that contains graph code ' \ |
| 51 'and data files of test results. Default value is ' \ |
| 52 '<CURRENT_WORKING_DIR>/chrome_graph') |
| 53 return parser.parse_args(argv) |
| 54 |
| 55 |
| 56 def Run(argv): |
| 57 """Start an HTTP server which serves Chrome Endure graphs.""" |
| 58 logging.basicConfig(format='[%(levelname)s] %(message)s', level=logging.DEBUG) |
| 59 options, _ = _ParseArgs(argv) |
| 60 graph_dir = os.path.abspath(options.graph_dir) |
| 61 cur_dir = os.getcwd() |
| 62 os.chdir(graph_dir) |
| 63 httpd = BaseHTTPServer.HTTPServer( |
| 64 ('', 0), SimpleHTTPServer.SimpleHTTPRequestHandler) |
| 65 try: |
| 66 logging.info('Serving %s at port %d', graph_dir, httpd.server_port) |
| 67 logging.info('View graphs at http://localhost:%d', httpd.server_port) |
| 68 logging.info('Press Ctrl-C to stop the server.') |
| 69 httpd.serve_forever() |
| 70 except KeyboardInterrupt: |
| 71 logging.info('Shutting down ...') |
| 72 httpd.shutdown() |
| 73 finally: |
| 74 os.chdir(cur_dir) |
| 75 return 0 |
| 76 |
| 77 |
| 78 if '__main__' == __name__: |
| 79 sys.exit(Run(sys.argv[1:])) |
OLD | NEW |