Skip to content

Commit

Permalink
[nrfconnect] Add to flake8 in workflow and fix python files (#25277)
Browse files Browse the repository at this point in the history
  • Loading branch information
DamMicSzm authored Mar 13, 2023
1 parent d1604b3 commit e5c1a3b
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 54 deletions.
6 changes: 0 additions & 6 deletions .flake8
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
5 changes: 3 additions & 2 deletions scripts/build/builders/nrf.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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()
}
2 changes: 1 addition & 1 deletion scripts/flashing/nrfconnect_firmware_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand Down
22 changes: 15 additions & 7 deletions scripts/setup/nrfconnect/update_ncs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.")

Expand Down Expand Up @@ -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():
Expand All @@ -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')
Expand Down
85 changes: 53 additions & 32 deletions scripts/tools/nrfconnect/generate_nrfconnect_chip_factory_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)<path to PAI cert .der file>,
dictionary: ["PAI_CERT": (str)<path to PAI cert .der file>,
"DAC_CERT": (str)<path to DAC cert .der file>,
"DAC_KEY": (str)<path to DAC key .der file>]
"""

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"])

Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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:
Expand All @@ -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)
Expand Down
Loading

0 comments on commit e5c1a3b

Please sign in to comment.