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 9934FC433F5 for ; Mon, 18 Apr 2022 19:58:20 +0000 (UTC) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 00FC783A9D; Mon, 18 Apr 2022 21:58:18 +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="DC+AZmAz"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id B558C839A7; Mon, 18 Apr 2022 21:58:16 +0200 (CEST) Received: from mail-ej1-x629.google.com (mail-ej1-x629.google.com [IPv6:2a00:1450:4864:20::629]) (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 7EA1D83A9F for ; Mon, 18 Apr 2022 21:58:05 +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-ej1-x629.google.com with SMTP id y10so11346036ejw.8 for ; Mon, 18 Apr 2022 12:58:05 -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=c+2nsNeUxetyVWrxCKvytpZnhJzNNZpqa/Pv67sy54k=; b=DC+AZmAzWGLH/lgRZsl7Ogki0eYl1B0tNxFu7R7Mr8ifXE46lUvuCrPLV34VVljtVj BcuYr2eBCLSk9ONGRt3EjekN2YoH9wY7IEP9uGzYlb7Up9T8pEql1xPrSRG5rT1VFtPn ZDe88+F1pjD7SFIIGLTae5hkjj2roufzPisMWbw81SnWi2jUQp8VUbYX/hSmaBnIHi/l WYI4dnnhBvnWp0EExxk/SYjXCrqq9QxJaTqLXwcCKm8oSXXWUc95D0CEFTAs7LWljJnB kMijYg2GtRTmYaE74w0bzHr7gF6KuZo/UMq/MHFaUOfv5ZBk3aP+INc093XR2jJRjaGd P0WA== 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=c+2nsNeUxetyVWrxCKvytpZnhJzNNZpqa/Pv67sy54k=; b=AEbkUemzeLaj1Qhp0w3WpxYQOSfhAQasJZ3jBg/5NcORHO6N1u4MFD/uxppLOiWFjy NZtWyrVtauE+VwPIPfDZiYE7l5HlarFE5KiEdCYz2DNMkZHTdiymAph6fPUNz+VK085i lcLPtYfES8kvMfZ6fNyFUs8jz52kRwoHMovxgtqbETuwjtsZ13kSwAXqdD7uA+cgJEEY DOgzJm1EFaF4+oRsqoR7kR1qGglKmmtgg7Dj6Pau8Asige5PfJ/iqrKVngvGXGAVTxvu bkvRaMWipM8UlcZJ3v7IdJlLg5NrpyagINZT1/qyd9RQc4l2N5Ux8RkDGW8vkUS97jIA p25w== X-Gm-Message-State: AOAM5317xjMSv/b0ebmy20HMkB7yzbwj03AS5rP0hS4fP7VU5kjj73Ck iDBGpF7GG1EcIVU1m/8TaA4= X-Google-Smtp-Source: ABdhPJyD4ZSJGC+eol0K/oOnZUQl/nfn8IWP6VMs7y01HjupWZPGdXTj1ewk85YjH9rtTNT21StJYg== X-Received: by 2002:a17:907:6d04:b0:6ee:402b:6cb with SMTP id sa4-20020a1709076d0400b006ee402b06cbmr9911897ejc.608.1650311884928; Mon, 18 Apr 2022 12:58:04 -0700 (PDT) Received: from [192.168.0.74] ([178.233.178.185]) by smtp.gmail.com with ESMTPSA id ga5-20020a1709070c0500b006de43e9605asm4797600ejc.181.2022.04.18.12.58.02 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 18 Apr 2022 12:58:04 -0700 (PDT) Message-ID: <5993486a-5e8e-b38d-2293-a9379e0365af@gmail.com> Date: Mon, 18 Apr 2022 22:55:56 +0300 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux aarch64; rv:91.0) Gecko/20100101 Thunderbird/91.8.0 From: Alper Nebi Yasak Subject: Re: [RESEND, RFC 1/8] tools: config: yaml: Add board config class to generate config binaries To: Neha Malcom Francis Cc: sjg@chromium.org, marek.behun@nic.cz, xypron.glpk@gmx.de, vigneshr@ti.com, a-govindraju@ti.com, kristo@kernel.org, s-anna@ti.com, kishon@ti.com, joel.peshkin@broadcom.com, patrick.delaunay@foss.st.com, mr.nuke.me@gmail.com, nm@ti.com, u-boot@lists.denx.de References: <20220406122919.6104-1-n-francis@ti.com> <20220406122919.6104-2-n-francis@ti.com> Content-Language: en-US In-Reply-To: <20220406122919.6104-2-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.5 at phobos.denx.de X-Virus-Status: Clean On 06/04/2022 15:29, 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. The subject lines (of other patches as well) sound too generic when most of them are TI specific, I'd expect at least a 'ti:' tag except where you already include more specific terms like a board name. (This one could be "tools: ti: Add ..." for example). > > 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 | 116 +++++++++++++++++++++++++++++++++++++++ > 2 files changed, 117 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..7635596906 > --- /dev/null > +++ b/tools/tibcfg_gen.py > @@ -0,0 +1,116 @@ > +# 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 > +# > + > +from jsonschema import validate > + > +import yaml > +import os > +import sys Standard library imports should appear before third-party libraries, with an empty line between them. > + > + > +class TIBoardConfig: > + file_yaml = {} > + schema_yaml = {} > + data_rules = {} These belong in __init__ as they are per-instance attributes. > + > + def __init__(self): > + pass > + > + def Load(self, file, schema, data_rules=""): You can rename this to be the __init__ function. > + 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 > + > + def CheckValidity(self): > + try: > + validate(self.file_yaml, self.schema_yaml) > + return True > + except Exception as e: > + print(e) > + return False You can also do this validation immediately after loading the yaml files in the __init__(), and then safely assume any created object is valid. > + > + def __ConvertToByteChunk(self, val, data_type): Methods should be in snake_case. Also consider using a single underscore as the prefix, double underscore does some special name mangling. > + br = [] > + size = 0 > + if(data_type == "#/definitions/u8"): > + size = 1 > + elif(data_type == "#/definitions/u16"): > + size = 2 > + elif(data_type == "#/definitions/u32"): > + size = 4 > + else: > + return -1 I think this case should raise an error of some kind. > + if(type(val) == int): > + while(val != 0): In general, don't use parentheses with if, while etc. > + br = br + [(val & 0xFF)] > + val = val >> 8 > + while(len(br) < size): > + br = br + [0] > + return br This all looks like val.to_bytes(size, 'little'), but as a list instead of bytes. If you want to get fancy, have a look at the struct module. (For example, struct.pack(' + > + def __CompileYaml(self, schema_yaml, file_yaml): > + br = [] Consider using a bytearray instead of a list-of-ints here. > + for key in file_yaml.keys(): I think things would be more readable if you extracted node = file_yaml[key] node_schema = schema_yaml['properties'][key] node_type = node_schema.get('type') as variables here and used those in the following code. > + if not 'type' in schema_yaml['properties'][key]: > + br = br + \ br += ... would be nicer for all of these. > + self.__ConvertToByteChunk( > + file_yaml[key], schema_yaml['properties'][key]["$ref"]) > + elif schema_yaml['properties'][key]['type'] == 'object': > + br = br + \ > + self.__CompileYaml( > + schema_yaml['properties'][key], file_yaml[key]) > + elif schema_yaml['properties'][key]['type'] == 'array': > + for item in file_yaml[key]: > + if not isinstance(item, dict): > + br = br + \ > + self.__ConvertToByteChunk( > + item, schema_yaml['properties'][key]['items']["$ref"]) > + else: > + br = br + \ > + self.__CompileYaml( > + schema_yaml['properties'][key]['items'], item) > + return br > + > + def GenerateBinaries(self, out_path=""): > + if not os.path.isdir(out_path): > + os.mkdir(out_path) > + if(self.CheckValidity()): > + for key in self.file_yaml.keys(): > + br = [] You don't need this assignment, it's overwritten in the next one anyway. > + br = self.__CompileYaml( > + self.schema_yaml['properties'][key], self.file_yaml[key]) > + with open(out_path + "/" + key + ".bin", 'wb') as cfg: Construct file paths with os.path.join() here and below. > + cfg.write(bytearray(br)) > + else: > + raise ValueError("Config YAML Validation failed!") > + > + def DeleteBinaries(self, out_path=""): > + if os.path.isdir(out_path): > + for key in self.file_yaml.keys(): > + if os.path.isfile(out_path + "/" + key + ".bin"): > + os.remove(out_path + "/" + key + ".bin") > + > + > +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 > + Pass the arguments along with the filename in the Makefile. > + """ > + tibcfg = TIBoardConfig() > + config_yaml = sys.argv[1] > + schema_yaml = sys.argv[2] > + try: > + tibcfg.Load(config_yaml, schema_yaml) > + except: > + raise ValueError("Could not find config files!") > + tibcfg.GenerateBinaries(os.environ['O']) I think it'd be better to pass the directory as an -o / --output-dir argument instead of reading it from environment. You can use argparse to parse the command line arguments. > + > + > +cfgBinaryGen() This should be guarded by if __name__ == '__main__'.