Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # |
| 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. |
| 6 | |
| 7 | """Command line tool for forwarding ports from a device to the host. |
| 8 | |
| 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 | """ |
| 13 | |
| 14 | import optparse |
| 15 | import sys |
| 16 | import time |
| 17 | |
| 18 | import devil_chromium |
| 19 | |
| 20 | from devil.android import device_blacklist |
| 21 | from devil.android import device_utils |
| 22 | from devil.android import forwarder |
| 23 | from devil.utils import run_tests_helper |
| 24 | |
| 25 | from pylib import constants |
| 26 | |
| 27 | |
| 28 | def main(argv): |
| 29 | parser = optparse.OptionParser(usage='Usage: %prog [options] device_port ' |
| 30 | 'host_port [device_port_2 host_port_2] ...', |
| 31 | description=__doc__) |
| 32 | parser.add_option('-v', |
| 33 | '--verbose', |
| 34 | dest='verbose_count', |
| 35 | default=0, |
| 36 | action='count', |
| 37 | help='Verbose level (multiple times for more)') |
| 38 | parser.add_option('--device', |
| 39 | help='Serial number of device we should use.') |
| 40 | parser.add_option('--blacklist-file', help='Device blacklist JSON file.') |
| 41 | parser.add_option('--debug', action='store_const', const='Debug', |
| 42 | dest='build_type', default='Release', |
| 43 | help='Use Debug build of host tools instead of Release.') |
| 44 | |
| 45 | options, args = parser.parse_args(argv) |
| 46 | run_tests_helper.SetLogLevel(options.verbose_count) |
| 47 | |
| 48 | devil_chromium.Initialize() |
| 49 | |
| 50 | if len(args) < 2 or not len(args) % 2: |
| 51 | parser.error('Need even number of port pairs') |
| 52 | sys.exit(1) |
| 53 | |
| 54 | try: |
| 55 | port_pairs = [int(a) for a in args[1:]] |
| 56 | port_pairs = zip(port_pairs[::2], port_pairs[1::2]) |
| 57 | except ValueError: |
| 58 | parser.error('Bad port number') |
| 59 | sys.exit(1) |
| 60 | |
| 61 | blacklist = (device_blacklist.Blacklist(options.blacklist_file) |
| 62 | if options.blacklist_file |
| 63 | else None) |
| 64 | device = device_utils.DeviceUtils.HealthyDevices( |
| 65 | blacklist=blacklist, device_arg=options.device)[0] |
| 66 | constants.SetBuildType(options.build_type) |
| 67 | try: |
| 68 | forwarder.Forwarder.Map(port_pairs, device) |
| 69 | while True: |
| 70 | time.sleep(60) |
| 71 | except KeyboardInterrupt: |
| 72 | sys.exit(0) |
| 73 | finally: |
| 74 | forwarder.Forwarder.UnmapAllDevicePorts(device) |
| 75 | |
| 76 | if __name__ == '__main__': |
| 77 | main(sys.argv) |