Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 8FE1DC43334 for ; Fri, 1 Jul 2022 19:09:33 +0000 (UTC) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id F02F484498; Fri, 1 Jul 2022 21:09:29 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="k1XLzKLZ"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id A0ED18449B; Fri, 1 Jul 2022 21:09:28 +0200 (CEST) Received: from mail-ed1-x534.google.com (mail-ed1-x534.google.com [IPv6:2a00:1450:4864:20::534]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id E044084492 for ; Fri, 1 Jul 2022 21:09:25 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=alpernebiyasak@gmail.com Received: by mail-ed1-x534.google.com with SMTP id c13so4034067eds.10 for ; Fri, 01 Jul 2022 12:09:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=message-id:date:mime-version:user-agent:from:subject:to:cc :references:content-language:in-reply-to:content-transfer-encoding; bh=QzzKbm4lrF6onhw/RFvY8jRapwUh5vQ+s542V52wlpc=; b=k1XLzKLZTShiuZTQ4B2oUi/mw6Z7iU+HdhnAzJGcsMXuRjjT8pD/1ZCD2nLrMliRAY pcCN2CTFF2n2j7cGPH3btBiBLT/c/78FNJrBbVv2ktLjZmQ0uSmLqX2b/+rf1wzHgnb/ 7SH8rtw9PpCnEB79ZhNth1rOwaBhwag9ndQto7YvH5OgtEUCnRMkFG/dF3GRSvdJ7ALV xpFEEARS4NOxq7u3ko6UsAW9lJKOPbg8T6wWZP3po4fc6+AlGkyXrtr0iYh/GSBIWM8H kqrRnk/k1s2U02ZvK5mH+yzCuo4AEwQPUBFqNz3BEMq9mQtjn/Dvo+LApa+z1RNrKKIQ V+eg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:date:mime-version:user-agent:from :subject:to:cc:references:content-language:in-reply-to :content-transfer-encoding; bh=QzzKbm4lrF6onhw/RFvY8jRapwUh5vQ+s542V52wlpc=; b=eyfRV+abPRDVeXGzO9QFQ+txXp/RmTp7on7NRzcCrJuM/hiXnXrpQcgpn3Q8b9v/B+ 5PmTKLhfwr5wzd3GS6kX+WFjMqxHfDq2w0T5srvjopyzcnOmgiroD5Jl2c9t/rP4ulrr q8PdgblpivowO+LbSyS/yakgcP/c4ARsjnbJWhlo85lf2L823KL3j3t+DQjPyL09cwOV TBgM45ilAtaAqaQTWT+pqO6ZPOP8eEORl27tlY3Bek3c+38ivINoFtz6bkiGRlCfIHRm u5Lzu6sb3Fa+KgNuncHd/VrIGxV1BOfHdaH4tJrK101IEPPwElDV1t4aymAI5uN7QXcN plWA== X-Gm-Message-State: AJIora90IzC5quhMSO/rXg+qHTe3yFJ3LwZyskBLCvmMnGFxZcez20nT UCEdEkzD6M43yMiVQjPSVWQ= X-Google-Smtp-Source: AGRyM1voqREgrR5pZDIm6N/TkAacOVbAYXu3Evq23gWoM9V650JFMhpGeFIr5CK1wd9IxMENQf3tPw== X-Received: by 2002:a05:6402:299a:b0:435:c75:64e6 with SMTP id eq26-20020a056402299a00b004350c7564e6mr21144939edb.134.1656702565480; Fri, 01 Jul 2022 12:09:25 -0700 (PDT) Received: from [192.168.0.74] ([178.233.178.185]) by smtp.gmail.com with ESMTPSA id w20-20020aa7dcd4000000b0042dc882c823sm15271946edu.70.2022.07.01.12.09.22 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 01 Jul 2022 12:09:25 -0700 (PDT) Message-ID: <23350c36-3dc2-da12-c790-d9bd0cbb1818@gmail.com> Date: Fri, 1 Jul 2022 22:07:20 +0300 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux aarch64; rv:91.0) Gecko/20100101 Thunderbird/91.10.0 From: Alper Nebi Yasak Subject: Re: [PATCH RFC v3 02/11] ti: tools: config: Add board config class to generate config binaries To: Neha Malcom Francis Cc: u-boot@lists.denx.de, afd@ti.com, trini@konsulko.com, rogerq@kernel.org, a-govindraju@ti.com, vigneshr@ti.com References: <20220615064804.29553-1-n-francis@ti.com> <20220615064804.29553-3-n-francis@ti.com> Content-Language: en-US In-Reply-To: <20220615064804.29553-3-n-francis@ti.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.6 at phobos.denx.de X-Virus-Status: Clean On 15/06/2022 09:47, Neha Malcom Francis wrote: > For validating config files and generating binary config artifacts, here > board specific config class is added. > > Add function cfgBinaryGen() in tibcfg_gen.py. It uses TIBoardConfig > class to load given schema and config files in YAML, validate them and > generate binaries. > > Signed-off-by: Tarun Sahu > [n-francis@ti.com: prepared patch for upstreaming] > Signed-off-by: Neha Malcom Francis > --- > test/py/requirements.txt | 1 + > tools/tibcfg_gen.py | 114 +++++++++++++++++++++++++++++++++++++++ > 2 files changed, 115 insertions(+) > create mode 100644 tools/tibcfg_gen.py > > diff --git a/test/py/requirements.txt b/test/py/requirements.txt > index 33c5c0bbc4..a91ba64563 100644 > --- a/test/py/requirements.txt > +++ b/test/py/requirements.txt > @@ -4,6 +4,7 @@ coverage==4.5.4 > extras==1.0.0 > fixtures==3.0.0 > importlib-metadata==0.23 > +jsonschema==4.0.0 > linecache2==1.0.0 > more-itertools==7.2.0 > packaging==19.2 > diff --git a/tools/tibcfg_gen.py b/tools/tibcfg_gen.py > new file mode 100644 > index 0000000000..e5fa2690c8 > --- /dev/null > +++ b/tools/tibcfg_gen.py > @@ -0,0 +1,114 @@ > +# SPDX-License-Identifier: GPL-2.0+ > +# Copyright (C) 2022 Texas Instruments Incorporated - https://www.ti.com/ > +# > +# TI Board Configuration Class for Schema Validation and Binary Generation > +# > + > +import os > +import getopt > +import sys > + > +import yaml > + > +from jsonschema import validate > + > + > +class TIBoardConfig: > + > + """ Texas Instruments Board Configuration File""" > + > + def __init__(self, file, schema, data_rules=""): > + """Load a YAML configuration file and YAML schema > + > + Validation of the config file against the schema is also done.""" > + with open(file, 'r') as f: > + self.file_yaml = yaml.safe_load(f) > + with open(schema, 'r') as sch: > + self.schema_yaml = yaml.safe_load(sch) > + self.data_rules = data_rules > + try: > + validate(self.file_yaml, self.schema_yaml) > + except Exception as e: > + print(e) Don't catch the exception here, so that we never have a non-validated TIBoardConfig object. Instead, catch it in cfgBinaryGen() below to report the validation error. > + > + def _convert_to_byte_chunk(self, val, data_type): > + """Convert value into byte array""" > + size = 0 > + if(data_type == "#/definitions/u8"): > + size = 1 > + elif(data_type == "#/definitions/u16"): > + size = 2 > + elif(data_type == "#/definitions/u32"): > + size = 4 Parentheses are unnecessary for these as well. > + else: > + raise Exception("Data type not present in definitions") > + if type(val) == int: > + br = val.to_bytes(size, byteorder="little") > + return br > + > + def _compile_yaml(self, schema_yaml, file_yaml): > + """Convert YAML file into byte array based on YAML schema""" > + br = bytearray() > + for key in file_yaml.keys(): > + node = file_yaml[key] > + node_schema = schema_yaml['properties'][key] > + node_type = node_schema.get('type') > + if not 'type' in node_schema: > + br += self._convert_to_byte_chunk(node, > + node_schema.get('$ref')) > + elif node_type == 'object': > + br += self._compile_yaml(node_schema, node) > + elif node_type == 'array': > + for item in node: > + if not isinstance(item, dict): > + br += self._convert_to_byte_chunk( > + item, schema_yaml['properties'][key]['items']["$ref"]) > + else: > + br += self._compile_yaml(node_schema.get('items'), item) > + return br > + > + def generate_binaries(self, out_path=""): > + """Generate config binary artifacts from the loaded YAML configuration file""" > + if not os.path.isdir(out_path): > + os.mkdir(out_path) > + for key in self.file_yaml.keys(): > + node = self.file_yaml[key] > + node_schema = self.schema_yaml['properties'][key] > + br = self._compile_yaml(node_schema, node) > + path = os.path.join(out_path, key + ".bin") > + with open(path, 'wb') as cfg: > + cfg.write(br) > + > + def delete_binaries(self, out_path=""): > + """Delete generated binaries""" > + if os.path.isdir(out_path): > + for key in self.file_yaml.keys(): > + path = os.path.join(out_path, key + ".bin") > + if os.path.isfile(path): > + os.remove(path) > + > + > +def cfgBinaryGen(): > + """Generate config binaries from YAML config file and YAML schema > + Arguments: > + - config_yaml: board config file in YAML > + - schema_yaml: schema file in YAML to validate config_yaml against > + - output_dir: output directory where generated binaries can be populated > + Pass the arguments along with the filename in the Makefile. > + """ > + opts, args = getopt.getopt(sys.argv[1:], "c:s:o") I'd prefer argparse, with both long/short forms of arguments and proper help messages. > + for opt, val in opts: > + if opt == "-c": > + config_yaml = val > + elif opt == "-s": > + schema_yaml = val > + elif opt == "-o": > + output_dir = os.path.abspath(val) > + try: > + tibcfg = TIBoardConfig(config_yaml, schema_yaml) > + tibcfg.generate_binaries(output_dir) > + except: > + raise ValueError("Could not find config files!") > + > + > +cfgBinaryGen() This needs to be if __name__ == "__main__": cfgBinaryGen() or it will be run when anything uses 'import tibcfg_gen' in Python.