Chromium Code Reviews| 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 optparse |
| 15 import sys | 15 import sys |
| 16 import time | 16 import time |
| 17 | 17 |
| 18 from pylib import android_commands | |
| 18 from pylib import constants, forwarder | 19 from pylib import constants, forwarder |
| 19 from pylib.device import device_utils | 20 from pylib.device import device_utils |
| 20 from pylib.utils import run_tests_helper | 21 from pylib.utils import run_tests_helper |
| 21 | 22 |
| 22 | 23 |
| 23 def main(argv): | 24 def main(argv): |
| 24 parser = optparse.OptionParser(usage='Usage: %prog [options] device_port ' | 25 parser = optparse.OptionParser(usage='Usage: %prog [options] device_port ' |
| 25 'host_port [device_port_2 host_port_2] ...', | 26 'host_port [device_port_2 host_port_2] ...', |
| 26 description=__doc__) | 27 description=__doc__) |
| 27 parser.add_option('-v', | 28 parser.add_option('-v', |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 43 parser.error('Need even number of port pairs') | 44 parser.error('Need even number of port pairs') |
| 44 sys.exit(1) | 45 sys.exit(1) |
| 45 | 46 |
| 46 try: | 47 try: |
| 47 port_pairs = map(int, args[1:]) | 48 port_pairs = map(int, args[1:]) |
| 48 port_pairs = zip(port_pairs[::2], port_pairs[1::2]) | 49 port_pairs = zip(port_pairs[::2], port_pairs[1::2]) |
| 49 except ValueError: | 50 except ValueError: |
| 50 parser.error('Bad port number') | 51 parser.error('Bad port number') |
| 51 sys.exit(1) | 52 sys.exit(1) |
| 52 | 53 |
| 53 device = device_utils.DeviceUtils(options.device) | 54 devices = android_commands.GetAttachedDevices() |
| 55 | |
| 56 if options.device: | |
| 57 if options.device not in devices: | |
| 58 raise Exception('Error: %s not in attached devices %s' % (options.device, | |
| 59 ','.join(devices))) | |
| 60 devices = [options.device] | |
| 61 | |
| 62 if not devices: | |
|
jbudorick
2014/11/03 23:22:50
nit: shuffle this around a little s.t. you print t
danduong
2014/11/04 00:21:47
Done.
| |
| 63 raise Exception('Error: no connected devices') | |
| 64 if len(devices) > 1: | |
| 65 print("No device specified. Defaulting to " + devices[0]) | |
| 66 | |
| 67 device = device_utils.DeviceUtils(devices[0]) | |
| 54 constants.SetBuildType(options.build_type) | 68 constants.SetBuildType(options.build_type) |
| 55 try: | 69 try: |
| 56 forwarder.Forwarder.Map(port_pairs, device) | 70 forwarder.Forwarder.Map(port_pairs, device) |
| 57 while True: | 71 while True: |
| 58 time.sleep(60) | 72 time.sleep(60) |
| 59 except KeyboardInterrupt: | 73 except KeyboardInterrupt: |
| 60 sys.exit(0) | 74 sys.exit(0) |
| 61 finally: | 75 finally: |
| 62 forwarder.Forwarder.UnmapAllDevicePorts(device) | 76 forwarder.Forwarder.UnmapAllDevicePorts(device) |
| 63 | 77 |
| 64 if __name__ == '__main__': | 78 if __name__ == '__main__': |
| 65 main(sys.argv) | 79 main(sys.argv) |
| OLD | NEW |