diff --git a/scripts/setup/nrfconnect/get_zap.py b/scripts/setup/nrfconnect/get_zap.py index 2179631686..5845bd253a 100644 --- a/scripts/setup/nrfconnect/get_zap.py +++ b/scripts/setup/nrfconnect/get_zap.py @@ -1,7 +1,6 @@ #!/usr/bin/env python3 - # -# Copyright (c) 2023 Project CHIP Authors +# Copyright (c) 2024 Project CHIP Authors # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -15,7 +14,6 @@ # See the License for the specific language governing permissions and # limitations under the License. # - # The script parses integrations/docker/images/chip-build/Dockerfile file # from the Matter repository and looks for ENV ZAP_VERSION= string to find # currently recommended ZAP version. After that the package matching this version @@ -32,7 +30,6 @@ import argparse from zipfile import ZipFile - def get_zap_recommended_version(): matter_root = os.path.abspath(os.path.join(os.path.dirname(__file__), os.path.normpath('../../..'))) zap_version_file = os.path.join(matter_root, 'scripts/tools/zap/zap_execution.py') @@ -48,11 +45,11 @@ def get_zap_recommended_version(): raise RuntimeError("Found multiple patterns matching ZAP_VERSION.") return result[0] + except Exception as e: raise RuntimeError( f"Encountered problem when trying to read {zap_version_file} file. {e}") - def get_zap_current_version(): try: cmd_out = subprocess.check_output(['zap', '--version']) @@ -69,7 +66,6 @@ def get_zap_current_version(): print(f"ZAP file not found {e}") return None - def download_recommended_zap_package(version, package_name, location): try: print("Trying to download ZAP tool package matching your system and recommended version.") @@ -88,13 +84,14 @@ def download_recommended_zap_package(version, package_name, location): merged_version = f"{splitted_version[0]}.{month}.{day}" url = f"https://github.com/project-chip/zap/releases/download/v{merged_version}-nightly/{package_name}.zip" + print(f"Downloading {url} into {os.path.join(location, f'{package_name}.zip')}") wget.download(url, out=location) print("\n") + except Exception as e: raise RuntimeError("Invalid URL to download ZAP tool package {}".format(e)) - def clear_old_artifacts(location, overwrite): if os.path.exists(location): # Ask for user consent if the overwrite flag was not provided @@ -102,35 +99,40 @@ def clear_old_artifacts(location, overwrite): consent = input("The ZAP directory already exists in this location. Do you agree to overwrite it? Yes[y]/No[n]:") if consent.lower() != 'yes' and consent.lower() != 'y': raise RuntimeError("Couldn't download ZAP package, as the file already exists in this location.") - shutil.rmtree(location) + shutil.rmtree(location) def remove_zip(location, package_name): path = os.path.join(location, f"{package_name}.zip") print(f"Deleting zip file: {path}") os.remove(path) - def set_executable(location, package_name, filename): file = os.path.join(location, package_name, filename) st = os.stat(file) os.chmod(file, st.st_mode | stat.S_IEXEC) - def unzip_zap_package(location, package_name): + package = location + f"/{package_name}.zip" + destination = location + "/" + package_name + try: - zip = ZipFile(os.path.join(location, f"{package_name}.zip")) - zip.extractall(os.path.join(location, package_name)) - zip.close() + if (platform.system() == 'Darwin'): + subprocess.run(['unzip', package, '-d', destination], stdout = subprocess.PIPE) + else: + zip = ZipFile(os.path.join(location, f"{package_name}.zip")) + zip.extractall(os.path.join(location, package_name)) + zip.close() + except Exception as e: raise RuntimeError("Encountered problem when trying to unzip the ZAP tool package. {}".format(e)) + finally: remove_zip(location, package_name) - def print_paths_warning(paths_to_print): - messages = ["Please add the following location(s) to the system PATH:"] + paths_to_print + messages = ["Please add the following location(s) to the system PATH:"] + paths_to_print longest_message = max(messages, key=len) for item in range(len(messages)): @@ -143,23 +145,27 @@ def print_paths_warning(paths_to_print): print(f"\33[33m{message}\x1b[0m") print(f"\33[33m{frame_message}\x1b[0m") - def install_zap_package(version, location, overwrite): current_os = platform.system() + if current_os == 'Linux': package = 'zap-linux-x64' zap_executable = 'zap' zap_cli_executable = 'zap-cli' + elif current_os == 'Windows': package = 'zap-win-x64' zap_executable = 'zap.exe' zap_cli_executable = 'zap-cli.exe' + elif current_os == 'Darwin': package = 'zap-mac-x64' zap_executable = 'zap.app/Contents/MacOS/zap' zap_cli_executable = 'zap-cli' + else: raise RuntimeError(f"Couldn't find the proper ZAP tool package for the currently used operating system: {current_os}") + clear_old_artifacts(os.path.join(location, package), overwrite) download_recommended_zap_package(version, package, location) unzip_zap_package(location, package) @@ -168,19 +174,16 @@ def install_zap_package(version, location, overwrite): print("ZAP tool package was downloaded and extracted in the given location.") + if current_os == 'Darwin': print_paths_warning([os.path.join(location, package, zap_executable), os.path.join(location, package)]) else: print_paths_warning([os.path.join(location, package)]) - def main(): - parser = argparse.ArgumentParser( - description='Script helping to download the ZAP tool in the currently recommended revision.') - parser.add_argument( - "-l", "--location", help="Path to the location that should be used for storing ZAP tool package.", type=str, required=True) - parser.add_argument( - "-o", "--overwrite", help="Overwrite files without asking, in case they already exist in given location", action="store_true") + parser = argparse.ArgumentParser(description='Script helping to download the ZAP tool in the currently recommended revision.') + parser.add_argument("-l", "--location", help="Path to the location that should be used for storing ZAP tool package.", type=str, required=True) + parser.add_argument("-o", "--overwrite", help="Overwrite files without asking, in case they already exist in given location", action="store_true") args = parser.parse_args() location = os.path.abspath(args.location) @@ -190,12 +193,14 @@ def main(): if not zap_current_version: print("No ZAP tool version was found installed on this device.") install_zap_package(zap_recommended_version, location, args.overwrite) + elif zap_current_version == zap_recommended_version: print(f"Your currenly installed ZAP tool version: {zap_current_version} matches the recommended one.") + else: print(f"Your currenly installed ZAP tool version: {zap_current_version} does not match the recommended one: {zap_recommended_version}") install_zap_package(zap_recommended_version, location, args.overwrite) - if __name__ == '__main__': + main()