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 9Allows an Android device to connect to services running on the host machine, 10i.e., "adb forward" in reverse. Requires |host_forwarder| and |device_forwarder| 11to be built. 12""" 13 14import optparse 15import sys 16import time 17 18from pylib import constants, forwarder 19from pylib.device import device_utils 20from pylib.utils import run_tests_helper 21 22 23def main(argv): 24 parser = optparse.OptionParser(usage='Usage: %prog [options] device_port ' 25 'host_port [device_port_2 host_port_2] ...', 26 description=__doc__) 27 parser.add_option('-v', 28 '--verbose', 29 dest='verbose_count', 30 default=0, 31 action='count', 32 help='Verbose level (multiple times for more)') 33 parser.add_option('--device', 34 help='Serial number of device we should use.') 35 parser.add_option('--debug', action='store_const', const='Debug', 36 dest='build_type', default='Release', 37 help='Use Debug build of host tools instead of Release.') 38 39 options, args = parser.parse_args(argv) 40 run_tests_helper.SetLogLevel(options.verbose_count) 41 42 if len(args) < 2 or not len(args) % 2: 43 parser.error('Need even number of port pairs') 44 sys.exit(1) 45 46 try: 47 port_pairs = map(int, args[1:]) 48 port_pairs = zip(port_pairs[::2], port_pairs[1::2]) 49 except ValueError: 50 parser.error('Bad port number') 51 sys.exit(1) 52 53 device = device_utils.DeviceUtils(options.device) 54 constants.SetBuildType(options.build_type) 55 try: 56 forwarder.Forwarder.Map(port_pairs, device) 57 while True: 58 time.sleep(60) 59 except KeyboardInterrupt: 60 sys.exit(0) 61 finally: 62 forwarder.Forwarder.UnmapAllDevicePorts(device) 63 64if __name__ == '__main__': 65 main(sys.argv) 66