Apply the reset device operation in Python

nRF Util

tags
nRF Util

The following Python code snippet uses the JSON output to try to reset all SEGGER J-Link devices and print the result of the operation for each device:

import json
import subprocess

# Run reset operation on all connected J-Link devices and collect the output
output = subprocess.run(
    ["nrfutil", "--json", "device", "reset", "--traits", "jlink"],
    capture_output=True,
)
stdout_as_lines = output.stdout.decode("utf-8").splitlines()

for line in stdout_as_lines:
    parsed_line = json.loads(line)

    # Find the lines indicating that a task ended
    if parsed_line["type"] == "task_end":
        # Print the results of the operation
        print(
            "{}: {}".format(
                parsed_line["data"]["task"]["description"],
                parsed_line["data"]["result"],
            )
        )