Skip to content

Commit

Permalink
Merge pull request #3913 from luckyh/fix-qemu-cpu-model
Browse files Browse the repository at this point in the history
cpu: adapt help text parsing to support modern qemus
  • Loading branch information
luckyh authored May 27, 2024
2 parents 42d0e39 + f0424ee commit d688cc7
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions virttest/cpu.py
Original file line number Diff line number Diff line change
Expand Up @@ -1050,6 +1050,42 @@ def _make_up_pattern(flags):
return cpu_support_model


def parse_qemu_cpu_models_modern(help_text):
"""
Get all cpu models from qemu -cpu help text.
Note: not completely supported for qemu version prior to v9.0.0
due to not all the hardware architectures printed cpu model
descriptions with using the pattern below.
Available CPUs:
...
:param help_text: Text produced by <qemu> -cpu '?'.
:return: List of cpu models.
"""
header_words = ("Available", "CPUs:")

model_list = []
search = False
for line in help_text.splitlines():
columns = re.split(r"\s+", line)
if tuple(columns) == header_words:
search = True
continue
if search:
if len(columns) == 1 and not columns[0]:
continue
elif columns[0] in ("", "x86", "PowerPC", "s390"):
model_list.append(columns[1])
continue
else:
# assumed the target section only contains
# either model descriptions or empty lines
break
return model_list


def extract_qemu_cpu_models(qemu_cpu_help_text):
"""
Get all cpu models from qemu -cpu help text.
Expand All @@ -1058,6 +1094,9 @@ def extract_qemu_cpu_models(qemu_cpu_help_text):
:return: list of cpu models
"""

if qemu_cpu_help_text.startswith("Available CPUs"):
return parse_qemu_cpu_models_modern(qemu_cpu_help_text)

def check_model_list(pattern):
cpu_re = re.compile(pattern)
qemu_cpu_model_list = cpu_re.findall(qemu_cpu_help_text)
Expand Down

0 comments on commit d688cc7

Please sign in to comment.