Zane Shelley | 352293d | 2023-04-06 17:38:15 -0500 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | |
| 3 | import argparse |
| 4 | import glob |
| 5 | import os |
| 6 | |
| 7 | from pyprd.chip_data.binary import binary_encode |
| 8 | from pyprd.chip_data.json import json_decode |
| 9 | from pyprd.chip_data.peltool import peltool_encode |
| 10 | |
| 11 | |
| 12 | def _import_chip_data(indir: str) -> dict: |
| 13 | """ |
| 14 | Iterates each JSON file in the given directory (all of which should be |
| 15 | properly formated Chip Data JSON). Then, combines the data into a singular |
| 16 | chip_data.Base object for each model/EC. |
| 17 | """ |
| 18 | |
| 19 | bases = dict() |
| 20 | |
| 21 | for file in glob.glob(os.path.join(indir, "*.json")): |
| 22 | with open(file, "r") as fp: |
| 23 | for base in json_decode(fp).split(): |
| 24 | # Should be only one model/EC |
| 25 | assert 1 == len(base.model_ec) |
| 26 | model_ec = base.model_ec[0] |
| 27 | |
| 28 | if model_ec not in bases: |
| 29 | bases[model_ec] = base |
| 30 | else: |
| 31 | bases[model_ec].merge(base) |
| 32 | |
| 33 | return bases |
| 34 | |
| 35 | |
| 36 | def gen_chip_data_binary(indir: str, outdir: str) -> None: |
| 37 | for model_ec, base in _import_chip_data(indir).items(): |
| 38 | file = f"chip_data_{model_ec.lower()}.cdb" |
| 39 | with open(os.path.join(outdir, file), "wb") as fp: |
| 40 | binary_encode(model_ec, base, fp) |
| 41 | |
| 42 | |
| 43 | def gen_peltool_json(indir: str, outdir: str) -> None: |
| 44 | for model_ec, base in _import_chip_data(indir).items(): |
| 45 | file = f"pel_parser_data_{model_ec.lower()}.json" |
| 46 | with open(os.path.join(outdir, file), "w") as fp: |
| 47 | peltool_encode(model_ec, base, fp) |
| 48 | |
| 49 | |
| 50 | if __name__ == "__main__": |
| 51 | parser = argparse.ArgumentParser( |
| 52 | description=""" |
| 53 | Parses the target Chip Data files and generates specified data files. |
| 54 | """ |
| 55 | ) |
| 56 | |
| 57 | parser.add_argument( |
| 58 | "format", |
| 59 | type=str, |
| 60 | metavar="<format>", |
| 61 | choices=["bin", "pel"], |
| 62 | help=""" |
| 63 | Output data type: 'bin' => Chip Data binary, 'pel' => BMC peltool JSON. |
| 64 | """, |
| 65 | ) |
| 66 | |
| 67 | parser.add_argument( |
| 68 | "indir", |
| 69 | type=str, |
| 70 | metavar="<input_path>", |
| 71 | help=""" |
| 72 | Input directory containing the Chip Data files. |
| 73 | """, |
| 74 | ) |
| 75 | |
| 76 | parser.add_argument( |
| 77 | "outdir", |
| 78 | type=str, |
| 79 | metavar="<output_path>", |
| 80 | help=""" |
| 81 | Output directory for the generated data. |
| 82 | """, |
| 83 | ) |
| 84 | |
| 85 | args = parser.parse_args() |
| 86 | |
| 87 | funcs = { |
| 88 | "bin": "gen_chip_data_binary", |
| 89 | "pel": "gen_peltool_json", |
| 90 | } |
| 91 | |
| 92 | globals()[funcs[args.format]](args.indir, args.outdir) |