blob: c78f44d2ec2b8aa7b12070ebbbf3d3f3b1de50d6 [file] [log] [blame]
Peter Wenb51e4542021-06-30 17:42:571#!/usr/bin/env vpython3
[email protected]47bd7602013-01-11 16:37:172#
Avi Drissman73a09d12022-09-08 20:33:383# Copyright 2013 The Chromium Authors
[email protected]47bd7602013-01-11 16:37:174# 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
jbudorickd9bce3902016-09-23 02:39:2214import argparse
[email protected]47bd7602013-01-11 16:37:1715import sys
16import time
17
jbudorickd28554a2016-01-11 16:22:5918import devil_chromium
19
John Budorick8b57e602020-09-01 16:57:5020from devil.android import device_denylist
jbudorick061629442015-09-03 18:00:5721from devil.android import device_utils
jbudorickd645efe82015-12-11 19:09:5922from devil.android import forwarder
jbudorick061629442015-09-03 18:00:5723from devil.utils import run_tests_helper
jbudorickd28554a2016-01-11 16:22:5924
jbudorickbfffb22e2015-04-16 14:10:0225from pylib import constants
[email protected]47bd7602013-01-11 16:37:1726
27
28def main(argv):
jbudorickd9bce3902016-09-23 02:39:2229 parser = argparse.ArgumentParser(
30 usage='Usage: %(prog)s [options] device_port '
31 'host_port [device_port_2 host_port_2] ...',
32 description=__doc__)
33 parser.add_argument(
34 '-v', '--verbose',
35 dest='verbose_count',
36 default=0,
37 action='count',
38 help='Verbose level (multiple times for more)')
39 parser.add_argument(
40 '--device',
41 help='Serial number of device we should use.')
John Budorick8b57e602020-09-01 16:57:5042 parser.add_argument('--denylist-file', help='Device denylist JSON file.')
jbudorickd9bce3902016-09-23 02:39:2243 parser.add_argument(
44 '--debug',
45 action='store_const',
46 const='Debug',
47 dest='build_type',
48 default='Release',
49 help='DEPRECATED: use --output-directory instead.')
50 parser.add_argument(
51 '--output-directory',
52 help='Path to the root build directory.')
53 parser.add_argument(
54 'ports',
55 nargs='+',
56 type=int,
57 help='Port pair to reverse forward.')
[email protected]47bd7602013-01-11 16:37:1758
jbudorickd9bce3902016-09-23 02:39:2259 args = parser.parse_args(argv)
60 run_tests_helper.SetLogLevel(args.verbose_count)
[email protected]47bd7602013-01-11 16:37:1761
jbudorickd9bce3902016-09-23 02:39:2262 if len(args.ports) < 2 or len(args.ports) % 2:
[email protected]47bd7602013-01-11 16:37:1763 parser.error('Need even number of port pairs')
[email protected]47bd7602013-01-11 16:37:1764
Peter Wenb51e4542021-06-30 17:42:5765 port_pairs = list(zip(args.ports[::2], args.ports[1::2]))
[email protected]47bd7602013-01-11 16:37:1766
jbudorickd9bce3902016-09-23 02:39:2267 if args.build_type:
68 constants.SetBuildType(args.build_type)
69 if args.output_directory:
70 constants.SetOutputDirectory(args.output_directory)
71 devil_chromium.Initialize(output_directory=constants.GetOutDirectory())
72
John Budorick8b57e602020-09-01 16:57:5073 denylist = (device_denylist.Denylist(args.denylist_file)
74 if args.denylist_file else None)
75 device = device_utils.DeviceUtils.HealthyDevices(denylist=denylist,
76 device_arg=args.device)[0]
[email protected]47bd7602013-01-11 16:37:1777 try:
[email protected]044d79b2014-04-10 19:37:3078 forwarder.Forwarder.Map(port_pairs, device)
[email protected]47bd7602013-01-11 16:37:1779 while True:
80 time.sleep(60)
81 except KeyboardInterrupt:
82 sys.exit(0)
83 finally:
[email protected]044d79b2014-04-10 19:37:3084 forwarder.Forwarder.UnmapAllDevicePorts(device)
[email protected]47bd7602013-01-11 16:37:1785
86if __name__ == '__main__':
jbudorickd9bce3902016-09-23 02:39:2287 sys.exit(main(sys.argv[1:]))