1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
|
# Copyright 2018 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
import logging
import os
import platform
import socket
import subprocess
import sys
DIR_SOURCE_ROOT = os.path.abspath(
os.path.join(os.path.dirname(__file__), os.pardir, os.pardir))
SDK_ROOT = os.path.join(DIR_SOURCE_ROOT, 'third_party', 'fuchsia-sdk', 'sdk')
def EnsurePathExists(path):
"""Checks that the file |path| exists on the filesystem and returns the path
if it does, raising an exception otherwise."""
if not os.path.exists(path):
raise IOError('Missing file: ' + path)
return path
def GetHostOsFromPlatform():
host_platform = sys.platform
if host_platform.startswith('linux'):
return 'linux'
elif host_platform.startswith('darwin'):
return 'mac'
raise Exception('Unsupported host platform: %s' % host_platform)
def GetHostArchFromPlatform():
host_arch = platform.machine()
if host_arch == 'x86_64':
return 'x64'
elif host_arch == 'aarch64':
return 'arm64'
raise Exception('Unsupported host architecture: %s' % host_arch)
def GetQemuRootForPlatform():
return os.path.join(DIR_SOURCE_ROOT, 'third_party',
'qemu-' + GetHostOsFromPlatform() + '-' +
GetHostArchFromPlatform())
def ConnectPortForwardingTask(target, local_port, remote_port = 0):
"""Establishes a port forwarding SSH task to a localhost TCP endpoint hosted
at port |local_port|. Blocks until port forwarding is established.
Returns the remote port number."""
forwarding_flags = ['-O', 'forward', # Send SSH mux control signal.
'-R', '%d:localhost:%d' % (remote_port, local_port),
'-v', # Get forwarded port info from stderr.
'-NT'] # Don't execute command; don't allocate terminal.
if remote_port != 0:
# Forward to a known remote port.
task = target.RunCommand([], ssh_args=forwarding_flags)
if task.returncode != 0:
raise Exception('Could not establish a port forwarding connection.')
return
task = target.RunCommandPiped([],
ssh_args=forwarding_flags,
stdout=subprocess.PIPE,
stderr=open('/dev/null'))
output = task.stdout.readlines()
task.wait()
if task.returncode != 0:
raise Exception('Got an error code when requesting port forwarding: %d' %
task.returncode)
parsed_port = int(output[0].strip())
logging.debug('Port forwarding established (local=%d, device=%d)' %
(local_port, parsed_port))
return parsed_port
def GetAvailableTcpPort():
"""Finds a (probably) open port by opening and closing a listen socket."""
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.bind(("", 0))
port = sock.getsockname()[1]
sock.close()
return port
|