diff --git a/.flake8 b/.flake8 index c2a1ce2c574fa0..03d013f52334a3 100644 --- a/.flake8 +++ b/.flake8 @@ -27,14 +27,12 @@ exclude = third_party scripts/build/builders/gn.py scripts/build/builders/imx.py scripts/build/builders/infineon.py - scripts/build/builders/nrf.py scripts/codegen.py scripts/codepregen.py scripts/error_table.py scripts/examples/gn_to_cmakelists.py scripts/flashing/bouffalolab_firmware_utils.py scripts/flashing/cyw30739_firmware_utils.py - scripts/flashing/nrfconnect_firmware_utils.py scripts/gen_chip_version.py scripts/helpers/bloat_check.py scripts/pregenerate/using_codegen.py @@ -51,15 +49,11 @@ exclude = third_party scripts/py_matter_yamltests/test_yaml_loader.py scripts/py_matter_yamltests/test_yaml_parser.py scripts/run-clang-tidy-on-compile-commands.py - scripts/setup/nrfconnect/update_ncs.py scripts/tests/chiptest/yamltest_with_chip_repl_tester.py scripts/tools/check_zcl_file_sync.py scripts/tools/convert_ini.py scripts/tools/memory/memdf/__init__.py scripts/tools/memory/report_summary.py - scripts/tools/nrfconnect/generate_nrfconnect_chip_factory_data.py - scripts/tools/nrfconnect/nrfconnect_generate_partition.py - scripts/tools/nrfconnect/tests/test_generate_factory_data.py scripts/tools/silabs/FactoryDataProvider.py scripts/tools/telink/mfg_tool.py scripts/tools/zap/generate.py diff --git a/scripts/build/builders/nrf.py b/scripts/build/builders/nrf.py index a1e7c457ea6ee2..6357a1e755fb3b 100644 --- a/scripts/build/builders/nrf.py +++ b/scripts/build/builders/nrf.py @@ -184,7 +184,8 @@ def generate(self): if self.enable_rpcs: flags.append("-DOVERLAY_CONFIG=rpc.overlay") - if self.board == NrfBoard.NRF52840DONGLE and self.app != NrfApp.ALL_CLUSTERS and self.app != NrfApp.ALL_CLUSTERS_MINIMAL: + if (self.board == NrfBoard.NRF52840DONGLE and + self.app != NrfApp.ALL_CLUSTERS and self.app != NrfApp.ALL_CLUSTERS_MINIMAL): flags.append("-DCONF_FILE=prj_no_dfu.conf") if self.options.pregen_dir: @@ -242,5 +243,5 @@ def flashbundle(self): with open(os.path.join(self.output_dir, self.app.FlashBundleName()), 'r') as fp: return { - l.strip(): os.path.join(self.output_dir, l.strip()) for l in fp.readlines() if l.strip() + line.strip(): os.path.join(self.output_dir, line.strip()) for line in fp.readlines() if line.strip() } diff --git a/scripts/flashing/nrfconnect_firmware_utils.py b/scripts/flashing/nrfconnect_firmware_utils.py index 5a04a660736d87..e8a6ab0c9694ca 100755 --- a/scripts/flashing/nrfconnect_firmware_utils.py +++ b/scripts/flashing/nrfconnect_firmware_utils.py @@ -170,7 +170,7 @@ def flash(self): def verify_platform_args(platform_args): required_args = ['application'] for r in required_args: - if not r in platform_args: + if r not in platform_args: raise ValueError("Required argument %s missing" % r) diff --git a/scripts/setup/nrfconnect/update_ncs.py b/scripts/setup/nrfconnect/update_ncs.py index b774b8690e3946..8d8b9a5fc1d4ff 100755 --- a/scripts/setup/nrfconnect/update_ncs.py +++ b/scripts/setup/nrfconnect/update_ncs.py @@ -52,7 +52,7 @@ def get_ncs_recommended_revision(): try: with open(os.path.join(chip_root, 'config/nrfconnect/.nrfconnect-recommended-revision'), 'r') as f: return f.readline().strip() - except: + except OSError: raise RuntimeError( "Encountered problem when trying to read .nrfconnect-recommended-revision file.") @@ -80,8 +80,12 @@ def print_check_revision_warning_message(current_revision, recommended_revision) # To keep right frame shape the space characters are added to messages shorter than the longest one. fmt = "# {:<%s}#" % (longest_message_len) - print_messages([(longest_message_len+3)*'#', fmt.format(current_revision_message), fmt.format(recommended_revision_message), fmt.format(''), - fmt.format(allowed_message), fmt.format(update_message), fmt.format(call_command_message), (longest_message_len+3)*'#'], sys.stdout.isatty()) + print_messages([ + (longest_message_len+3)*'#', fmt.format(current_revision_message), + fmt.format(recommended_revision_message), fmt.format(''), + fmt.format(allowed_message), fmt.format(update_message), + fmt.format(call_command_message), (longest_message_len+3)*'#' + ], sys.stdout.isatty()) def main(): @@ -95,13 +99,17 @@ def main(): parser = argparse.ArgumentParser( description='Script helping to update nRF Connect SDK to currently recommended revision.') parser.add_argument( - "-c", "--check", help="Check if your current nRF Connect SDK revision is the same as recommended one.", action="store_true") + "-c", "--check", + help="Check if your current nRF Connect SDK revision is the same as recommended one.", action="store_true") parser.add_argument( - "-u", "--update", help="Update your nRF Connect SDK to currently recommended revision.", action="store_true") + "-u", "--update", + help="Update your nRF Connect SDK to currently recommended revision.", action="store_true") parser.add_argument( - "-s", "--shallow", help="Fetch only specific commits (without the history) when updating nRF Connect SDK.", action="store_true") + "-s", "--shallow", + help="Fetch only specific commits (without the history) when updating nRF Connect SDK.", action="store_true") parser.add_argument( - "-q", "--quiet", help="Don't print any message if the check succeeds.", action="store_true") + "-q", "--quiet", + help="Don't print any message if the check succeeds.", action="store_true") args = parser.parse_args() ncs_base = os.path.join(zephyr_base, '../nrf') diff --git a/scripts/tools/nrfconnect/generate_nrfconnect_chip_factory_data.py b/scripts/tools/nrfconnect/generate_nrfconnect_chip_factory_data.py index 4eafc7b68f7cc8..d0f063fbde11cf 100644 --- a/scripts/tools/nrfconnect/generate_nrfconnect_chip_factory_data.py +++ b/scripts/tools/nrfconnect/generate_nrfconnect_chip_factory_data.py @@ -93,19 +93,23 @@ def gen_test_certs(chip_cert_exe: str, product_id (int): an identification number specific to Product device_name (str): human-readable device name generate_cd (bool, optional): Generate Certificate Declaration and store it in thee output directory. Defaults to False. - paa_cert_path (str, optional): provide PAA certification path. Defaults to None - a path will be set to /credentials/test/attestation directory. - paa_key_path (str, optional): provide PAA key path. Defaults to None - a path will be set to /credentials/test/attestation directory. + paa_cert_path (str, optional): provide PAA certification path. Defaults to None - a path will be set to + /credentials/test/attestation directory. + paa_key_path (str, optional): provide PAA key path. Defaults to None - a path will be set to + /credentials/test/attestation directory. Returns: - dictionary: ["PAI_CERT": (str), + dictionary: ["PAI_CERT": (str), "DAC_CERT": (str), "DAC_KEY": (str)] """ CD_PATH = MATTER_ROOT + "/credentials/test/certification-declaration/Chip-Test-CD-Signing-Cert.pem" CD_KEY_PATH = MATTER_ROOT + "/credentials/test/certification-declaration/Chip-Test-CD-Signing-Key.pem" - PAA_PATH = paa_cert_path if paa_cert_path != None else MATTER_ROOT + "/credentials/test/attestation/Chip-Test-PAA-NoVID-Cert.pem" - PAA_KEY_PATH = paa_key_path if paa_key_path != None else MATTER_ROOT + "/credentials/test/attestation/Chip-Test-PAA-NoVID-Key.pem" + PAA_PATH = paa_cert_path if paa_cert_path is not None else (MATTER_ROOT + + "/credentials/test/attestation/Chip-Test-PAA-NoVID-Cert.pem") + PAA_KEY_PATH = paa_key_path if paa_key_path is not None else (MATTER_ROOT + + "/credentials/test/attestation/Chip-Test-PAA-NoVID-Key.pem") attestation_certs = namedtuple("attestation_certs", ["dac_cert", "dac_key", "pai_cert"]) @@ -164,7 +168,7 @@ def gen_test_certs(chip_cert_exe: str, # convert to .der files for cert_k, cert_v in new_certificates.items(): - action_type = "convert-cert" if cert_k.find("CERT") != -1 else "convert-key" + action_type = "convert-cert" if cert_k.find("CERT") is not -1 else "convert-key" log.info(cert_v + ".der") cmd = [chip_cert_exe, action_type, cert_v + ".pem", @@ -338,7 +342,7 @@ def generate_json(self): try: if is_json_valid: json_file.write(json_object) - except IOError as e: + except IOError: log.error("Cannot save output file into directory: {}".format(self._args.output)) def _add_entry(self, name: str, value: any): @@ -372,7 +376,7 @@ def _validate_output_json(self, output_json: str): schema = json.loads(schema_file.read()) validator = jsonschema.Draft202012Validator(schema=schema) validator.validate(instance=json.loads(output_json)) - except IOError as e: + except IOError: log.error("Provided JSON schema file is wrong: {}".format(self._args.schema)) return False else: @@ -412,10 +416,10 @@ def base64_str(s): return base64.b64decode(s) # Json known-keys values # mandatory keys mandatory_arguments.add_argument("--sn", type=str, required=True, - help="[ascii string] Serial number of a device which can be used to identify \ - the serial number field in the Matter certificate structure. \ - Maximum length of serial number is 20 bytes. \ - Strings longer than 20 bytes will be declined in script") + help=("[ascii string] Serial number of a device which can be used to identify " + "the serial number field in the Matter certificate structure. " + "Maximum length of serial number is 20 bytes. " + "Strings longer than 20 bytes will be declined in script")) mandatory_arguments.add_argument("--vendor_id", type=allow_any_int, help="[int | hex int] Provide Vendor Identification Number") mandatory_arguments.add_argument("--product_id", type=allow_any_int, @@ -425,9 +429,9 @@ def base64_str(s): return base64.b64decode(s) mandatory_arguments.add_argument("--product_name", type=str, help="[string] provide human-readable product name") mandatory_arguments.add_argument("--date", type=str, required=True, - help="[ascii string] Provide manufacturing date \ - A manufacturing date specifies the date that the Node was manufactured. \ - Used format for providing a manufacturing date is ISO 8601 e.g. YYYY-MM-DD.") + help=("[ascii string] Provide manufacturing date " + "A manufacturing date specifies the date that the Node was manufactured. " + "Used format for providing a manufacturing date is ISO 8601 e.g. YYYY-MM-DD.")) mandatory_arguments.add_argument("--hw_ver", type=allow_any_int, required=True, help="[int | hex int] Provide hardware version in int format.") mandatory_arguments.add_argument("--hw_ver_str", type=str, required=True, @@ -449,37 +453,52 @@ def base64_str(s): return base64.b64decode(s) optional_arguments.add_argument("--part_number", type=str, help="[string] provide human-readable product number") optional_arguments.add_argument("--chip_cert_path", type=str, - help="Generate DAC and PAI certificates instead giving a path to .der files. This option requires a path to chip-cert executable." - "By default you can find chip-cert in connectedhomeip/src/tools/chip-cert directory and build it there.") + help=("Generate DAC and PAI certificates instead giving a path to .der files. " + "This option requires a path to chip-cert executable." + "By default you can find chip-cert in connectedhomeip/src/tools/chip-cert directory " + "and build it there.")) optional_arguments.add_argument("--dac_cert", type=str, help="[.der] Provide the path to .der file containing DAC certificate.") optional_arguments.add_argument("--dac_key", type=str, help="[.der] Provide the path to .der file containing DAC keys.") optional_arguments.add_argument("--generate_rd_uid", action="store_true", - help="Generate a new rotating device unique ID, print it out to console output and store it in factory data.") + help=("Generate a new rotating device unique ID, print it out to console output " + "and store it in factory data.")) optional_arguments.add_argument("--dac_key_password", type=str, - help="Provide a password to decode dac key. If dac key is not encrypted do not provide this argument.") + help=("Provide a password to decode dac key. If dac key is not encrypted do not " + "provide this argument.")) optional_arguments.add_argument("--pai_cert", type=str, help="[.der] Provide the path to .der file containing PAI certificate.") optional_arguments.add_argument("--rd_uid", type=str, - help="[hex string] [128-bit hex-encoded] Provide the rotating device unique ID. If this argument is not provided a new rotating device id unique id will be generated.") + help=("[hex string] [128-bit hex-encoded] Provide the rotating device unique ID. " + "If this argument is not provided a new rotating device id unique id will be generated.")) optional_arguments.add_argument("--passcode", type=allow_any_int, - help="[int | hex] Default PASE session passcode. (This is mandatory to generate Spake2+ verifier).") + help=("[int | hex] Default PASE session passcode. " + "(This is mandatory to generate Spake2+ verifier).")) optional_arguments.add_argument("--spake2_verifier", type=base64_str, help="[base64 string] Provide Spake2+ verifier without generating it.") optional_arguments.add_argument("--enable_key", type=str, - help="[hex string] [128-bit hex-encoded] The Enable Key is a 128-bit value that triggers manufacturer-specific action while invoking the TestEventTrigger Command." - "This value is used during Certification Tests, and should not be present on production devices.") + help=("[hex string] [128-bit hex-encoded] The Enable Key is a 128-bit value that " + "triggers manufacturer-specific action while invoking the TestEventTrigger Command." + "This value is used during Certification Tests, and should " + "not be present on production devices.")) optional_arguments.add_argument("--user", type=str, - help="[string] Provide additional user-specific keys in JSON format: {'name_1': 'value_1', 'name_2': 'value_2', ... 'name_n', 'value_n'}.") + help=("[string] Provide additional user-specific keys in JSON format: " + "{'name_1': 'value_1', 'name_2': 'value_2', ... 'name_n', 'value_n'}.")) optional_arguments.add_argument("--gen_cd", action="store_true", default=False, - help="Generate a new Certificate Declaration in .der format according to used Vendor ID and Product ID. This certificate will not be included to the factory data.") + help=("Generate a new Certificate Declaration in .der format according to used Vendor ID " + "and Product ID. This certificate will not be included to the factory data.")) optional_arguments.add_argument("--cd_type", type=int, default=1, - help="[int] Type of generated Certification Declaration: 0 - development, 1 - provisional, 2 - official") + help=("[int] Type of generated Certification Declaration: " + "0 - development, 1 - provisional, 2 - official")) optional_arguments.add_argument("--paa_cert", type=str, - help="Provide a path to the Product Attestation Authority (PAA) certificate to generate the PAI certificate. Without providing it, a testing PAA stored in the Matter repository will be used.") + help=("Provide a path to the Product Attestation Authority (PAA) certificate to generate " + "the PAI certificate. Without providing it, a testing PAA stored in the Matter " + "repository will be used.")) optional_arguments.add_argument("--paa_key", type=str, - help="Provide a path to the Product Attestation Authority (PAA) key to generate the PAI certificate. Without providing it, a testing PAA key stored in the Matter repository will be used.") + help=("Provide a path to the Product Attestation Authority (PAA) key to generate " + "the PAI certificate. Without providing it, a testing PAA key stored in the Matter " + "repository will be used.")) args = parser.parse_args() if args.verbose: @@ -489,13 +508,15 @@ def base64_str(s): return base64.b64decode(s) # check if json file already exist if (exists(args.output) and not args.overwrite): - log.error("Output file: {} already exist, to create a new one add argument '--overwrite'. By default overwriting is disabled".format(args.output)) + 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.") + 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) diff --git a/scripts/tools/nrfconnect/nrfconnect_generate_partition.py b/scripts/tools/nrfconnect/nrfconnect_generate_partition.py index d3da44ab4dc1c5..621ed03b06507d 100644 --- a/scripts/tools/nrfconnect/nrfconnect_generate_partition.py +++ b/scripts/tools/nrfconnect/nrfconnect_generate_partition.py @@ -90,7 +90,7 @@ def _convert_to_dict(data): """ Converts a list containing tuples ("key_name", "key_value") to a dictionary - If "key_value" of data entry is a string-type variable and contains a HEX_PREFIX algorithm decodes it + If "key_value" of data entry is a string-type variable and contains a HEX_PREFIX algorithm decodes it to hex format to be sure that a cbor file will contain proper bytes. If "key_value" of data entry is a dictionary, algorithm appends it to the created dictionary. @@ -143,7 +143,8 @@ def allow_any_int(i): return int(i, 0) parser.add_argument("-i", "--input", type=str, required=True, help="Path to input .json file") parser.add_argument("-o", "--output", type=str, required=True, - help="Prefix for output file paths, e.g. setting dir/output causes creation of the following files: dir/output.hex, and dir/output.bin") + help=("Prefix for output file paths, e.g. setting dir/output causes creation of the following files: " + "dir/output.hex, and dir/output.bin")) parser.add_argument("--offset", type=allow_any_int, required=True, help="Partition offset - an address in device's NVM memory, where factory data will be stored") parser.add_argument("--size", type=allow_any_int, required=True, @@ -151,7 +152,8 @@ def allow_any_int(i): return int(i, 0) parser.add_argument("-v", "--verbose", action="store_true", help="Run this script with DEBUG logging level") parser.add_argument("-r", "--raw", action="store_true", - help="Do not print flashing help and other logs, only generate a .hex file. It can be useful when the script is used by other script.") + help=("Do not print flashing help and other logs, only generate a .hex file. " + "It can be useful when the script is used by other script.")) args = parser.parse_args() if args.verbose: diff --git a/scripts/tools/nrfconnect/tests/test_generate_factory_data.py b/scripts/tools/nrfconnect/tests/test_generate_factory_data.py index d3fd86f69bf3e5..3ffa4c75b6bf0b 100755 --- a/scripts/tools/nrfconnect/tests/test_generate_factory_data.py +++ b/scripts/tools/nrfconnect/tests/test_generate_factory_data.py @@ -167,7 +167,8 @@ def test_generate_factory_data_all_specified(self): '--spake2_it', '2000', '--spake2_salt', 'U1BBS0UyUCBLZXkgU2FsdA==', '--passcode', '13243546', - '--spake2_verifier', 'WN0SgEXLfUN19BbJqp6qn4pS69EtdNLReIMZwv/CIM0ECMP7ytiAJ7txIYJ0Ovlha/rQ3E+88mj3qaqqnviMaZzG+OyXEdSocDIT9ZhmkTCgWwERaHz4Vdh3G37RT6kqbw==', + '--spake2_verifier', ('WN0SgEXLfUN19BbJqp6qn4pS69EtdNLReIMZwv/CIM0ECMP7ytiAJ7txIYJ0Ovlha/' + 'rQ3E+88mj3qaqqnviMaZzG+OyXEdSocDIT9ZhmkTCgWwERaHz4Vdh3G37RT6kqbw=='), '--discriminator', '0xFED', '--rd_uid', '91a9c12a7c80700a31ddcfa7fce63e44', '--enable_key', '00112233445566778899aabbccddeeff', @@ -195,7 +196,8 @@ def test_generate_factory_data_all_specified(self): self.assertEqual(factory_data.get('spake2_it'), 2000) self.assertEqual(factory_data.get('spake2_salt'), base64_to_json('U1BBS0UyUCBLZXkgU2FsdA==')) self.assertEqual(factory_data.get('spake2_verifier'), base64_to_json( - 'WN0SgEXLfUN19BbJqp6qn4pS69EtdNLReIMZwv/CIM0ECMP7ytiAJ7txIYJ0Ovlha/rQ3E+88mj3qaqqnviMaZzG+OyXEdSocDIT9ZhmkTCgWwERaHz4Vdh3G37RT6kqbw==')) + 'WN0SgEXLfUN19BbJqp6qn4pS69EtdNLReIMZwv/CIM0ECMP7ytiAJ7txIYJ0Ovlha/' + 'rQ3E+88mj3qaqqnviMaZzG+OyXEdSocDIT9ZhmkTCgWwERaHz4Vdh3G37RT6kqbw==')) self.assertEqual(factory_data.get('discriminator'), 0xFED) self.assertEqual(factory_data.get('passcode'), 13243546) self.assertEqual(factory_data.get('rd_uid'), 'hex:91a9c12a7c80700a31ddcfa7fce63e44') @@ -244,7 +246,8 @@ def test_generate_spake2p_verifier_default(self): base64_to_json('U1BBS0UyUCBLZXkgU2FsdA==')) self.assertEqual(factory_data.get('spake2_it'), 1000) self.assertEqual(factory_data.get('spake2_verifier'), base64_to_json( - 'uWFwqugDNGiEck/po7KHwwMwwqZgN10XuyBajPGuyzUEV/iree4lOrao5GuwnlQ65CJzbeUB49s31EH+NEkg0JVI5MGCQGMMT/SRPFNRODm3wH/MBiehuFc6FJ/NH6Rmzw==')) + 'uWFwqugDNGiEck/po7KHwwMwwqZgN10XuyBajPGuyzUEV/' + 'iree4lOrao5GuwnlQ65CJzbeUB49s31EH+NEkg0JVI5MGCQGMMT/SRPFNRODm3wH/MBiehuFc6FJ/NH6Rmzw==')) self.assertEqual(factory_data.get('user'), {'name': 'product_name', 'version': 123, 'revision': '0x123'}) subprocess.check_call(['python3', os.path.join(TOOLS_DIR, 'nrfconnect_generate_partition.py'),