diff --git a/examples/chef/chef.py b/examples/chef/chef.py index f92e0d038f92fd..b85674ce33ee7e 100755 --- a/examples/chef/chef.py +++ b/examples/chef/chef.py @@ -40,8 +40,7 @@ _CHEF_ZZZ_ROOT = os.path.join(_CHEF_SCRIPT_PATH, "zzz_generated") _CI_DEVICE_MANIFEST_NAME = "INPUTMD5.txt" _CI_ZAP_MANIFEST_NAME = "ZAPSHA.txt" -_CICD_CONFIG_FILE_NAME = os.path.join(_CHEF_SCRIPT_PATH, "cicd_meta.json") -_CI_ALLOW_LIST = ["lighting-app"] +_CICD_CONFIG_FILE_NAME = os.path.join(_CHEF_SCRIPT_PATH, "cicd_config.json") _CD_STAGING_DIR = os.path.join(_CHEF_SCRIPT_PATH, "staging") gen_dir = "" # Filled in after sample app type is read from args. @@ -117,11 +116,12 @@ def check_zap() -> str: def generate_device_manifest( - write_manifest_file: bool = False) -> Dict[str, Any]: - """Produces dictionary containing md5 of device dir zap files. + write_manifest_files: bool = False) -> Dict[str, Any]: + """Produces dictionary containing md5 of device dir zap files + and zap commit. Args: - write_manifest_file: Serialize manifest in tree. + write_manifest_files: Serialize digests in tree. Returns: Dict containing MD5 of device dir zap files. """ @@ -136,7 +136,7 @@ def generate_device_manifest( device_file_md5 = hashlib.md5(device_file_data).hexdigest() devices_manifest[device_name] = device_file_md5 flush_print(f"Current digest for {device_name} : {device_file_md5}") - if write_manifest_file: + if write_manifest_files: device_zzz_dir = os.path.join(_CHEF_ZZZ_ROOT, device_name) device_zzz_md5_file = os.path.join(device_zzz_dir, _CI_DEVICE_MANIFEST_NAME) with open(device_zzz_md5_file, "w+") as md5_file: @@ -150,12 +150,6 @@ def generate_device_manifest( def load_cicd_config() -> Dict[str, Any]: with open(_CICD_CONFIG_FILE_NAME) as config_file: config = json.loads(config_file.read()) - for platform_name, platform_config in config.items(): - has_build_dir = "build_dir" in platform_config - has_plat_label = "platform_label" in platform_config - if not has_build_dir or not has_plat_label: - flush_print(f"{platform_name} CICD config missing build_dir or platform_label") - exit(1) return config @@ -176,26 +170,27 @@ def flush_print( def bundle(platform: str, device_name: str) -> None: """Filters files from the build output folder for CD. - calls bundle_platform(device_name). - Clears the staging dir + Clears the staging dir. + Calls bundle_{platform}(device_name). + exit(1) for missing bundle_{platform}. Args: - platform: the platform to bundle - device_name: the example to bundle + platform: The platform to bundle. + device_name: The example to bundle. """ - flush_print(f"Bundling {platform}") + flush_print(f"Bundling {platform}", with_border=True) shutil.rmtree(_CD_STAGING_DIR, ignore_errors=True) os.mkdir(_CD_STAGING_DIR) - bundle_fn_name = f"bundle_{platform}" - if bundle_fn_name in globals(): - globals()[bundle_fn_name](device_name) + bundler_name = f"bundle_{platform}" + if bundler_name in globals(): + globals()[bundler_name](device_name) else: flush_print(f"No bundle function for {platform}!") exit(1) # -# Bundle functions +# Per-platform bundle functions # @@ -213,11 +208,9 @@ def bundle_linux(device_name: str) -> None: def bundle_nrfconnect(device_name: str) -> None: - # src files zephyr_exts = ["elf", "map", "hex"] script_files = ["firmware_utils.py", "nrfconnect_firmware_utils.py"] - # src dirs nrf_root = os.path.join(_CHEF_SCRIPT_PATH, "nrfconnect", "build", @@ -225,32 +218,24 @@ def bundle_nrfconnect(device_name: str) -> None: scripts_root = os.path.join(_REPO_BASE_PATH, "scripts", "flashing") - # output prep + gen_script = os.path.join(scripts_root, + "gen_flashing_script.py") sub_dir = os.path.join(_CD_STAGING_DIR, device_name) os.mkdir(sub_dir) - # copy src files for zephyr_ext in zephyr_exts: input_base = f"zephyr.{zephyr_ext}" output_base = f"{device_name}.{zephyr_ext}" + src_item = os.path.join(nrf_root, input_base) if zephyr_ext == "hex": - dest_item = os.path.join(sub_dir, - output_base) + dest_item = os.path.join(sub_dir, output_base) else: - dest_item = os.path.join(_CD_STAGING_DIR, - output_base) - src_item = os.path.join(nrf_root, - input_base) + dest_item = os.path.join(_CD_STAGING_DIR, output_base) shutil.copy(src_item, dest_item) for script_file in script_files: - src_item = os.path.join(scripts_root, - script_file) - dest_item = os.path.join(sub_dir, - script_file) + src_item = os.path.join(scripts_root, script_file) + dest_item = os.path.join(sub_dir, script_file) shutil.copy(src_item, dest_item) - # gen flash script shell.run_cmd(f"cd {sub_dir}") - gen_script_path = os.path.join(scripts_root, - "gen_flashing_script.py") command = textwrap.dedent(f"""\ python3 {gen_script_path} nrfconnect --output {device_name}.flash.py @@ -260,20 +245,20 @@ def bundle_nrfconnect(device_name: str) -> None: def bundle_esp32(device_name: str) -> None: - """Reference example for bundle_ - Should copy/move files crom cicd_config()['build_dir'] - into _CD_STAGING_DIR to be tar'd and archived. + """Reference example for bundle_{platform} + functions, which should copy/move files from a build + output dir into _CD_STAGING_DIR to be archived. Args: - device_name: the device currently built. + device_name: The device to bundle. """ esp_root = os.path.join(_CHEF_SCRIPT_PATH, "esp32", "build") - manifest = os.path.join(esp_root, + manifest_file = os.path.join(esp_root, "chip-shell.flashbundle.txt") - with open(manifest) as m: - for item in m: + with open(manifest_file) as manifest: + for item in manifest: item = item.replace("\n", "") if os.sep in item: new_dir = item[:item.rindex(os.sep)] @@ -391,7 +376,8 @@ def main(argv: Sequence[str]) -> None: cd ../../.. ./examples/chef/chef.py --generate_zzz git add examples/chef/zzz_generated - Ensure you are running with the latest version of ZAP from master!""") + Ensure you are running with the latest version of ZAP from master! + """) ci_manifest = generate_device_manifest() current_zap = ci_manifest["zap_commit"] for device, device_md5 in ci_manifest["devices"].items(): @@ -463,11 +449,13 @@ def main(argv: Sequence[str]) -> None: device_name, "zap-generated") os.makedirs(device_out_dir) - shell.run_cmd(textwrap.dedent(f"""\ + command = textwrap.dedent(f"""\ {_REPO_BASE_PATH}/scripts/tools/zap/generate.py \ {_CHEF_SCRIPT_PATH}/devices/{device_name}.zap -o {device_out_dir}""")) + command = command.replace("\n", " ") + shell.run_cmd(command) shell.run_cmd(f"touch {device_out_dir}/af-gen-event.h") - generate_device_manifest(write_manifest_file=True) + generate_device_manifest(write_manifest_files=True) exit(0) # @@ -475,13 +463,13 @@ def main(argv: Sequence[str]) -> None: # if options.ci: - for device_name in [d for d in _DEVICE_LIST if d in _CI_ALLOW_LIST]: + for device_name in [d for d in _DEVICE_LIST if d in cicd_config["ci_allow_list"]]: if options.build_target == "nrfconnect": shell.run_cmd("export GNUARMEMB_TOOLCHAIN_PATH=\"$PW_ARM_CIPD_INSTALL_DIR\"") shell.run_cmd(f"cd {_CHEF_SCRIPT_PATH}") command = f"./chef.py -cbr --use_zzz -d {device_name} -t {options.build_target}" flush_print(f"Building {command}", with_border=True) - shell.run_cmd(command) + # shell.run_cmd(command) bundle(options.build_target, device_name) exit(0) @@ -495,7 +483,7 @@ def main(argv: Sequence[str]) -> None: archive_suffix = ".tar.gz" os.makedirs(archive_prefix, exist_ok=True) for device_name in _DEVICE_LIST: - for platform, platform_meta in cicd_config.items(): + for platform, platform_meta in cicd_config["cd_platforms"].items(): directory = platform_meta['build_dir'] label = platform_meta['platform_label'] output_dir = os.path.join(_CHEF_SCRIPT_PATH, directory) diff --git a/examples/chef/cicd_config.json b/examples/chef/cicd_config.json new file mode 100644 index 00000000000000..5504346c279e69 --- /dev/null +++ b/examples/chef/cicd_config.json @@ -0,0 +1,8 @@ +{ + "ci_allow_list": ["lighting-app"], + "cd_platforms": { + "linux": "linux_x86", + "esp32": "esp32-m5stack", + "nrfconnect": "nrf-nrf52840dk" + } +} diff --git a/examples/chef/cicd_meta.json b/examples/chef/cicd_meta.json deleted file mode 100644 index 34cabcb530a302..00000000000000 --- a/examples/chef/cicd_meta.json +++ /dev/null @@ -1,14 +0,0 @@ -{ - "linux": { - "build_dir": "linux/out", - "platform_label": "linux_x86" - }, - "esp32": { - "build_dir": "esp32/build", - "platform_label": "esp32-m5stack" - }, - "nrfconnect": { - "build_dir": "nrfconnect/build", - "platform_label": "nrf-nrf52840dk" - } -}