| 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 optparse | 14 import argparse |
| 15 import sys | 15 import sys |
| 16 import time | 16 import time |
| 17 | 17 |
| 18 import devil_chromium | 18 import devil_chromium |
| 19 | 19 |
| 20 from devil.android import device_blacklist | 20 from devil.android import device_blacklist |
| 21 from devil.android import device_utils | 21 from devil.android import device_utils |
| 22 from devil.android import forwarder | 22 from devil.android import forwarder |
| 23 from devil.utils import run_tests_helper | 23 from devil.utils import run_tests_helper |
| 24 | 24 |
| 25 from pylib import constants | 25 from pylib import constants |
| 26 | 26 |
| 27 | 27 |
| 28 def main(argv): | 28 def main(argv): |
| 29 parser = optparse.OptionParser(usage='Usage: %prog [options] device_port ' | 29 parser = argparse.ArgumentParser( |
| 30 'host_port [device_port_2 host_port_2] ...', | 30 usage='Usage: %(prog)s [options] device_port ' |
| 31 description=__doc__) | 31 'host_port [device_port_2 host_port_2] ...', |
| 32 parser.add_option('-v', | 32 description=__doc__) |
| 33 '--verbose', | 33 parser.add_argument( |
| 34 dest='verbose_count', | 34 '-v', '--verbose', |
| 35 default=0, | 35 dest='verbose_count', |
| 36 action='count', | 36 default=0, |
| 37 help='Verbose level (multiple times for more)') | 37 action='count', |
| 38 parser.add_option('--device', | 38 help='Verbose level (multiple times for more)') |
| 39 help='Serial number of device we should use.') | 39 parser.add_argument( |
| 40 parser.add_option('--blacklist-file', help='Device blacklist JSON file.') | 40 '--device', |
| 41 parser.add_option('--debug', action='store_const', const='Debug', | 41 help='Serial number of device we should use.') |
| 42 dest='build_type', default='Release', | 42 parser.add_argument( |
| 43 help='Use Debug build of host tools instead of Release.') | 43 '--blacklist-file', |
| 44 help='Device blacklist JSON file.') |
| 45 parser.add_argument( |
| 46 '--debug', |
| 47 action='store_const', |
| 48 const='Debug', |
| 49 dest='build_type', |
| 50 default='Release', |
| 51 help='DEPRECATED: use --output-directory instead.') |
| 52 parser.add_argument( |
| 53 '--output-directory', |
| 54 help='Path to the root build directory.') |
| 55 parser.add_argument( |
| 56 'ports', |
| 57 nargs='+', |
| 58 type=int, |
| 59 help='Port pair to reverse forward.') |
| 44 | 60 |
| 45 options, args = parser.parse_args(argv) | 61 args = parser.parse_args(argv) |
| 46 run_tests_helper.SetLogLevel(options.verbose_count) | 62 run_tests_helper.SetLogLevel(args.verbose_count) |
| 47 | 63 |
| 48 devil_chromium.Initialize() | 64 if len(args.ports) < 2 or len(args.ports) % 2: |
| 65 parser.error('Need even number of port pairs') |
| 49 | 66 |
| 50 if len(args) < 2 or not len(args) % 2: | 67 port_pairs = zip(args.ports[::2], args.ports[1::2]) |
| 51 parser.error('Need even number of port pairs') | |
| 52 sys.exit(1) | |
| 53 | 68 |
| 54 try: | 69 if args.build_type: |
| 55 port_pairs = [int(a) for a in args[1:]] | 70 constants.SetBuildType(args.build_type) |
| 56 port_pairs = zip(port_pairs[::2], port_pairs[1::2]) | 71 if args.output_directory: |
| 57 except ValueError: | 72 constants.SetOutputDirectory(args.output_directory) |
| 58 parser.error('Bad port number') | 73 devil_chromium.Initialize(output_directory=constants.GetOutDirectory()) |
| 59 sys.exit(1) | |
| 60 | 74 |
| 61 blacklist = (device_blacklist.Blacklist(options.blacklist_file) | 75 blacklist = (device_blacklist.Blacklist(args.blacklist_file) |
| 62 if options.blacklist_file | 76 if args.blacklist_file |
| 63 else None) | 77 else None) |
| 64 device = device_utils.DeviceUtils.HealthyDevices( | 78 device = device_utils.DeviceUtils.HealthyDevices( |
| 65 blacklist=blacklist, device_arg=options.device)[0] | 79 blacklist=blacklist, device_arg=args.device)[0] |
| 66 constants.SetBuildType(options.build_type) | |
| 67 try: | 80 try: |
| 68 forwarder.Forwarder.Map(port_pairs, device) | 81 forwarder.Forwarder.Map(port_pairs, device) |
| 69 while True: | 82 while True: |
| 70 time.sleep(60) | 83 time.sleep(60) |
| 71 except KeyboardInterrupt: | 84 except KeyboardInterrupt: |
| 72 sys.exit(0) | 85 sys.exit(0) |
| 73 finally: | 86 finally: |
| 74 forwarder.Forwarder.UnmapAllDevicePorts(device) | 87 forwarder.Forwarder.UnmapAllDevicePorts(device) |
| 75 | 88 |
| 76 if __name__ == '__main__': | 89 if __name__ == '__main__': |
| 77 main(sys.argv) | 90 sys.exit(main(sys.argv[1:])) |
| OLD | NEW |