| 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 from pylib import constants | 18 from pylib import android_commands |
| 20 from pylib import forwarder | 19 from pylib import constants, forwarder |
| 21 from pylib.device import adb_wrapper | |
| 22 from pylib.device import device_filter | |
| 23 from pylib.device import device_utils | 20 from pylib.device import device_utils |
| 24 from pylib.utils import run_tests_helper | 21 from pylib.utils import run_tests_helper |
| 25 | 22 |
| 26 | 23 |
| 27 def main(argv): | 24 def main(argv): |
| 28 parser = optparse.OptionParser(usage='Usage: %prog [options] device_port ' | 25 parser = optparse.OptionParser(usage='Usage: %prog [options] device_port ' |
| 29 'host_port [device_port_2 host_port_2] ...', | 26 'host_port [device_port_2 host_port_2] ...', |
| 30 description=__doc__) | 27 description=__doc__) |
| 31 parser.add_option('-v', | 28 parser.add_option('-v', |
| 32 '--verbose', | 29 '--verbose', |
| (...skipping 14 matching lines...) Expand all Loading... |
| 47 parser.error('Need even number of port pairs') | 44 parser.error('Need even number of port pairs') |
| 48 sys.exit(1) | 45 sys.exit(1) |
| 49 | 46 |
| 50 try: | 47 try: |
| 51 port_pairs = map(int, args[1:]) | 48 port_pairs = map(int, args[1:]) |
| 52 port_pairs = zip(port_pairs[::2], port_pairs[1::2]) | 49 port_pairs = zip(port_pairs[::2], port_pairs[1::2]) |
| 53 except ValueError: | 50 except ValueError: |
| 54 parser.error('Bad port number') | 51 parser.error('Bad port number') |
| 55 sys.exit(1) | 52 sys.exit(1) |
| 56 | 53 |
| 57 devices = adb_wrapper.AdbWrapper.Devices( | 54 devices = android_commands.GetAttachedDevices() |
| 58 filters=device_filter.DefaultFilters()) | |
| 59 | 55 |
| 60 if options.device: | 56 if options.device: |
| 61 if options.device not in [d.GetDeviceSerial() for d in devices]: | 57 if options.device not in devices: |
| 62 raise Exception('Error: %s not in attached devices %s' % (options.device, | 58 raise Exception('Error: %s not in attached devices %s' % (options.device, |
| 63 ','.join(devices))) | 59 ','.join(devices))) |
| 64 devices = [options.device] | 60 devices = [options.device] |
| 65 else: | 61 else: |
| 66 if not devices: | 62 if not devices: |
| 67 raise Exception('Error: no connected devices') | 63 raise Exception('Error: no connected devices') |
| 68 logging.info('No device specified. Defaulting to %s', devices[0]) | 64 print "No device specified. Defaulting to " + devices[0] |
| 69 | 65 |
| 70 device = device_utils.DeviceUtils(devices[0]) | 66 device = device_utils.DeviceUtils(devices[0]) |
| 71 constants.SetBuildType(options.build_type) | 67 constants.SetBuildType(options.build_type) |
| 72 try: | 68 try: |
| 73 forwarder.Forwarder.Map(port_pairs, device) | 69 forwarder.Forwarder.Map(port_pairs, device) |
| 74 while True: | 70 while True: |
| 75 time.sleep(60) | 71 time.sleep(60) |
| 76 except KeyboardInterrupt: | 72 except KeyboardInterrupt: |
| 77 sys.exit(0) | 73 sys.exit(0) |
| 78 finally: | 74 finally: |
| 79 forwarder.Forwarder.UnmapAllDevicePorts(device) | 75 forwarder.Forwarder.UnmapAllDevicePorts(device) |
| 80 | 76 |
| 81 if __name__ == '__main__': | 77 if __name__ == '__main__': |
| 82 main(sys.argv) | 78 main(sys.argv) |
| OLD | NEW |