adb_reverse_forwarder.py revision a36e5920737c6adbddd3e43b760e5de8431db6e0
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 android_commands, forwarder 19from pylib.utils import run_tests_helper 20 21 22def main(argv): 23 parser = optparse.OptionParser(usage='Usage: %prog [options] device_port ' 24 'host_port [device_port_2 host_port_2] ...', 25 description=__doc__) 26 parser.add_option('-v', 27 '--verbose', 28 dest='verbose_count', 29 default=0, 30 action='count', 31 help='Verbose level (multiple times for more)') 32 parser.add_option('--device', 33 help='Serial number of device we should use.') 34 parser.add_option('--debug', action='store_const', const='Debug', 35 dest='build_type', default='Release', 36 help='Use Debug build of host tools instead of Release.') 37 38 options, args = parser.parse_args(argv) 39 run_tests_helper.SetLogLevel(options.verbose_count) 40 41 if len(args) < 2 or not len(args) % 2: 42 parser.error('Need even number of port pairs') 43 sys.exit(1) 44 45 try: 46 port_pairs = map(int, args[1:]) 47 port_pairs = zip(port_pairs[::2], port_pairs[1::2]) 48 except ValueError: 49 parser.error('Bad port number') 50 sys.exit(1) 51 52 adb = android_commands.AndroidCommands(options.device) 53 try: 54 forwarder.Forwarder.Map(port_pairs, adb, options.build_type) 55 while True: 56 time.sleep(60) 57 except KeyboardInterrupt: 58 sys.exit(0) 59 finally: 60 forwarder.Forwarder.UnmapAllDevicePorts(adb) 61 62if __name__ == '__main__': 63 main(sys.argv) 64