Add Apps.AppListSearchQueryLength UMA histogram.
[chromium-blink-merge.git] / build / android / adb_reverse_forwarder.py
blob6cae0cf5c8375e0cec73b8ab603d34bf45729b04
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 logging
15 import optparse
16 import sys
17 import time
19 from pylib import constants
20 from pylib import forwarder
21 from pylib.device import adb_wrapper
22 from pylib.device import device_utils
23 from pylib.utils import run_tests_helper
26 def main(argv):
27 parser = optparse.OptionParser(usage='Usage: %prog [options] device_port '
28 'host_port [device_port_2 host_port_2] ...',
29 description=__doc__)
30 parser.add_option('-v',
31 '--verbose',
32 dest='verbose_count',
33 default=0,
34 action='count',
35 help='Verbose level (multiple times for more)')
36 parser.add_option('--device',
37 help='Serial number of device we should use.')
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 devices = device_utils.DeviceUtils.HealthyDevices()
58 if options.device:
59 if options.device not in [str(d) for d in devices]:
60 raise Exception('Error: %s not in attached devices %s' % (options.device,
61 ','.join(devices)))
62 devices = [options.device]
63 else:
64 if not devices:
65 raise Exception('Error: no connected devices')
66 logging.info('No device specified. Defaulting to %s', devices[0])
68 device = devices[0]
69 constants.SetBuildType(options.build_type)
70 try:
71 forwarder.Forwarder.Map(port_pairs, device)
72 while True:
73 time.sleep(60)
74 except KeyboardInterrupt:
75 sys.exit(0)
76 finally:
77 forwarder.Forwarder.UnmapAllDevicePorts(device)
79 if __name__ == '__main__':
80 main(sys.argv)