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 | |
15 import optparse | 14 import optparse |
16 import sys | 15 import sys |
17 import time | 16 import time |
18 | 17 |
19 import devil_chromium | 18 import devil_chromium |
20 | 19 |
21 from devil.android import device_blacklist | 20 from devil.android import device_blacklist |
22 from devil.android import device_errors | |
23 from devil.android import device_utils | 21 from devil.android import device_utils |
24 from devil.android import forwarder | 22 from devil.android import forwarder |
25 from devil.utils import run_tests_helper | 23 from devil.utils import run_tests_helper |
26 | 24 |
27 from pylib import constants | 25 from pylib import constants |
28 | 26 |
29 | 27 |
30 def main(argv): | 28 def main(argv): |
31 parser = optparse.OptionParser(usage='Usage: %prog [options] device_port ' | 29 parser = optparse.OptionParser(usage='Usage: %prog [options] device_port ' |
32 'host_port [device_port_2 host_port_2] ...', | 30 'host_port [device_port_2 host_port_2] ...', |
(...skipping 23 matching lines...) Expand all Loading... |
56 try: | 54 try: |
57 port_pairs = [int(a) for a in args[1:]] | 55 port_pairs = [int(a) for a in args[1:]] |
58 port_pairs = zip(port_pairs[::2], port_pairs[1::2]) | 56 port_pairs = zip(port_pairs[::2], port_pairs[1::2]) |
59 except ValueError: | 57 except ValueError: |
60 parser.error('Bad port number') | 58 parser.error('Bad port number') |
61 sys.exit(1) | 59 sys.exit(1) |
62 | 60 |
63 blacklist = (device_blacklist.Blacklist(options.blacklist_file) | 61 blacklist = (device_blacklist.Blacklist(options.blacklist_file) |
64 if options.blacklist_file | 62 if options.blacklist_file |
65 else None) | 63 else None) |
66 devices = device_utils.DeviceUtils.HealthyDevices(blacklist) | 64 device = device_utils.DeviceUtils.HealthyDevices(blacklist=blacklist, |
67 | 65 device_arg=options.device) |
68 if options.device: | |
69 device = next((d for d in devices if d == options.device), None) | |
70 if not device: | |
71 raise device_errors.DeviceUnreachableError(options.device) | |
72 elif devices: | |
73 device = devices[0] | |
74 logging.info('No device specified. Defaulting to %s', devices[0]) | |
75 else: | |
76 raise device_errors.NoDevicesError() | |
77 | |
78 constants.SetBuildType(options.build_type) | 66 constants.SetBuildType(options.build_type) |
79 try: | 67 try: |
80 forwarder.Forwarder.Map(port_pairs, device) | 68 forwarder.Forwarder.Map(port_pairs, device) |
81 while True: | 69 while True: |
82 time.sleep(60) | 70 time.sleep(60) |
83 except KeyboardInterrupt: | 71 except KeyboardInterrupt: |
84 sys.exit(0) | 72 sys.exit(0) |
85 finally: | 73 finally: |
86 forwarder.Forwarder.UnmapAllDevicePorts(device) | 74 forwarder.Forwarder.UnmapAllDevicePorts(device) |
87 | 75 |
88 if __name__ == '__main__': | 76 if __name__ == '__main__': |
89 main(sys.argv) | 77 main(sys.argv) |
OLD | NEW |