Separate Simple Backend creation from initialization.
[chromium-blink-merge.git] / build / android / adb_reverse_forwarder.py
blobff6d77b159cc401c7108ec9369fc82b5d67097e6
1 #!/usr/bin/env python
3 # Copyright (c) 2013 The Chromium Authors. All rights reserved.
4 # Use of this source code is governed by a BSD-style license that can be
5 # found in the LICENSE file.
7 """Command line tool for forwarding ports from a device to the host.
9 Allows an Android device to connect to services running on the host machine,
10 i.e., "adb forward" in reverse. Requires |host_forwarder| and |device_forwarder|
11 to be built.
12 """
14 import optparse
15 import sys
16 import time
18 from pylib import android_commands, forwarder
19 from pylib.utils import run_tests_helper
20 from pylib.valgrind_tools import CreateTool
23 def main(argv):
24 parser = optparse.OptionParser(usage='Usage: %prog [options] device_port '
25 'host_port [device_port_2 host_port_2] ...',
26 description=__doc__)
27 parser.add_option('-v',
28 '--verbose',
29 dest='verbose_count',
30 default=0,
31 action='count',
32 help='Verbose level (multiple times for more)')
33 parser.add_option('--device',
34 help='Serial number of device we should use.')
35 parser.add_option('--host',
36 help='Host address to forward to from the host machine. '
37 '127.0.0.1 by default', default='127.0.0.1')
38 parser.add_option('--debug', action='store_const', const='Debug',
39 dest='build_type', default='Release',
40 help='Use Debug build of host tools instead of Release.')
42 options, args = parser.parse_args(argv)
43 run_tests_helper.SetLogLevel(options.verbose_count)
45 if len(args) < 2 or not len(args) % 2:
46 parser.error('Need even number of port pairs')
47 sys.exit(1)
49 try:
50 port_pairs = map(int, args[1:])
51 port_pairs = zip(port_pairs[::2], port_pairs[1::2])
52 except ValueError:
53 parser.error('Bad port number')
54 sys.exit(1)
56 adb = android_commands.AndroidCommands(options.device)
57 tool = CreateTool(None, adb)
58 forwarder_instance = forwarder.Forwarder(adb, options.build_type)
59 try:
60 forwarder_instance.Run(port_pairs, tool, options.host)
61 while True:
62 time.sleep(60)
63 except KeyboardInterrupt:
64 sys.exit(0)
65 finally:
66 forwarder_instance.Close()
69 if __name__ == '__main__':
70 main(sys.argv)