cpu-cpuid: Use argparse to parse arguments

Using 'argparse' for argument handling simplifies merging this script
with cpu-gather.py in a later patch.

Signed-off-by: Tim Wiederhake <twiederh@redhat.com>
Reviewed-by: Jiri Denemark <jdenemar@redhat.com>
This commit is contained in:
Tim Wiederhake 2021-01-04 12:30:11 +01:00 committed by Jiri Denemark
parent 3c97cb2cad
commit b53eb0db35
1 changed files with 42 additions and 35 deletions

View File

@ -1,5 +1,6 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import argparse
import os import os
import sys import sys
import json import json
@ -191,18 +192,19 @@ def formatCPUData(cpuData, path, comment):
f.write("</cpudata>\n") f.write("</cpudata>\n")
def diff(cpuMap, path): def diff(args):
base = path.replace(".json", "") cpuMap = parseMap()
jsonFile = path
cpuDataFile = base + ".xml" for jsonFile in args.json_files:
enabledFile = base + "-enabled.xml" cpuDataFile = jsonFile.replace(".json", ".xml")
disabledFile = base + "-disabled.xml" enabledFile = jsonFile.replace(".json", "-enabled.xml")
disabledFile = jsonFile.replace(".json", "-disabled.xml")
cpuData = parseCPUData(cpuDataFile) cpuData = parseCPUData(cpuDataFile)
qemu = parseQemu(jsonFile, cpuMap) qemu = parseQemu(jsonFile, cpuMap)
enabled = {"cpuid": {}} enabled = dict()
disabled = {"cpuid": {}} disabled = dict()
for feature in cpuMap.values(): for feature in cpuMap.values():
if checkFeature(qemu, feature): if checkFeature(qemu, feature):
addFeature(enabled, feature) addFeature(enabled, feature)
@ -213,17 +215,22 @@ def diff(cpuMap, path):
formatCPUData(disabled, disabledFile, "Features disabled by QEMU") formatCPUData(disabled, disabledFile, "Features disabled by QEMU")
if len(sys.argv) < 3: def main():
print("Usage: %s diff json_file..." % sys.argv[0]) parser = argparse.ArgumentParser(description="Diff cpuid results")
sys.exit(1) subparsers = parser.add_subparsers(dest="action", required=True)
diffparser = subparsers.add_parser(
"diff",
help="Diff json description of CPU model against known features.")
diffparser.add_argument(
"json_files",
nargs="+",
metavar="FILE",
type=os.path.realpath,
help="Path to one or more json CPU model descriptions.")
args = parser.parse_args()
action = sys.argv[1] diff(args)
args = sys.argv[2:]
if action == "diff":
cpuMap = parseMap() if __name__ == "__main__":
for path in args: main()
diff(cpuMap, path)
else:
print("Unknown action: %s" % action)
sys.exit(1)