blob: e000d5d251564146fb73aebc848b63771e0ab1db [file] [log] [blame]
#!/usr/bin/env python3
import argparse
import glob
import os
from pyprd.chip_data.binary import binary_encode
from pyprd.chip_data.json import json_decode
from pyprd.chip_data.peltool import peltool_encode
def _import_chip_data(indir: str) -> dict:
"""
Iterates each JSON file in the given directory (all of which should be
properly formated Chip Data JSON). Then, combines the data into a singular
chip_data.Base object for each model/EC.
"""
bases = dict()
for file in glob.glob(os.path.join(indir, "*.json")):
with open(file, "r") as fp:
for base in json_decode(fp).split():
# Should be only one model/EC
assert 1 == len(base.model_ec)
model_ec = base.model_ec[0]
if model_ec not in bases:
bases[model_ec] = base
else:
bases[model_ec].merge(base)
return bases
def gen_chip_data_binary(indir: str, outdir: str) -> None:
for model_ec, base in _import_chip_data(indir).items():
file = f"chip_data_{model_ec.lower()}.cdb"
with open(os.path.join(outdir, file), "wb") as fp:
binary_encode(model_ec, base, fp)
def gen_peltool_json(indir: str, outdir: str) -> None:
for model_ec, base in _import_chip_data(indir).items():
file = f"pel_parser_data_{model_ec.lower()}.json"
with open(os.path.join(outdir, file), "w") as fp:
peltool_encode(model_ec, base, fp)
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="""
Parses the target Chip Data files and generates specified data files.
"""
)
parser.add_argument(
"format",
type=str,
metavar="<format>",
choices=["bin", "pel"],
help="""
Output data type: 'bin' => Chip Data binary, 'pel' => BMC peltool JSON.
""",
)
parser.add_argument(
"indir",
type=str,
metavar="<input_path>",
help="""
Input directory containing the Chip Data files.
""",
)
parser.add_argument(
"outdir",
type=str,
metavar="<output_path>",
help="""
Output directory for the generated data.
""",
)
args = parser.parse_args()
funcs = {
"bin": "gen_chip_data_binary",
"pel": "gen_peltool_json",
}
globals()[funcs[args.format]](args.indir, args.outdir)