OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # | 2 # |
3 # Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 4 # Use of this source code is governed by a BSD-style license that can be |
5 # found in the LICENSE file. | 5 # found in the LICENSE file. |
6 | 6 |
7 """Command line tool for forwarding ports from a device to the host. | 7 """Command line tool for forwarding ports from a device to the host. |
8 | 8 |
9 Allows an Android device to connect to services running on the host machine, | 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| | 10 i.e., "adb forward" in reverse. Requires |host_forwarder| and |device_forwarder| |
11 to be built. | 11 to be built. |
12 """ | 12 """ |
13 | 13 |
14 import logging | 14 import logging |
15 import optparse | 15 import optparse |
16 import sys | 16 import sys |
17 import time | 17 import time |
18 | 18 |
19 from pylib import constants | 19 from pylib import constants |
20 from pylib import forwarder | 20 from pylib import forwarder |
21 from pylib.device import adb_wrapper | 21 from pylib.device import adb_wrapper |
| 22 from pylib.device import device_blacklist |
22 from pylib.device import device_errors | 23 from pylib.device import device_errors |
23 from pylib.device import device_utils | 24 from pylib.device import device_utils |
24 from pylib.utils import run_tests_helper | 25 from pylib.utils import run_tests_helper |
25 | 26 |
26 | 27 |
27 def main(argv): | 28 def main(argv): |
28 parser = optparse.OptionParser(usage='Usage: %prog [options] device_port ' | 29 parser = optparse.OptionParser(usage='Usage: %prog [options] device_port ' |
29 'host_port [device_port_2 host_port_2] ...', | 30 'host_port [device_port_2 host_port_2] ...', |
30 description=__doc__) | 31 description=__doc__) |
31 parser.add_option('-v', | 32 parser.add_option('-v', |
32 '--verbose', | 33 '--verbose', |
33 dest='verbose_count', | 34 dest='verbose_count', |
34 default=0, | 35 default=0, |
35 action='count', | 36 action='count', |
36 help='Verbose level (multiple times for more)') | 37 help='Verbose level (multiple times for more)') |
37 parser.add_option('--device', | 38 parser.add_option('--device', |
38 help='Serial number of device we should use.') | 39 help='Serial number of device we should use.') |
| 40 parser.add_option('--blacklist-file', help='Device blacklist JSON file.') |
39 parser.add_option('--debug', action='store_const', const='Debug', | 41 parser.add_option('--debug', action='store_const', const='Debug', |
40 dest='build_type', default='Release', | 42 dest='build_type', default='Release', |
41 help='Use Debug build of host tools instead of Release.') | 43 help='Use Debug build of host tools instead of Release.') |
42 | 44 |
43 options, args = parser.parse_args(argv) | 45 options, args = parser.parse_args(argv) |
44 run_tests_helper.SetLogLevel(options.verbose_count) | 46 run_tests_helper.SetLogLevel(options.verbose_count) |
45 | 47 |
46 if len(args) < 2 or not len(args) % 2: | 48 if len(args) < 2 or not len(args) % 2: |
47 parser.error('Need even number of port pairs') | 49 parser.error('Need even number of port pairs') |
48 sys.exit(1) | 50 sys.exit(1) |
49 | 51 |
50 try: | 52 try: |
51 port_pairs = map(int, args[1:]) | 53 port_pairs = map(int, args[1:]) |
52 port_pairs = zip(port_pairs[::2], port_pairs[1::2]) | 54 port_pairs = zip(port_pairs[::2], port_pairs[1::2]) |
53 except ValueError: | 55 except ValueError: |
54 parser.error('Bad port number') | 56 parser.error('Bad port number') |
55 sys.exit(1) | 57 sys.exit(1) |
56 | 58 |
57 devices = device_utils.DeviceUtils.HealthyDevices() | 59 if options.blacklist_file: |
| 60 blacklist = device_blacklist.Blacklist(options.blacklist_file) |
| 61 else: |
| 62 blacklist = None |
| 63 |
| 64 devices = device_utils.DeviceUtils.HealthyDevices(blacklist) |
58 | 65 |
59 if options.device: | 66 if options.device: |
60 device = next((d for d in devices if d == options.device), None) | 67 device = next((d for d in devices if d == options.device), None) |
61 if not device: | 68 if not device: |
62 raise device_errors.DeviceUnreachableError(options.device) | 69 raise device_errors.DeviceUnreachableError(options.device) |
63 elif devices: | 70 elif devices: |
64 device = devices[0] | 71 device = devices[0] |
65 logging.info('No device specified. Defaulting to %s', devices[0]) | 72 logging.info('No device specified. Defaulting to %s', devices[0]) |
66 else: | 73 else: |
67 raise device_errors.NoDevicesError() | 74 raise device_errors.NoDevicesError() |
68 | 75 |
69 constants.SetBuildType(options.build_type) | 76 constants.SetBuildType(options.build_type) |
70 try: | 77 try: |
71 forwarder.Forwarder.Map(port_pairs, device) | 78 forwarder.Forwarder.Map(port_pairs, device) |
72 while True: | 79 while True: |
73 time.sleep(60) | 80 time.sleep(60) |
74 except KeyboardInterrupt: | 81 except KeyboardInterrupt: |
75 sys.exit(0) | 82 sys.exit(0) |
76 finally: | 83 finally: |
77 forwarder.Forwarder.UnmapAllDevicePorts(device) | 84 forwarder.Forwarder.UnmapAllDevicePorts(device) |
78 | 85 |
79 if __name__ == '__main__': | 86 if __name__ == '__main__': |
80 main(sys.argv) | 87 main(sys.argv) |
OLD | NEW |