From 3560358b4731f09a785d8caa61c619aa6cb3e432 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arkadiusz=20Ba=C5=82ys?= Date: Thu, 8 Dec 2022 14:05:15 +0100 Subject: [PATCH] [nrfconnect] Fix for jsonschema documentation and factory data script (#23966) * [nrfconnect] Fix for jsonschema documentation and factory data script In the factory data script, there was no checking if a user has jsonschema module installed. That caused an error even if the user did not want to use that way of JSON validation. Added checking a jsonschema module in python script for generating factory data and added proper information in the documentation about how to obtain jsonschema python module. * Restyled by prettier-markdown Co-authored-by: Restyled.io --- .../nrfconnect_factory_data_configuration.md | 24 ++++++++++++++++--- .../generate_nrfconnect_chip_factory_data.py | 14 ++++++++++- 2 files changed, 34 insertions(+), 4 deletions(-) diff --git a/docs/guides/nrfconnect_factory_data_configuration.md b/docs/guides/nrfconnect_factory_data_configuration.md index 071fc69ed9a02e..c05b5a61ebaa16 100644 --- a/docs/guides/nrfconnect_factory_data_configuration.md +++ b/docs/guides/nrfconnect_factory_data_configuration.md @@ -386,9 +386,27 @@ window indicates the validation status. #### Option 3: Using the nRF Connect Python script You can have the JSON file checked automatically by the Python script during the -file generation. For this to happen, provide the path to the JSON schema file as -an additional argument, which should replace the __ variable in -the following command: +file generation. For this to happen, you must install the `jsonschema` Python +module in your Python environment and provide the path to the JSON schema file +as an additional argument. To do this, complete the following steps: + +1. Install the `jsonschema` Python module by invoking one of the following + commands from the Matter root directory: + + - Install only the `jsonschema` module: + + ``` + $ python -m pip install jsonschema + ``` + + - Install the `jsonschema` module together with all dependencies for Matter: + + ``` + $ python -m pip install -r ./scripts/requirements.nrfconnect.txt + ``` + +2. Run the following command (remember to replace the __ + variable): ``` $ python generate_nrfconnect_chip_factory_data.py --schema diff --git a/scripts/tools/nrfconnect/generate_nrfconnect_chip_factory_data.py b/scripts/tools/nrfconnect/generate_nrfconnect_chip_factory_data.py index e69bbc0be91fd1..ff4f64e3d82b25 100644 --- a/scripts/tools/nrfconnect/generate_nrfconnect_chip_factory_data.py +++ b/scripts/tools/nrfconnect/generate_nrfconnect_chip_factory_data.py @@ -19,7 +19,6 @@ import os import sys import json -import jsonschema import secrets import argparse import subprocess @@ -29,6 +28,13 @@ from cryptography.hazmat.backends import default_backend from cryptography.hazmat.primitives.serialization import load_der_private_key +try: + import jsonschema +except ImportError: + no_jsonschema_module = True +else: + no_jsonschema_module = False + # A user can not change the factory data version and must be coherent with # the factory data version set in the nRF Connect platform Kconfig file (CHIP_FACTORY_DATA_VERSION). FACTORY_DATA_VERSION = 1 @@ -485,6 +491,12 @@ def base64_str(s): return base64.b64decode(s) log.error("Output file: {} already exist, to create a new one add argument '--overwrite'. By default overwriting is disabled".format(args.output)) return + if args.schema and no_jsonschema_module: + log.error("Requested verification of the JSON file using jsonschema, but the module is not installed. \n \ + Install only the module by invoking: pip3 install jsonschema \n \ + Alternatively, install it with all dependencies for Matter by invoking: pip3 install -r ./scripts/requirements.nrfconnect.txt from the Matter root directory.") + return + generator = FactoryDataGenerator(args) generator.generate_json()