xref: /qemu/python/qemu/utils/__init__.py (revision f9734d5d)
1"""
2QEMU development and testing utilities
3
4This package provides a small handful of utilities for performing
5various tasks not directly related to the launching of a VM.
6"""
7
8# Copyright (C) 2021 Red Hat Inc.
9#
10# Authors:
11#  John Snow <jsnow@redhat.com>
12#  Cleber Rosa <crosa@redhat.com>
13#
14# This work is licensed under the terms of the GNU GPL, version 2.  See
15# the COPYING file in the top-level directory.
16#
17
18import re
19from typing import Optional
20
21# pylint: disable=import-error
22from .accel import kvm_available, list_accel, tcg_available
23
24
25__all__ = (
26    'get_info_usernet_hostfwd_port',
27    'kvm_available',
28    'list_accel',
29    'tcg_available',
30)
31
32
33def get_info_usernet_hostfwd_port(info_usernet_output: str) -> Optional[int]:
34    """
35    Returns the port given to the hostfwd parameter via info usernet
36
37    :param info_usernet_output: output generated by hmp command "info usernet"
38    :return: the port number allocated by the hostfwd option
39    """
40    for line in info_usernet_output.split('\r\n'):
41        regex = r'TCP.HOST_FORWARD.*127\.0\.0\.1\s+(\d+)\s+10\.'
42        match = re.search(regex, line)
43        if match is not None:
44            return int(match[1])
45    return None
46