# 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 signal import socket import subprocess import sys import time import threading
def EnsurePathExists(path): """Checks that the file |path| exists on the filesystem and returns the path if it does, raising an exception otherwise."""
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)
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
def SubprocessCallWithTimeout(command, silent=False, timeout_secs=None): """Helper function for running a command.
Args:
command: The command to run.
silent: Iftrue, stdout and stderr of the command will not be printed.
timeout_secs: Maximum amount of time allowed for the command to finish.
Returns:
A tuple of (return code, stdout, stderr) of the command. Raises
an exception if the subprocess times out. """
if silent:
devnull = open(os.devnull, 'w')
process = subprocess.Popen(command, stdout=devnull, stderr=devnull) else:
process = subprocess.Popen(command,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
timeout_timer = None if timeout_secs:
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.