1d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)# Copyright (C) 2010 Google Inc. All rights reserved.
2d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)#
3d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)# Redistribution and use in source and binary forms, with or without
4d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)# modification, are permitted provided that the following conditions are
5d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)# met:
6d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)#
7d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)#     * Redistributions of source code must retain the above copyright
8d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)# notice, this list of conditions and the following disclaimer.
9d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)#     * Redistributions in binary form must reproduce the above
10d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)# copyright notice, this list of conditions and the following disclaimer
11d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)# in the documentation and/or other materials provided with the
12d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)# distribution.
13d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)#     * Neither the name of Google Inc. nor the names of its
14d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)# contributors may be used to endorse or promote products derived from
15d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)# this software without specific prior written permission.
16d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)#
17d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)
29d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)"""A utility script for starting and stopping servers as they are used in the layout tests."""
30d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)
31d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)import logging
32d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)import optparse
33d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)
34d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)from webkitpy.common.host import Host
35d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)
36d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)_log = logging.getLogger(__name__)
37d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)
38d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)
39d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)def main(server_constructor, input_fn=None, argv=None, **kwargs):
40d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    input_fn = input_fn or raw_input
41d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)
42d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    option_parser = optparse.OptionParser()
43d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    option_parser.add_option('--output-dir', dest='output_dir',
44d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)                             default=None, help='output directory.')
45d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    option_parser.add_option('-v', '--verbose', action='store_true')
46d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    options, args = option_parser.parse_args(argv)
47d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)
48d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    logging.basicConfig()
49d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    logger = logging.getLogger()
50d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    logger.setLevel(logging.DEBUG if options.verbose else logging.INFO)
51d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)
52d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    host = Host()
53d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    port_obj = host.port_factory.get()
54d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    if not options.output_dir:
55d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)        options.output_dir = port_obj.default_results_directory()
56d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)
57d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    # Create the output directory if it doesn't already exist.
58d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    port_obj.host.filesystem.maybe_make_directory(options.output_dir)
59d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)
60d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    server = server_constructor(port_obj, options.output_dir, **kwargs)
61d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    server.start()
62d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    try:
63d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)        _ = input_fn('Hit any key to stop the server and exit.')
64d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    except (KeyboardInterrupt, EOFError) as e:
65d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)        pass
66d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)
67d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    server.stop()
68