Skip to content

Commit

Permalink
[nrfconnect] Fix for jsonschema documentation and factory data script (
Browse files Browse the repository at this point in the history
…#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 <[email protected]>
  • Loading branch information
2 people authored and pull[bot] committed Jan 25, 2023
1 parent d501de5 commit 3560358
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
24 changes: 21 additions & 3 deletions docs/guides/nrfconnect_factory_data_configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 _<path_to_schema>_ 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 _<path_to_schema>_
variable):
```
$ python generate_nrfconnect_chip_factory_data.py --schema <path_to_schema>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import os
import sys
import json
import jsonschema
import secrets
import argparse
import subprocess
Expand All @@ -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
Expand Down Expand Up @@ -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()

Expand Down

0 comments on commit 3560358

Please sign in to comment.