From 311052a17b2ae3db56b82bec63634a33fb72d6b1 Mon Sep 17 00:00:00 2001 From: mengyiw Date: Mon, 11 Dec 2023 17:43:00 +0100 Subject: [PATCH 01/22] Add Matter plugin --- snapcraft/parts/plugins/__init__.py | 2 + snapcraft/parts/plugins/matter_plugin.py | 138 +++++++++++++++++++++++ snapcraft/parts/plugins/register.py | 3 +- 3 files changed, 142 insertions(+), 1 deletion(-) create mode 100644 snapcraft/parts/plugins/matter_plugin.py diff --git a/snapcraft/parts/plugins/__init__.py b/snapcraft/parts/plugins/__init__.py index 385a1c0c73..4e77b9bd26 100644 --- a/snapcraft/parts/plugins/__init__.py +++ b/snapcraft/parts/plugins/__init__.py @@ -22,12 +22,14 @@ from .flutter_plugin import FlutterPlugin from .kernel_plugin import KernelPlugin from .python_plugin import PythonPlugin +from .matter_plugin import MatterPlugin from .register import register __all__ = [ "ColconPlugin", "CondaPlugin", "FlutterPlugin", + "MatterPlugin", "KernelPlugin", "PythonPlugin", "register", diff --git a/snapcraft/parts/plugins/matter_plugin.py b/snapcraft/parts/plugins/matter_plugin.py new file mode 100644 index 0000000000..78cb79c202 --- /dev/null +++ b/snapcraft/parts/plugins/matter_plugin.py @@ -0,0 +1,138 @@ +# -*- Mode:Python; indent-tabs-mode:nil; tab-width:4 -*- +# +# Copyright 2023 Canonical Ltd. +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License version 3 as +# published by the Free Software Foundation. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +"""The matter plugin.""" +import os + +from typing import Any, Dict, List, Set + +from craft_parts import infos, plugins +from overrides import overrides + +MATTER_REPO = "https://github.com/project-chip/connectedhomeip.git" +"""The repository where the matter SDK resides.""" + + +class MatterPluginProperties(plugins.PluginProperties, plugins.PluginModel): + """The part properties used by the matter plugin.""" + + # matter_branch: str + # zap_version: str + + @classmethod + @overrides + def unmarshal(cls, data: Dict[str, Any]) -> "MatterPluginProperties": + """Populate class attributes from the part specification. + + :param data: A dictionary containing part properties. + + :return: The populated plugin properties data object. + + :raise pydantic.ValidationError: If validation fails. + """ + plugin_data = plugins.extract_plugin_properties( + data, + plugin_name="matter", + # required=["matter_branch", "zap_version"] + ) + return cls(**plugin_data) + + +class MatterPlugin(plugins.Plugin): + """A plugin for matter project. + + This plugin uses the common plugin keywords. + For more information check the 'plugins' topic. + + Additionally, this plugin uses the following plugin-specific keywords: + - matter-branch + (str, no default) + The matter branch to use for the build. + - zap-version + (str, no default) + The zap version to use for the build. + """ + + properties_class = MatterPluginProperties + + def __init__( + self, + *, + properties: plugins.PluginProperties, + part_info: infos.PartInfo, + ) -> None: + super().__init__(properties=properties, part_info=part_info) + + self.matter_dir = part_info.part_build_dir + self.snap_arch = os.getenv("SNAP_ARCH") + + @overrides + def get_build_packages(self) -> Set[str]: + return { + "wget", + "unzip", + "clang", + "pkg-config", + "git", + "cmake", + "ninja-build", + "unzip", + "libssl-dev", + "libdbus-1-dev", + "libglib2.0-dev", + "libavahi-client-dev", + "python3-venv", + "python3-dev", + "python3-pip", + "libgirepository1.0-dev", + "libcairo2-dev", + "libreadline-dev", + "generate-ninja", + } + + @overrides + def get_build_environment(self) -> Dict[str, str]: + return {} + + @overrides + def get_build_snaps(self) -> Set[str]: + return set() + + @overrides + def get_build_commands(self) -> List[str]: + commands = [] + + if self.snap_arch == "arm64": + commands.extend( + [ + f"wget --no-verbose https://github.com/project-chip/zap/releases/download/v2023.11.13/zap-linux-{self.snap_arch}.zip", + f"unzip -o zap-linux-{self.snap_arch}.zip", + "echo 'export ZAP_INSTALL_PATH=$PWD'", + ] + ) + + commands.extend( + [ + f"if [ ! -d matter ]; then git clone --depth 1 -b v1.2.0.1 {MATTER_REPO} matter; fi", + "cd matter || echo 'skip clone'", + f"scripts/checkout_submodules.py --shallow --platform linux", + f"set +u && source scripts/activate.sh && set -u", + "cp -vr ./* $CRAFT_PART_INSTALL/", + "echo 'Cloned Matter repository and activated submodules'", + ] + ) + + return commands diff --git a/snapcraft/parts/plugins/register.py b/snapcraft/parts/plugins/register.py index 87b0a731eb..f1385330ac 100644 --- a/snapcraft/parts/plugins/register.py +++ b/snapcraft/parts/plugins/register.py @@ -23,12 +23,13 @@ from .flutter_plugin import FlutterPlugin from .kernel_plugin import KernelPlugin from .python_plugin import PythonPlugin +from .matter_plugin import MatterPlugin def register() -> None: """Register Snapcraft plugins.""" craft_parts.plugins.register({"colcon": ColconPlugin}) craft_parts.plugins.register({"conda": CondaPlugin}) - craft_parts.plugins.register({"flutter": FlutterPlugin}) + craft_parts.plugins.register({"matter": MatterPlugin}) craft_parts.plugins.register({"python": PythonPlugin}) craft_parts.plugins.register({"kernel": KernelPlugin}) From 76fa675a3081c39e013fe41a2b5470876469110f Mon Sep 17 00:00:00 2001 From: Mengyi Wang Date: Tue, 12 Dec 2023 15:45:05 +0100 Subject: [PATCH 02/22] Add comments for Matter SDK building related commands --- snapcraft/parts/plugins/matter_plugin.py | 20 +++++++++++++++----- snapcraft/parts/plugins/register.py | 3 ++- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/snapcraft/parts/plugins/matter_plugin.py b/snapcraft/parts/plugins/matter_plugin.py index 78cb79c202..52513c7392 100644 --- a/snapcraft/parts/plugins/matter_plugin.py +++ b/snapcraft/parts/plugins/matter_plugin.py @@ -124,14 +124,24 @@ def get_build_commands(self) -> List[str]: ] ) + """Clone Matter repository if not present""" + commands.extend( + [ + f"if [ ! -d matter ]; then git clone --depth 1 -b v1.2.0.1 {MATTER_REPO} matter && cd matter; " + f"else cd matter || echo 'skip clone'; fi" + ] + ) + + """Checkout submodules for Linux platform""" + commands.extend(["scripts/checkout_submodules.py --shallow --platform linux"]) + + """Bootstrapping script for building Matter SDK and setting up the environment""" + commands.extend(["set +u && source setup/bootstrap.sh && set -u"]) + commands.extend( [ - f"if [ ! -d matter ]; then git clone --depth 1 -b v1.2.0.1 {MATTER_REPO} matter; fi", - "cd matter || echo 'skip clone'", - f"scripts/checkout_submodules.py --shallow --platform linux", - f"set +u && source scripts/activate.sh && set -u", "cp -vr ./* $CRAFT_PART_INSTALL/", - "echo 'Cloned Matter repository and activated submodules'", + "echo 'Cloned Matter repository and built Matter SDK'", ] ) diff --git a/snapcraft/parts/plugins/register.py b/snapcraft/parts/plugins/register.py index f1385330ac..f94a873595 100644 --- a/snapcraft/parts/plugins/register.py +++ b/snapcraft/parts/plugins/register.py @@ -30,6 +30,7 @@ def register() -> None: """Register Snapcraft plugins.""" craft_parts.plugins.register({"colcon": ColconPlugin}) craft_parts.plugins.register({"conda": CondaPlugin}) - craft_parts.plugins.register({"matter": MatterPlugin}) + craft_parts.plugins.register({"flutter": FlutterPlugin}) craft_parts.plugins.register({"python": PythonPlugin}) craft_parts.plugins.register({"kernel": KernelPlugin}) + craft_parts.plugins.register({"matter": MatterPlugin}) From 815b68e2b3a67f03999e2f9f08fc2afd0c3384cb Mon Sep 17 00:00:00 2001 From: mengyiw Date: Thu, 14 Dec 2023 11:37:42 +0100 Subject: [PATCH 03/22] Use plugin name as a prefix for plugin-specific keywords --- snapcraft/parts/plugins/matter_plugin.py | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/snapcraft/parts/plugins/matter_plugin.py b/snapcraft/parts/plugins/matter_plugin.py index 52513c7392..23f8fb6d1b 100644 --- a/snapcraft/parts/plugins/matter_plugin.py +++ b/snapcraft/parts/plugins/matter_plugin.py @@ -17,7 +17,7 @@ """The matter plugin.""" import os -from typing import Any, Dict, List, Set +from typing import Any, Dict, List, Set, cast from craft_parts import infos, plugins from overrides import overrides @@ -29,8 +29,8 @@ class MatterPluginProperties(plugins.PluginProperties, plugins.PluginModel): """The part properties used by the matter plugin.""" - # matter_branch: str - # zap_version: str + matter_tag: str + matter_zap_tag: str @classmethod @overrides @@ -46,7 +46,7 @@ def unmarshal(cls, data: Dict[str, Any]) -> "MatterPluginProperties": plugin_data = plugins.extract_plugin_properties( data, plugin_name="matter", - # required=["matter_branch", "zap_version"] + # required=["matter_tag", "matter_zap_tag"] ) return cls(**plugin_data) @@ -58,10 +58,10 @@ class MatterPlugin(plugins.Plugin): For more information check the 'plugins' topic. Additionally, this plugin uses the following plugin-specific keywords: - - matter-branch + - matter-tag (str, no default) The matter branch to use for the build. - - zap-version + - matter-zap-tag (str, no default) The zap version to use for the build. """ @@ -113,12 +113,13 @@ def get_build_snaps(self) -> Set[str]: @overrides def get_build_commands(self) -> List[str]: + options = cast(MatterPluginProperties, self._options) commands = [] if self.snap_arch == "arm64": commands.extend( [ - f"wget --no-verbose https://github.com/project-chip/zap/releases/download/v2023.11.13/zap-linux-{self.snap_arch}.zip", + f"wget --no-verbose https://github.com/project-chip/zap/releases/download/{options.matter_zap_tag}/zap-linux-{self.snap_arch}.zip", f"unzip -o zap-linux-{self.snap_arch}.zip", "echo 'export ZAP_INSTALL_PATH=$PWD'", ] @@ -127,7 +128,7 @@ def get_build_commands(self) -> List[str]: """Clone Matter repository if not present""" commands.extend( [ - f"if [ ! -d matter ]; then git clone --depth 1 -b v1.2.0.1 {MATTER_REPO} matter && cd matter; " + f"if [ ! -d matter ]; then git clone --depth 1 -b {options.matter_tag} {MATTER_REPO} matter && cd matter; " f"else cd matter || echo 'skip clone'; fi" ] ) From a0fcab567f48cd5e3fc78fde2104e2ab48ec8558 Mon Sep 17 00:00:00 2001 From: mengyiw Date: Thu, 14 Dec 2023 12:41:47 +0100 Subject: [PATCH 04/22] Replace storage paths in Matter SDK --- snapcraft/parts/plugins/matter_plugin.py | 28 ++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/snapcraft/parts/plugins/matter_plugin.py b/snapcraft/parts/plugins/matter_plugin.py index 23f8fb6d1b..a0830fa48d 100644 --- a/snapcraft/parts/plugins/matter_plugin.py +++ b/snapcraft/parts/plugins/matter_plugin.py @@ -136,8 +136,32 @@ def get_build_commands(self) -> List[str]: """Checkout submodules for Linux platform""" commands.extend(["scripts/checkout_submodules.py --shallow --platform linux"]) - """Bootstrapping script for building Matter SDK and setting up the environment""" - commands.extend(["set +u && source setup/bootstrap.sh && set -u"]) + """Bootstrapping script for building Matter SDK with minimal "build" requirements and setting up the environment""" + commands.extend( + ["set +u && source setup/bootstrap.sh --platform build && set -u"] + ) + + """ + The project writes its data to /tmp which isn't persisted. + + Setting TMPDIR env var when running the app isn't sufficient as + chip_[config,counter,factory,kvs].ini still get written under /tmp. + The chip-tool currently has no way of overriding the default paths to + storage and security config files. + + Snap does not allow bind mounting a persistent directory on /tmp, + so we need to replace it in the source with another path, e.g. /mnt. + See the top-level layout definition which bind mounts a persisted + directory within the confined snap space on /mnt. + """ + """Replace storage paths""" + commands.extend( + ["sed -i 's/\/tmp/\/mnt/g' src/platform/Linux/CHIPLinuxStorage.h"] + ) + """Replace key-value store path""" + commands.extend( + ["sed -i 's/\/tmp/\/mnt/g' src/platform/Linux/CHIPPlatformConfig.h"] + ) commands.extend( [ From 872a234a4a1a61ed6356356a9319f3128a210aaf Mon Sep 17 00:00:00 2001 From: mengyiw Date: Thu, 14 Dec 2023 15:09:15 +0100 Subject: [PATCH 05/22] Format and uncomment --- snapcraft/parts/plugins/matter_plugin.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/snapcraft/parts/plugins/matter_plugin.py b/snapcraft/parts/plugins/matter_plugin.py index a0830fa48d..cbc2cdb9f7 100644 --- a/snapcraft/parts/plugins/matter_plugin.py +++ b/snapcraft/parts/plugins/matter_plugin.py @@ -44,9 +44,7 @@ def unmarshal(cls, data: Dict[str, Any]) -> "MatterPluginProperties": :raise pydantic.ValidationError: If validation fails. """ plugin_data = plugins.extract_plugin_properties( - data, - plugin_name="matter", - # required=["matter_tag", "matter_zap_tag"] + data, plugin_name="matter", required=["matter_tag", "matter_zap_tag"] ) return cls(**plugin_data) From 90740f3f7e6c5220eae990fba10d34a1eea03a3d Mon Sep 17 00:00:00 2001 From: mengyiw Date: Fri, 15 Dec 2023 12:56:00 +0100 Subject: [PATCH 06/22] Update plugin keywords names, move bootstrap script after path replacement update format --- snapcraft/parts/plugins/matter_plugin.py | 42 +++++++++++++----------- 1 file changed, 22 insertions(+), 20 deletions(-) diff --git a/snapcraft/parts/plugins/matter_plugin.py b/snapcraft/parts/plugins/matter_plugin.py index cbc2cdb9f7..7ec44e3bb2 100644 --- a/snapcraft/parts/plugins/matter_plugin.py +++ b/snapcraft/parts/plugins/matter_plugin.py @@ -29,8 +29,8 @@ class MatterPluginProperties(plugins.PluginProperties, plugins.PluginModel): """The part properties used by the matter plugin.""" - matter_tag: str - matter_zap_tag: str + matter_sdk_version: str + matter_zap_version: str @classmethod @overrides @@ -44,7 +44,7 @@ def unmarshal(cls, data: Dict[str, Any]) -> "MatterPluginProperties": :raise pydantic.ValidationError: If validation fails. """ plugin_data = plugins.extract_plugin_properties( - data, plugin_name="matter", required=["matter_tag", "matter_zap_tag"] + data, plugin_name="matter", required=["matter_sdk_version", "matter_zap_version"] ) return cls(**plugin_data) @@ -56,10 +56,10 @@ class MatterPlugin(plugins.Plugin): For more information check the 'plugins' topic. Additionally, this plugin uses the following plugin-specific keywords: - - matter-tag + - matter-sdk-version (str, no default) - The matter branch to use for the build. - - matter-zap-tag + The matter SDK version to use for the build. + - matter-zap-version (str, no default) The zap version to use for the build. """ @@ -87,7 +87,6 @@ def get_build_packages(self) -> Set[str]: "git", "cmake", "ninja-build", - "unzip", "libssl-dev", "libdbus-1-dev", "libglib2.0-dev", @@ -117,28 +116,24 @@ def get_build_commands(self) -> List[str]: if self.snap_arch == "arm64": commands.extend( [ - f"wget --no-verbose https://github.com/project-chip/zap/releases/download/{options.matter_zap_tag}/zap-linux-{self.snap_arch}.zip", + f"wget --no-verbose https://github.com/project-chip/zap/releases/download/" + f"{options.matter_zap_version}/zap-linux-{self.snap_arch}.zip", f"unzip -o zap-linux-{self.snap_arch}.zip", "echo 'export ZAP_INSTALL_PATH=$PWD'", ] ) - """Clone Matter repository if not present""" + # Clone Matter repository if not present commands.extend( [ - f"if [ ! -d matter ]; then git clone --depth 1 -b {options.matter_tag} {MATTER_REPO} matter && cd matter; " - f"else cd matter || echo 'skip clone'; fi" + f"if [ ! -d matter ]; then git clone --depth 1 -b {options.matter_sdk_version} " + f"{MATTER_REPO} matter && cd matter; else cd matter || echo 'skip clone'; fi" ] ) - """Checkout submodules for Linux platform""" + # Checkout submodules for Linux platform commands.extend(["scripts/checkout_submodules.py --shallow --platform linux"]) - """Bootstrapping script for building Matter SDK with minimal "build" requirements and setting up the environment""" - commands.extend( - ["set +u && source setup/bootstrap.sh --platform build && set -u"] - ) - """ The project writes its data to /tmp which isn't persisted. @@ -154,11 +149,18 @@ def get_build_commands(self) -> List[str]: """ """Replace storage paths""" commands.extend( - ["sed -i 's/\/tmp/\/mnt/g' src/platform/Linux/CHIPLinuxStorage.h"] + [ + r"sed -i 's/\/tmp/\/mnt/g' src/platform/Linux/CHIPLinuxStorage.h", + r"sed -i 's/\/tmp/\/mnt/g' src/platform/Linux/CHIPPlatformConfig.h", + ] ) - """Replace key-value store path""" + + """ + Bootstrapping script for building Matter SDK with minimal "build" requirements + and setting up the environment + """ commands.extend( - ["sed -i 's/\/tmp/\/mnt/g' src/platform/Linux/CHIPPlatformConfig.h"] + ["set +u && source setup/bootstrap.sh --platform build && set -u"] ) commands.extend( From 18b3a68a2a90a93b32a2b3fe1c7f98cd622bd3e7 Mon Sep 17 00:00:00 2001 From: Mengyi Wang Date: Tue, 2 Jan 2024 14:16:06 +0100 Subject: [PATCH 07/22] Enhance formatting and remove redundant copy command - address linter errors - improve readability --- snapcraft/parts/plugins/__init__.py | 2 +- snapcraft/parts/plugins/matter_plugin.py | 71 ++++++++++++------------ snapcraft/parts/plugins/register.py | 2 +- 3 files changed, 38 insertions(+), 37 deletions(-) diff --git a/snapcraft/parts/plugins/__init__.py b/snapcraft/parts/plugins/__init__.py index 4e77b9bd26..19fcbcc9fc 100644 --- a/snapcraft/parts/plugins/__init__.py +++ b/snapcraft/parts/plugins/__init__.py @@ -21,8 +21,8 @@ from .conda_plugin import CondaPlugin from .flutter_plugin import FlutterPlugin from .kernel_plugin import KernelPlugin -from .python_plugin import PythonPlugin from .matter_plugin import MatterPlugin +from .python_plugin import PythonPlugin from .register import register __all__ = [ diff --git a/snapcraft/parts/plugins/matter_plugin.py b/snapcraft/parts/plugins/matter_plugin.py index 7ec44e3bb2..955c165564 100644 --- a/snapcraft/parts/plugins/matter_plugin.py +++ b/snapcraft/parts/plugins/matter_plugin.py @@ -16,14 +16,13 @@ """The matter plugin.""" import os - from typing import Any, Dict, List, Set, cast from craft_parts import infos, plugins from overrides import overrides +# The repository where the matter SDK resides. MATTER_REPO = "https://github.com/project-chip/connectedhomeip.git" -"""The repository where the matter SDK resides.""" class MatterPluginProperties(plugins.PluginProperties, plugins.PluginModel): @@ -44,7 +43,9 @@ def unmarshal(cls, data: Dict[str, Any]) -> "MatterPluginProperties": :raise pydantic.ValidationError: If validation fails. """ plugin_data = plugins.extract_plugin_properties( - data, plugin_name="matter", required=["matter_sdk_version", "matter_zap_version"] + data, + plugin_name="matter", + required=["matter_sdk_version", "matter_zap_version"], ) return cls(**plugin_data) @@ -80,24 +81,24 @@ def __init__( @overrides def get_build_packages(self) -> Set[str]: return { - "wget", - "unzip", "clang", - "pkg-config", - "git", "cmake", - "ninja-build", - "libssl-dev", + "generate-ninja", + "git", + "libavahi-client-dev", + "libcairo2-dev", "libdbus-1-dev", + "libgirepository1.0-dev", "libglib2.0-dev", - "libavahi-client-dev", - "python3-venv", + "libreadline-dev", + "libssl-dev", + "ninja-build", + "pkg-config", "python3-dev", "python3-pip", - "libgirepository1.0-dev", - "libcairo2-dev", - "libreadline-dev", - "generate-ninja", + "python3-venv", + "unzip", + "wget", } @overrides @@ -126,28 +127,33 @@ def get_build_commands(self) -> List[str]: # Clone Matter repository if not present commands.extend( [ - f"if [ ! -d matter ]; then git clone --depth 1 -b {options.matter_sdk_version} " - f"{MATTER_REPO} matter && cd matter; else cd matter || echo 'skip clone'; fi" + "if [ ! -d matter ]; then", + f" git clone --depth 1 -b {options.matter_sdk_version} {MATTER_REPO} matter;", + "else", + " echo 'Matter repository already exists, skip clone';", + "fi", + "cd matter;", ] ) # Checkout submodules for Linux platform commands.extend(["scripts/checkout_submodules.py --shallow --platform linux"]) - """ + """ The project writes its data to /tmp which isn't persisted. - Setting TMPDIR env var when running the app isn't sufficient as - chip_[config,counter,factory,kvs].ini still get written under /tmp. - The chip-tool currently has no way of overriding the default paths to - storage and security config files. + Setting TMPDIR env var when running the app isn't sufficient as + chip_[config,counter,factory,kvs].ini still get written under /tmp. + The chip-tool currently has no way of overriding the default paths to + storage and security config files. - Snap does not allow bind mounting a persistent directory on /tmp, - so we need to replace it in the source with another path, e.g. /mnt. - See the top-level layout definition which bind mounts a persisted - directory within the confined snap space on /mnt. + Snap does not allow bind mounting a persistent directory on /tmp, + so we need to replace it in the source with another path, e.g. /mnt. + See the top-level layout definition which bind mounts a persisted + directory within the confined snap space on /mnt. """ - """Replace storage paths""" + + # Replace storage paths commands.extend( [ r"sed -i 's/\/tmp/\/mnt/g' src/platform/Linux/CHIPLinuxStorage.h", @@ -156,18 +162,13 @@ def get_build_commands(self) -> List[str]: ) """ - Bootstrapping script for building Matter SDK with minimal "build" requirements - and setting up the environment + Bootstrapping script for building Matter SDK with minimal "build" requirements + and setting up the environment. """ commands.extend( ["set +u && source setup/bootstrap.sh --platform build && set -u"] ) - commands.extend( - [ - "cp -vr ./* $CRAFT_PART_INSTALL/", - "echo 'Cloned Matter repository and built Matter SDK'", - ] - ) + commands.extend(["echo 'Built Matter SDK'"]) return commands diff --git a/snapcraft/parts/plugins/register.py b/snapcraft/parts/plugins/register.py index f94a873595..6362fcf1e0 100644 --- a/snapcraft/parts/plugins/register.py +++ b/snapcraft/parts/plugins/register.py @@ -22,8 +22,8 @@ from .conda_plugin import CondaPlugin from .flutter_plugin import FlutterPlugin from .kernel_plugin import KernelPlugin -from .python_plugin import PythonPlugin from .matter_plugin import MatterPlugin +from .python_plugin import PythonPlugin def register() -> None: From da14ed177605a56cd9262192f885a4fbead65d76 Mon Sep 17 00:00:00 2001 From: Mengyi Wang Date: Thu, 25 Jan 2024 18:26:47 +0100 Subject: [PATCH 08/22] Add unit and spread tests for Matter plugin * Add matter plugin unit test * Add craft parts matter plugin test * Export zap and pigweed environment variables * Fix tox linter * Improve formatting --- snapcraft/parts/plugins/__init__.py | 4 +- ...{matter_plugin.py => matter_sdk_plugin.py} | 128 ++++++++------ snapcraft/parts/plugins/register.py | 4 +- .../craft-parts/matter-sdk/snapcraft.yaml | 51 ++++++ .../plugins/craft-parts/matter-sdk/task.yaml | 51 ++++++ .../parts/plugins/test_matter_sdk_plugin.py | 157 ++++++++++++++++++ 6 files changed, 336 insertions(+), 59 deletions(-) rename snapcraft/parts/plugins/{matter_plugin.py => matter_sdk_plugin.py} (53%) create mode 100644 tests/spread/plugins/craft-parts/matter-sdk/snapcraft.yaml create mode 100644 tests/spread/plugins/craft-parts/matter-sdk/task.yaml create mode 100644 tests/unit/parts/plugins/test_matter_sdk_plugin.py diff --git a/snapcraft/parts/plugins/__init__.py b/snapcraft/parts/plugins/__init__.py index 19fcbcc9fc..c6fe764b75 100644 --- a/snapcraft/parts/plugins/__init__.py +++ b/snapcraft/parts/plugins/__init__.py @@ -21,7 +21,7 @@ from .conda_plugin import CondaPlugin from .flutter_plugin import FlutterPlugin from .kernel_plugin import KernelPlugin -from .matter_plugin import MatterPlugin +from .matter_sdk_plugin import MatterSdkPlugin from .python_plugin import PythonPlugin from .register import register @@ -29,7 +29,7 @@ "ColconPlugin", "CondaPlugin", "FlutterPlugin", - "MatterPlugin", + "MatterSdkPlugin", "KernelPlugin", "PythonPlugin", "register", diff --git a/snapcraft/parts/plugins/matter_plugin.py b/snapcraft/parts/plugins/matter_sdk_plugin.py similarity index 53% rename from snapcraft/parts/plugins/matter_plugin.py rename to snapcraft/parts/plugins/matter_sdk_plugin.py index 955c165564..3bf8b132fa 100644 --- a/snapcraft/parts/plugins/matter_plugin.py +++ b/snapcraft/parts/plugins/matter_sdk_plugin.py @@ -14,7 +14,7 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . -"""The matter plugin.""" +"""The matter SDK plugin.""" import os from typing import Any, Dict, List, Set, cast @@ -22,18 +22,19 @@ from overrides import overrides # The repository where the matter SDK resides. -MATTER_REPO = "https://github.com/project-chip/connectedhomeip.git" +MATTER_SDK_REPO = "https://github.com/project-chip/connectedhomeip" +ZAP_REPO = "https://github.com/project-chip/zap" -class MatterPluginProperties(plugins.PluginProperties, plugins.PluginModel): - """The part properties used by the matter plugin.""" +class MatterSdkPluginProperties(plugins.PluginProperties, plugins.PluginModel): + """The part properties used by the matter SDK plugin.""" matter_sdk_version: str - matter_zap_version: str + matter_sdk_zap_version: str @classmethod @overrides - def unmarshal(cls, data: Dict[str, Any]) -> "MatterPluginProperties": + def unmarshal(cls, data: Dict[str, Any]) -> "MatterSdkPluginProperties": """Populate class attributes from the part specification. :param data: A dictionary containing part properties. @@ -44,14 +45,14 @@ def unmarshal(cls, data: Dict[str, Any]) -> "MatterPluginProperties": """ plugin_data = plugins.extract_plugin_properties( data, - plugin_name="matter", - required=["matter_sdk_version", "matter_zap_version"], + plugin_name="matter-sdk", + required=["matter_sdk_version", "matter_sdk_zap_version"], ) return cls(**plugin_data) -class MatterPlugin(plugins.Plugin): - """A plugin for matter project. +class MatterSdkPlugin(plugins.Plugin): + """A plugin for matter SDK project. This plugin uses the common plugin keywords. For more information check the 'plugins' topic. @@ -60,12 +61,12 @@ class MatterPlugin(plugins.Plugin): - matter-sdk-version (str, no default) The matter SDK version to use for the build. - - matter-zap-version + - matter-sdk-zap-version (str, no default) The zap version to use for the build. """ - properties_class = MatterPluginProperties + properties_class = MatterSdkPluginProperties def __init__( self, @@ -75,9 +76,42 @@ def __init__( ) -> None: super().__init__(properties=properties, part_info=part_info) - self.matter_dir = part_info.part_build_dir + self.matter_sdk_dir = part_info.part_build_dir self.snap_arch = os.getenv("SNAP_ARCH") + @overrides + def get_pull_commands(self) -> List[str]: + options = cast(MatterSdkPluginProperties, self._options) + commands = [] + + if self.snap_arch == "arm64": + commands.extend( + [ + f"wget --no-verbose {ZAP_REPO}/releases/download/" + f"{options.matter_sdk_zap_version}/zap-linux-{self.snap_arch}.zip", + f"unzip -o zap-linux-{self.snap_arch}.zip -d zap", + 'set -a && echo "ZAP_INSTALL_PATH=$PWD/zap" >> matter_sdk_env && set +a', + "echo 'ZAP_INSTALL_PATH environment variable exported to matter_sdk_env file'", + ] + ) + + # Clone Matter SDK repository + commands.extend( + [ + "if [ ! -d matter ]; then", + " git init", + f" git remote add origin {MATTER_SDK_REPO}", + f" git fetch --depth 1 origin {options.matter_sdk_version}", + " git checkout FETCH_HEAD", + "fi", + ] + ) + + # Checkout submodules for Linux platform + commands.extend(["scripts/checkout_submodules.py --shallow --platform linux"]) + + return commands + @overrides def get_build_packages(self) -> Set[str]: return { @@ -111,47 +145,19 @@ def get_build_snaps(self) -> Set[str]: @overrides def get_build_commands(self) -> List[str]: - options = cast(MatterPluginProperties, self._options) commands = [] - if self.snap_arch == "arm64": - commands.extend( - [ - f"wget --no-verbose https://github.com/project-chip/zap/releases/download/" - f"{options.matter_zap_version}/zap-linux-{self.snap_arch}.zip", - f"unzip -o zap-linux-{self.snap_arch}.zip", - "echo 'export ZAP_INSTALL_PATH=$PWD'", - ] - ) - - # Clone Matter repository if not present - commands.extend( - [ - "if [ ! -d matter ]; then", - f" git clone --depth 1 -b {options.matter_sdk_version} {MATTER_REPO} matter;", - "else", - " echo 'Matter repository already exists, skip clone';", - "fi", - "cd matter;", - ] - ) + # The project writes its data to /tmp which isn't persisted. - # Checkout submodules for Linux platform - commands.extend(["scripts/checkout_submodules.py --shallow --platform linux"]) + # Setting TMPDIR env var when running the app isn't sufficient as + # chip_[config,counter,factory,kvs].ini still get written under /tmp. + # The chip-tool currently has no way of overriding the default paths to + # storage and security config files. - """ - The project writes its data to /tmp which isn't persisted. - - Setting TMPDIR env var when running the app isn't sufficient as - chip_[config,counter,factory,kvs].ini still get written under /tmp. - The chip-tool currently has no way of overriding the default paths to - storage and security config files. - - Snap does not allow bind mounting a persistent directory on /tmp, - so we need to replace it in the source with another path, e.g. /mnt. - See the top-level layout definition which bind mounts a persisted - directory within the confined snap space on /mnt. - """ + # Snap does not allow bind mounting a persistent directory on /tmp, + # so we need to replace it in the source with another path, e.g. /mnt. + # The consumer snap needs to bind mount a persisted directory within + # the confined snap space on /mnt. # Replace storage paths commands.extend( @@ -161,14 +167,26 @@ def get_build_commands(self) -> List[str]: ] ) - """ - Bootstrapping script for building Matter SDK with minimal "build" requirements - and setting up the environment. - """ + # Bootstrapping script for building Matter SDK with minimal "build" requirements + # and setting up the environment. commands.extend( - ["set +u && source setup/bootstrap.sh --platform build && set -u"] + ["set +u && source scripts/setup/bootstrap.sh --platform build && set -u"] ) commands.extend(["echo 'Built Matter SDK'"]) + # Compare and output pigweed related environment variables to matter_sdk_env env file + commands.extend( + [ + "set -a", + 'echo "PATH=$PATH" >> matter_sdk_env', + 'env | grep "^PW_" >> matter_sdk_env', + 'echo "VIRTUAL_ENV=$VIRTUAL_ENV" >> matter_sdk_env', + 'echo "CIPD_CACHE_DIR=$CIPD_CACHE_DIR" >> matter_sdk_env', + "set +a", + "echo 'pigweed related environment variables differences exported", + "to matter_sdk_env file'", + ] + ) + return commands diff --git a/snapcraft/parts/plugins/register.py b/snapcraft/parts/plugins/register.py index 6362fcf1e0..2317b8e51b 100644 --- a/snapcraft/parts/plugins/register.py +++ b/snapcraft/parts/plugins/register.py @@ -22,7 +22,7 @@ from .conda_plugin import CondaPlugin from .flutter_plugin import FlutterPlugin from .kernel_plugin import KernelPlugin -from .matter_plugin import MatterPlugin +from .matter_sdk_plugin import MatterSdkPlugin from .python_plugin import PythonPlugin @@ -33,4 +33,4 @@ def register() -> None: craft_parts.plugins.register({"flutter": FlutterPlugin}) craft_parts.plugins.register({"python": PythonPlugin}) craft_parts.plugins.register({"kernel": KernelPlugin}) - craft_parts.plugins.register({"matter": MatterPlugin}) + craft_parts.plugins.register({"matter-sdk": MatterSdkPlugin}) diff --git a/tests/spread/plugins/craft-parts/matter-sdk/snapcraft.yaml b/tests/spread/plugins/craft-parts/matter-sdk/snapcraft.yaml new file mode 100644 index 0000000000..00a10de2fe --- /dev/null +++ b/tests/spread/plugins/craft-parts/matter-sdk/snapcraft.yaml @@ -0,0 +1,51 @@ +name: matter-lighting +summary: Matter plugin test +description: An lighting application to test the matter plugin. +version: "1.0" + +base: core22 + +grade: stable +build-base: core22 +confinement: strict + +layout: + /mnt: + bind: $SNAP_COMMON/mnt + +apps: + matter-lighting: + daemon: simple + command: bin/lighting-app + install-mode: disable + plugs: + - network + - network-bind + - bluez + - avahi-control + +parts: + matter-sdk: + plugin: matter-sdk + matter-sdk-version: "1536ca20c5917578ca40ce509400e97b52751788" # use this commit with ptpython version fix; needs to be updated once matter sdk have a stable release + matter-sdk-zap-version: "v2023.11.13" + + lighting: + plugin: nil + after: [matter-sdk] + build-environment: + - MATTER_SDK_ENV: ../../matter-sdk/build/matter_sdk_env + override-build: | + # Source the Matter SDK environment variables + source $MATTER_SDK_ENV + + # Build the lighting app for snapcraft spread testing purposes + cd ../../matter-sdk/build/examples/lighting-app/linux + gn gen out/build + ninja -C out/build + + ldd out/build/chip-lighting-app + + mkdir -p $CRAFT_PART_INSTALL/bin + cp out/build/chip-lighting-app $CRAFT_PART_INSTALL/bin/lighting-app + diff --git a/tests/spread/plugins/craft-parts/matter-sdk/task.yaml b/tests/spread/plugins/craft-parts/matter-sdk/task.yaml new file mode 100644 index 0000000000..9ff1c88a60 --- /dev/null +++ b/tests/spread/plugins/craft-parts/matter-sdk/task.yaml @@ -0,0 +1,51 @@ +summary: Craft Parts matter SDK plugin test +manual: true +kill-timeout: 180m + +systems: + - ubuntu-22.04-64 + +prepare: | + #shellcheck source=tests/spread/tools/snapcraft-yaml.sh + . "$TOOLS_DIR/snapcraft-yaml.sh" + set_base "$SNAP/snap/snapcraft.yaml" + +restore: | + cd "$SNAP" + snapcraft clean + rm -f ./*.snap + + #shellcheck source=tests/spread/tools/snapcraft-yaml.sh + . "$TOOLS_DIR/snapcraft-yaml.sh" + restore_yaml "snap/snapcraft.yaml" + +execute: | + cd "$SNAP" + + # Build and install the snap + snapcraft + snap install "${SNAP}"*.snap --dangerous + + start_time=$(date +"%Y-%m-%d %H:%M:%S") + snap start matter-lighting + + # Check if storage path replacement from /tmp to SNAP_COMMON/mnt works + for file in /tmp/chip_*; do + if [ -e "$file" ]; then + echo "Error: $file should not exist." + exit 1 + fi + done + + if [ ! -e "${SNAP_COMMON/mnt/chip_*}" ]; then + echo "Error: ${SNAP_COMMON}/mnt/chip_* does not exist." + exit 1 + fi + + # Check if server initialization is complete for matter-lighting + if ! journalctl --since "$start_time" | grep matter-lighting | grep "CHIP:SVR: Server initialization complete"; then + echo "Error: matter-lighting initialization failed." + exit 1 + fi + + \ No newline at end of file diff --git a/tests/unit/parts/plugins/test_matter_sdk_plugin.py b/tests/unit/parts/plugins/test_matter_sdk_plugin.py new file mode 100644 index 0000000000..b4de6cda4e --- /dev/null +++ b/tests/unit/parts/plugins/test_matter_sdk_plugin.py @@ -0,0 +1,157 @@ +# -*- Mode:Python; indent-tabs-mode:nil; tab-width:4 -*- +# +# Copyright 2023 Canonical Ltd. +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License version 3 as published by the Free Software Foundation. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public License +# along with this program. If not, see . + + +import pytest +from craft_parts import Part, PartInfo, ProjectInfo + +from snapcraft.parts.plugins import MatterSdkPlugin + +# The repository where the matter SDK resides. +MATTER_SDK_REPO = "https://github.com/project-chip/connectedhomeip" +ZAP_REPO = "https://github.com/project-chip/zap" + + +@pytest.fixture(autouse=True) +def part_info(new_dir): + yield PartInfo( + project_info=ProjectInfo( + application_name="test", project_name="test-snap", cache_dir=new_dir + ), + part=Part("my-part", {}), + ) + + +def test_get_pull_commands(part_info): + properties = MatterSdkPlugin.properties_class.unmarshal( + {"matter-sdk-version": "master", "matter-sdk-zap-version": "v2023.11.13"} + ) + plugin = MatterSdkPlugin(properties=properties, part_info=part_info) + + sdk_version = properties.matter_sdk_version # type: ignore + zap_version = properties.matter_sdk_zap_version # type: ignore + + expected_commands = [] + + if plugin.snap_arch == "arm64": + expected_commands.extend( + [ + f"wget --no-verbose {ZAP_REPO}/releases/download/" + f"{zap_version}/zap-linux-{plugin.snap_arch}.zip", + f"unzip -o zap-linux-{plugin.snap_arch}.zip -d zap", + 'set -a && echo "ZAP_INSTALL_PATH=$PWD/zap" >> matter_sdk_env && set +a', + "echo 'ZAP_INSTALL_PATH environment variable exported to matter_sdk_env file'", + ] + ) + + expected_commands.extend( + [ + "if [ ! -d matter ]; then", + " git init", + f" git remote add origin {MATTER_SDK_REPO}", + f" git fetch --depth 1 origin {sdk_version}", + " git checkout FETCH_HEAD", + "fi", + ] + ) + + expected_commands.extend( + [ + "scripts/checkout_submodules.py --shallow --platform linux", + ] + ) + + assert plugin.get_pull_commands() == expected_commands + + +def test_get_build_snaps(part_info): + properties = MatterSdkPlugin.properties_class.unmarshal( + {"matter-sdk-version": "master", "matter-sdk-zap-version": "v2023.11.13"} + ) + plugin = MatterSdkPlugin(properties=properties, part_info=part_info) + assert plugin.get_build_snaps() == set() + + +def test_get_build_packages(part_info): + properties = MatterSdkPlugin.properties_class.unmarshal( + {"matter-sdk-version": "master", "matter-sdk-zap-version": "v2023.11.13"} + ) + plugin = MatterSdkPlugin(properties=properties, part_info=part_info) + assert plugin.get_build_packages() == { + "clang", + "cmake", + "generate-ninja", + "git", + "libavahi-client-dev", + "libcairo2-dev", + "libdbus-1-dev", + "libgirepository1.0-dev", + "libglib2.0-dev", + "libreadline-dev", + "libssl-dev", + "ninja-build", + "pkg-config", + "python3-dev", + "python3-pip", + "python3-venv", + "unzip", + "wget", + } + + +def test_get_build_environment(part_info): + properties = MatterSdkPlugin.properties_class.unmarshal( + {"matter-sdk-version": "master", "matter-sdk-zap-version": "v2023.11.13"} + ) + plugin = MatterSdkPlugin(properties=properties, part_info=part_info) + + assert plugin.get_build_environment() == {} + + +def test_get_build_commands(part_info): + properties = MatterSdkPlugin.properties_class.unmarshal( + {"matter-sdk-version": "master", "matter-sdk-zap-version": "v2023.11.13"} + ) + plugin = MatterSdkPlugin(properties=properties, part_info=part_info) + + expected_commands = [] + + expected_commands.extend( + [ + r"sed -i 's/\/tmp/\/mnt/g' src/platform/Linux/CHIPLinuxStorage.h", + r"sed -i 's/\/tmp/\/mnt/g' src/platform/Linux/CHIPPlatformConfig.h", + ] + ) + + expected_commands.extend( + ["set +u && source scripts/setup/bootstrap.sh --platform build && set -u"] + ) + expected_commands.extend(["echo 'Built Matter SDK'"]) + + expected_commands.extend( + [ + "set -a", + 'echo "PATH=$PATH" >> matter_sdk_env', + 'env | grep "^PW_" >> matter_sdk_env', + 'echo "VIRTUAL_ENV=$VIRTUAL_ENV" >> matter_sdk_env', + 'echo "CIPD_CACHE_DIR=$CIPD_CACHE_DIR" >> matter_sdk_env', + "set +a", + "echo 'pigweed related environment variables differences exported", + "to matter_sdk_env file'", + ] + ) + + assert plugin.get_build_commands() == expected_commands From 7c484e23be79a39e6c1430125c155bf36b802c0d Mon Sep 17 00:00:00 2001 From: Mengyi Date: Mon, 12 Feb 2024 14:00:12 +0100 Subject: [PATCH 09/22] Remove ZAP build, export `PATH` only --- snapcraft/parts/plugins/matter_sdk_plugin.py | 21 ++----------------- .../parts/plugins/test_matter_sdk_plugin.py | 21 ++----------------- 2 files changed, 4 insertions(+), 38 deletions(-) diff --git a/snapcraft/parts/plugins/matter_sdk_plugin.py b/snapcraft/parts/plugins/matter_sdk_plugin.py index 3bf8b132fa..b760d27b26 100644 --- a/snapcraft/parts/plugins/matter_sdk_plugin.py +++ b/snapcraft/parts/plugins/matter_sdk_plugin.py @@ -84,17 +84,6 @@ def get_pull_commands(self) -> List[str]: options = cast(MatterSdkPluginProperties, self._options) commands = [] - if self.snap_arch == "arm64": - commands.extend( - [ - f"wget --no-verbose {ZAP_REPO}/releases/download/" - f"{options.matter_sdk_zap_version}/zap-linux-{self.snap_arch}.zip", - f"unzip -o zap-linux-{self.snap_arch}.zip -d zap", - 'set -a && echo "ZAP_INSTALL_PATH=$PWD/zap" >> matter_sdk_env && set +a', - "echo 'ZAP_INSTALL_PATH environment variable exported to matter_sdk_env file'", - ] - ) - # Clone Matter SDK repository commands.extend( [ @@ -178,14 +167,8 @@ def get_build_commands(self) -> List[str]: # Compare and output pigweed related environment variables to matter_sdk_env env file commands.extend( [ - "set -a", - 'echo "PATH=$PATH" >> matter_sdk_env', - 'env | grep "^PW_" >> matter_sdk_env', - 'echo "VIRTUAL_ENV=$VIRTUAL_ENV" >> matter_sdk_env', - 'echo "CIPD_CACHE_DIR=$CIPD_CACHE_DIR" >> matter_sdk_env', - "set +a", - "echo 'pigweed related environment variables differences exported", - "to matter_sdk_env file'", + 'echo "export PATH=$PATH" >> matter_sdk_env', + "echo 'environment variable PATH has been exported to matter_sdk_env file'", ] ) diff --git a/tests/unit/parts/plugins/test_matter_sdk_plugin.py b/tests/unit/parts/plugins/test_matter_sdk_plugin.py index b4de6cda4e..b0a479ba3c 100644 --- a/tests/unit/parts/plugins/test_matter_sdk_plugin.py +++ b/tests/unit/parts/plugins/test_matter_sdk_plugin.py @@ -46,17 +46,6 @@ def test_get_pull_commands(part_info): expected_commands = [] - if plugin.snap_arch == "arm64": - expected_commands.extend( - [ - f"wget --no-verbose {ZAP_REPO}/releases/download/" - f"{zap_version}/zap-linux-{plugin.snap_arch}.zip", - f"unzip -o zap-linux-{plugin.snap_arch}.zip -d zap", - 'set -a && echo "ZAP_INSTALL_PATH=$PWD/zap" >> matter_sdk_env && set +a', - "echo 'ZAP_INSTALL_PATH environment variable exported to matter_sdk_env file'", - ] - ) - expected_commands.extend( [ "if [ ! -d matter ]; then", @@ -143,14 +132,8 @@ def test_get_build_commands(part_info): expected_commands.extend( [ - "set -a", - 'echo "PATH=$PATH" >> matter_sdk_env', - 'env | grep "^PW_" >> matter_sdk_env', - 'echo "VIRTUAL_ENV=$VIRTUAL_ENV" >> matter_sdk_env', - 'echo "CIPD_CACHE_DIR=$CIPD_CACHE_DIR" >> matter_sdk_env', - "set +a", - "echo 'pigweed related environment variables differences exported", - "to matter_sdk_env file'", + 'echo "export PATH=$PATH" >> matter_sdk_env', + "echo 'environment variable PATH has been exported to matter_sdk_env file'", ] ) From d34cc0122e187993cba51535ebe9fed99fce2510 Mon Sep 17 00:00:00 2001 From: Mengyi Date: Mon, 12 Feb 2024 14:37:02 +0100 Subject: [PATCH 10/22] Remove ZAP plugin-specific keyword --- snapcraft/parts/plugins/matter_sdk_plugin.py | 8 +------- .../plugins/craft-parts/matter-sdk/snapcraft.yaml | 1 - tests/unit/parts/plugins/test_matter_sdk_plugin.py | 13 +++++-------- 3 files changed, 6 insertions(+), 16 deletions(-) diff --git a/snapcraft/parts/plugins/matter_sdk_plugin.py b/snapcraft/parts/plugins/matter_sdk_plugin.py index b760d27b26..45e57b0efe 100644 --- a/snapcraft/parts/plugins/matter_sdk_plugin.py +++ b/snapcraft/parts/plugins/matter_sdk_plugin.py @@ -23,14 +23,11 @@ # The repository where the matter SDK resides. MATTER_SDK_REPO = "https://github.com/project-chip/connectedhomeip" -ZAP_REPO = "https://github.com/project-chip/zap" - class MatterSdkPluginProperties(plugins.PluginProperties, plugins.PluginModel): """The part properties used by the matter SDK plugin.""" matter_sdk_version: str - matter_sdk_zap_version: str @classmethod @overrides @@ -46,7 +43,7 @@ def unmarshal(cls, data: Dict[str, Any]) -> "MatterSdkPluginProperties": plugin_data = plugins.extract_plugin_properties( data, plugin_name="matter-sdk", - required=["matter_sdk_version", "matter_sdk_zap_version"], + required="matter_sdk_version", ) return cls(**plugin_data) @@ -61,9 +58,6 @@ class MatterSdkPlugin(plugins.Plugin): - matter-sdk-version (str, no default) The matter SDK version to use for the build. - - matter-sdk-zap-version - (str, no default) - The zap version to use for the build. """ properties_class = MatterSdkPluginProperties diff --git a/tests/spread/plugins/craft-parts/matter-sdk/snapcraft.yaml b/tests/spread/plugins/craft-parts/matter-sdk/snapcraft.yaml index 00a10de2fe..26f79f80b6 100644 --- a/tests/spread/plugins/craft-parts/matter-sdk/snapcraft.yaml +++ b/tests/spread/plugins/craft-parts/matter-sdk/snapcraft.yaml @@ -28,7 +28,6 @@ parts: matter-sdk: plugin: matter-sdk matter-sdk-version: "1536ca20c5917578ca40ce509400e97b52751788" # use this commit with ptpython version fix; needs to be updated once matter sdk have a stable release - matter-sdk-zap-version: "v2023.11.13" lighting: plugin: nil diff --git a/tests/unit/parts/plugins/test_matter_sdk_plugin.py b/tests/unit/parts/plugins/test_matter_sdk_plugin.py index b0a479ba3c..85b504476f 100644 --- a/tests/unit/parts/plugins/test_matter_sdk_plugin.py +++ b/tests/unit/parts/plugins/test_matter_sdk_plugin.py @@ -22,8 +22,6 @@ # The repository where the matter SDK resides. MATTER_SDK_REPO = "https://github.com/project-chip/connectedhomeip" -ZAP_REPO = "https://github.com/project-chip/zap" - @pytest.fixture(autouse=True) def part_info(new_dir): @@ -37,12 +35,11 @@ def part_info(new_dir): def test_get_pull_commands(part_info): properties = MatterSdkPlugin.properties_class.unmarshal( - {"matter-sdk-version": "master", "matter-sdk-zap-version": "v2023.11.13"} + {"matter-sdk-version": "master"} ) plugin = MatterSdkPlugin(properties=properties, part_info=part_info) sdk_version = properties.matter_sdk_version # type: ignore - zap_version = properties.matter_sdk_zap_version # type: ignore expected_commands = [] @@ -68,7 +65,7 @@ def test_get_pull_commands(part_info): def test_get_build_snaps(part_info): properties = MatterSdkPlugin.properties_class.unmarshal( - {"matter-sdk-version": "master", "matter-sdk-zap-version": "v2023.11.13"} + {"matter-sdk-version": "master"} ) plugin = MatterSdkPlugin(properties=properties, part_info=part_info) assert plugin.get_build_snaps() == set() @@ -76,7 +73,7 @@ def test_get_build_snaps(part_info): def test_get_build_packages(part_info): properties = MatterSdkPlugin.properties_class.unmarshal( - {"matter-sdk-version": "master", "matter-sdk-zap-version": "v2023.11.13"} + {"matter-sdk-version": "master"} ) plugin = MatterSdkPlugin(properties=properties, part_info=part_info) assert plugin.get_build_packages() == { @@ -103,7 +100,7 @@ def test_get_build_packages(part_info): def test_get_build_environment(part_info): properties = MatterSdkPlugin.properties_class.unmarshal( - {"matter-sdk-version": "master", "matter-sdk-zap-version": "v2023.11.13"} + {"matter-sdk-version": "master"} ) plugin = MatterSdkPlugin(properties=properties, part_info=part_info) @@ -112,7 +109,7 @@ def test_get_build_environment(part_info): def test_get_build_commands(part_info): properties = MatterSdkPlugin.properties_class.unmarshal( - {"matter-sdk-version": "master", "matter-sdk-zap-version": "v2023.11.13"} + {"matter-sdk-version": "master"} ) plugin = MatterSdkPlugin(properties=properties, part_info=part_info) From 9f5c229f351d9d279e904d504b5f215ece1e8036 Mon Sep 17 00:00:00 2001 From: Mengyi Date: Thu, 15 Feb 2024 12:33:49 +0100 Subject: [PATCH 11/22] Prepend PATH difference to existing PATH --- snapcraft/parts/plugins/matter_sdk_plugin.py | 24 ++++++++++++++++--- .../parts/plugins/test_matter_sdk_plugin.py | 20 ++++++++++++++-- 2 files changed, 39 insertions(+), 5 deletions(-) diff --git a/snapcraft/parts/plugins/matter_sdk_plugin.py b/snapcraft/parts/plugins/matter_sdk_plugin.py index 45e57b0efe..0a1487a5c8 100644 --- a/snapcraft/parts/plugins/matter_sdk_plugin.py +++ b/snapcraft/parts/plugins/matter_sdk_plugin.py @@ -150,6 +150,9 @@ def get_build_commands(self) -> List[str]: ] ) + # Store the initial value of PATH before executing the bootstrap script + commands.extend(["OLD_PATH=$PATH"]) + # Bootstrapping script for building Matter SDK with minimal "build" requirements # and setting up the environment. commands.extend( @@ -158,11 +161,26 @@ def get_build_commands(self) -> List[str]: commands.extend(["echo 'Built Matter SDK'"]) - # Compare and output pigweed related environment variables to matter_sdk_env env file + # Compare the difference between the original PATH and the modified PATH + commands.extend( + [ + 'IFS=: read -r -a OLD_PATH_ARRAY <<< "$OLD_PATH"', + 'IFS=: read -r -a NEW_PATH_ARRAY <<< "$PATH"', + "declare -a DIFFERENCE=()", + 'for element in "${NEW_PATH_ARRAY[@]}"; do', + ' if [[ ! " ${OLD_PATH_ARRAY[@]} " =~ " $element " ]]; then', + ' DIFFERENCE+=("$element")', + " fi", + "done", + 'MATTER_SDK_PATH=$(IFS=:; echo "${DIFFERENCE[*]}")', + ] + ) + + # Prepend the Matter SDK related PATH to the beginning of the PATH environment variable + # and save it to the matter-sdk-env.sh file commands.extend( [ - 'echo "export PATH=$PATH" >> matter_sdk_env', - "echo 'environment variable PATH has been exported to matter_sdk_env file'", + 'echo "export PATH=$MATTER_SDK_PATH:\\$PATH" >> matter-sdk-env.sh', ] ) diff --git a/tests/unit/parts/plugins/test_matter_sdk_plugin.py b/tests/unit/parts/plugins/test_matter_sdk_plugin.py index 85b504476f..e33b3b48e2 100644 --- a/tests/unit/parts/plugins/test_matter_sdk_plugin.py +++ b/tests/unit/parts/plugins/test_matter_sdk_plugin.py @@ -122,15 +122,31 @@ def test_get_build_commands(part_info): ] ) + expected_commands.extend(["OLD_PATH=$PATH"]) + expected_commands.extend( ["set +u && source scripts/setup/bootstrap.sh --platform build && set -u"] ) + expected_commands.extend(["echo 'Built Matter SDK'"]) expected_commands.extend( [ - 'echo "export PATH=$PATH" >> matter_sdk_env', - "echo 'environment variable PATH has been exported to matter_sdk_env file'", + 'IFS=: read -r -a OLD_PATH_ARRAY <<< "$OLD_PATH"', + 'IFS=: read -r -a NEW_PATH_ARRAY <<< "$PATH"', + "declare -a DIFFERENCE=()", + 'for element in "${NEW_PATH_ARRAY[@]}"; do', + ' if [[ ! " ${OLD_PATH_ARRAY[@]} " =~ " $element " ]]; then', + ' DIFFERENCE+=("$element")', + " fi", + "done", + 'MATTER_SDK_PATH=$(IFS=:; echo "${DIFFERENCE[*]}")', + ] + ) + + expected_commands.extend( + [ + 'echo "export PATH=$MATTER_SDK_PATH:\\$PATH" >> matter-sdk-env.sh', ] ) From 76d8ce770e6192a1e8f74aae424abc43cf615f5e Mon Sep 17 00:00:00 2001 From: Mengyi Date: Thu, 15 Feb 2024 13:46:49 +0100 Subject: [PATCH 12/22] Fix format, source matter-sdk-env.sh file in matter lighting snap --- snapcraft/parts/plugins/matter_sdk_plugin.py | 3 ++- tests/spread/plugins/craft-parts/matter-sdk/snapcraft.yaml | 4 +--- tests/unit/parts/plugins/test_matter_sdk_plugin.py | 1 + 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/snapcraft/parts/plugins/matter_sdk_plugin.py b/snapcraft/parts/plugins/matter_sdk_plugin.py index 0a1487a5c8..9701303495 100644 --- a/snapcraft/parts/plugins/matter_sdk_plugin.py +++ b/snapcraft/parts/plugins/matter_sdk_plugin.py @@ -24,6 +24,7 @@ # The repository where the matter SDK resides. MATTER_SDK_REPO = "https://github.com/project-chip/connectedhomeip" + class MatterSdkPluginProperties(plugins.PluginProperties, plugins.PluginModel): """The part properties used by the matter SDK plugin.""" @@ -43,7 +44,7 @@ def unmarshal(cls, data: Dict[str, Any]) -> "MatterSdkPluginProperties": plugin_data = plugins.extract_plugin_properties( data, plugin_name="matter-sdk", - required="matter_sdk_version", + required=["matter_sdk_version"], ) return cls(**plugin_data) diff --git a/tests/spread/plugins/craft-parts/matter-sdk/snapcraft.yaml b/tests/spread/plugins/craft-parts/matter-sdk/snapcraft.yaml index 26f79f80b6..2a38b859a3 100644 --- a/tests/spread/plugins/craft-parts/matter-sdk/snapcraft.yaml +++ b/tests/spread/plugins/craft-parts/matter-sdk/snapcraft.yaml @@ -32,11 +32,9 @@ parts: lighting: plugin: nil after: [matter-sdk] - build-environment: - - MATTER_SDK_ENV: ../../matter-sdk/build/matter_sdk_env override-build: | # Source the Matter SDK environment variables - source $MATTER_SDK_ENV + source ../../matter-sdk/build/matter-sdk-env.sh # Build the lighting app for snapcraft spread testing purposes cd ../../matter-sdk/build/examples/lighting-app/linux diff --git a/tests/unit/parts/plugins/test_matter_sdk_plugin.py b/tests/unit/parts/plugins/test_matter_sdk_plugin.py index e33b3b48e2..42eea6787d 100644 --- a/tests/unit/parts/plugins/test_matter_sdk_plugin.py +++ b/tests/unit/parts/plugins/test_matter_sdk_plugin.py @@ -23,6 +23,7 @@ # The repository where the matter SDK resides. MATTER_SDK_REPO = "https://github.com/project-chip/connectedhomeip" + @pytest.fixture(autouse=True) def part_info(new_dir): yield PartInfo( From 1cb674dd18b695326fab7a50c0a3ca42cd5874da Mon Sep 17 00:00:00 2001 From: Mengyi Date: Thu, 15 Feb 2024 18:39:39 +0100 Subject: [PATCH 13/22] Use shell parameter expansion --- snapcraft/parts/plugins/matter_sdk_plugin.py | 12 ++---------- tests/unit/parts/plugins/test_matter_sdk_plugin.py | 12 ++---------- 2 files changed, 4 insertions(+), 20 deletions(-) diff --git a/snapcraft/parts/plugins/matter_sdk_plugin.py b/snapcraft/parts/plugins/matter_sdk_plugin.py index 9701303495..1b92664e5a 100644 --- a/snapcraft/parts/plugins/matter_sdk_plugin.py +++ b/snapcraft/parts/plugins/matter_sdk_plugin.py @@ -165,15 +165,7 @@ def get_build_commands(self) -> List[str]: # Compare the difference between the original PATH and the modified PATH commands.extend( [ - 'IFS=: read -r -a OLD_PATH_ARRAY <<< "$OLD_PATH"', - 'IFS=: read -r -a NEW_PATH_ARRAY <<< "$PATH"', - "declare -a DIFFERENCE=()", - 'for element in "${NEW_PATH_ARRAY[@]}"; do', - ' if [[ ! " ${OLD_PATH_ARRAY[@]} " =~ " $element " ]]; then', - ' DIFFERENCE+=("$element")', - " fi", - "done", - 'MATTER_SDK_PATH=$(IFS=:; echo "${DIFFERENCE[*]}")', + 'MATTER_SDK_PATHS="${PATH%$OLD_PATH}"', ] ) @@ -181,7 +173,7 @@ def get_build_commands(self) -> List[str]: # and save it to the matter-sdk-env.sh file commands.extend( [ - 'echo "export PATH=$MATTER_SDK_PATH:\\$PATH" >> matter-sdk-env.sh', + 'echo "export PATH=$MATTER_SDK_PATHS:\\$PATH" >> matter-sdk-env.sh', ] ) diff --git a/tests/unit/parts/plugins/test_matter_sdk_plugin.py b/tests/unit/parts/plugins/test_matter_sdk_plugin.py index 42eea6787d..b881bc1d4d 100644 --- a/tests/unit/parts/plugins/test_matter_sdk_plugin.py +++ b/tests/unit/parts/plugins/test_matter_sdk_plugin.py @@ -133,21 +133,13 @@ def test_get_build_commands(part_info): expected_commands.extend( [ - 'IFS=: read -r -a OLD_PATH_ARRAY <<< "$OLD_PATH"', - 'IFS=: read -r -a NEW_PATH_ARRAY <<< "$PATH"', - "declare -a DIFFERENCE=()", - 'for element in "${NEW_PATH_ARRAY[@]}"; do', - ' if [[ ! " ${OLD_PATH_ARRAY[@]} " =~ " $element " ]]; then', - ' DIFFERENCE+=("$element")', - " fi", - "done", - 'MATTER_SDK_PATH=$(IFS=:; echo "${DIFFERENCE[*]}")', + 'MATTER_SDK_PATHS="${PATH%$OLD_PATH}"', ] ) expected_commands.extend( [ - 'echo "export PATH=$MATTER_SDK_PATH:\\$PATH" >> matter-sdk-env.sh', + 'echo "export PATH=$MATTER_SDK_PATHS:\\$PATH" >> matter-sdk-env.sh', ] ) From eca52bd3c5ea58c9822fcaf4e57c66d85c7c4bdb Mon Sep 17 00:00:00 2001 From: Mengyi Date: Fri, 16 Feb 2024 15:23:38 +0100 Subject: [PATCH 14/22] Remove extra separator --- snapcraft/parts/plugins/matter_sdk_plugin.py | 2 +- tests/unit/parts/plugins/test_matter_sdk_plugin.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/snapcraft/parts/plugins/matter_sdk_plugin.py b/snapcraft/parts/plugins/matter_sdk_plugin.py index 1b92664e5a..88888f23ab 100644 --- a/snapcraft/parts/plugins/matter_sdk_plugin.py +++ b/snapcraft/parts/plugins/matter_sdk_plugin.py @@ -173,7 +173,7 @@ def get_build_commands(self) -> List[str]: # and save it to the matter-sdk-env.sh file commands.extend( [ - 'echo "export PATH=$MATTER_SDK_PATHS:\\$PATH" >> matter-sdk-env.sh', + 'echo "export PATH=$MATTER_SDK_PATHS\\$PATH" >> matter-sdk-env.sh', ] ) diff --git a/tests/unit/parts/plugins/test_matter_sdk_plugin.py b/tests/unit/parts/plugins/test_matter_sdk_plugin.py index b881bc1d4d..bfb71a78cf 100644 --- a/tests/unit/parts/plugins/test_matter_sdk_plugin.py +++ b/tests/unit/parts/plugins/test_matter_sdk_plugin.py @@ -139,7 +139,7 @@ def test_get_build_commands(part_info): expected_commands.extend( [ - 'echo "export PATH=$MATTER_SDK_PATHS:\\$PATH" >> matter-sdk-env.sh', + 'echo "export PATH=$MATTER_SDK_PATHS\\$PATH" >> matter-sdk-env.sh', ] ) From 12e1e6ac3e92ddaed064e8d0f07b1894cde6215a Mon Sep 17 00:00:00 2001 From: Mengyi Date: Fri, 23 Feb 2024 09:36:52 +0100 Subject: [PATCH 15/22] Combine multiple statements into one for better readability --- .../plugins/craft-parts/matter-sdk/task.yaml | 2 +- .../parts/plugins/test_matter_sdk_plugin.py | 64 ++++++------------- 2 files changed, 19 insertions(+), 47 deletions(-) diff --git a/tests/spread/plugins/craft-parts/matter-sdk/task.yaml b/tests/spread/plugins/craft-parts/matter-sdk/task.yaml index 9ff1c88a60..1a7032b17b 100644 --- a/tests/spread/plugins/craft-parts/matter-sdk/task.yaml +++ b/tests/spread/plugins/craft-parts/matter-sdk/task.yaml @@ -24,7 +24,7 @@ execute: | # Build and install the snap snapcraft - snap install "${SNAP}"*.snap --dangerous + snap install "${SNAP}*.snap" --dangerous start_time=$(date +"%Y-%m-%d %H:%M:%S") snap start matter-lighting diff --git a/tests/unit/parts/plugins/test_matter_sdk_plugin.py b/tests/unit/parts/plugins/test_matter_sdk_plugin.py index bfb71a78cf..6b3a84b003 100644 --- a/tests/unit/parts/plugins/test_matter_sdk_plugin.py +++ b/tests/unit/parts/plugins/test_matter_sdk_plugin.py @@ -42,24 +42,15 @@ def test_get_pull_commands(part_info): sdk_version = properties.matter_sdk_version # type: ignore - expected_commands = [] - - expected_commands.extend( - [ - "if [ ! -d matter ]; then", - " git init", - f" git remote add origin {MATTER_SDK_REPO}", - f" git fetch --depth 1 origin {sdk_version}", - " git checkout FETCH_HEAD", - "fi", - ] - ) - - expected_commands.extend( - [ - "scripts/checkout_submodules.py --shallow --platform linux", - ] - ) + expected_commands = [ + "if [ ! -d matter ]; then", + " git init", + f" git remote add origin {MATTER_SDK_REPO}", + f" git fetch --depth 1 origin {sdk_version}", + " git checkout FETCH_HEAD", + "fi", + "scripts/checkout_submodules.py --shallow --platform linux", + ] assert plugin.get_pull_commands() == expected_commands @@ -114,33 +105,14 @@ def test_get_build_commands(part_info): ) plugin = MatterSdkPlugin(properties=properties, part_info=part_info) - expected_commands = [] - - expected_commands.extend( - [ - r"sed -i 's/\/tmp/\/mnt/g' src/platform/Linux/CHIPLinuxStorage.h", - r"sed -i 's/\/tmp/\/mnt/g' src/platform/Linux/CHIPPlatformConfig.h", - ] - ) - - expected_commands.extend(["OLD_PATH=$PATH"]) - - expected_commands.extend( - ["set +u && source scripts/setup/bootstrap.sh --platform build && set -u"] - ) - - expected_commands.extend(["echo 'Built Matter SDK'"]) - - expected_commands.extend( - [ - 'MATTER_SDK_PATHS="${PATH%$OLD_PATH}"', - ] - ) - - expected_commands.extend( - [ - 'echo "export PATH=$MATTER_SDK_PATHS\\$PATH" >> matter-sdk-env.sh', - ] - ) + expected_commands = [ + r"sed -i 's/\/tmp/\/mnt/g' src/platform/Linux/CHIPLinuxStorage.h", + r"sed -i 's/\/tmp/\/mnt/g' src/platform/Linux/CHIPPlatformConfig.h", + "OLD_PATH=$PATH", + "set +u && source scripts/setup/bootstrap.sh --platform build && set -u", + "echo 'Built Matter SDK'", + 'MATTER_SDK_PATHS="${PATH%$OLD_PATH}"', + 'echo "export PATH=$MATTER_SDK_PATHS\\$PATH" >> matter-sdk-env.sh', + ] assert plugin.get_build_commands() == expected_commands From 0e415a24f1b62439fb4e099430e3acce9d5d6dff Mon Sep 17 00:00:00 2001 From: Mengyi Date: Fri, 23 Feb 2024 13:58:19 +0100 Subject: [PATCH 16/22] Remove obsolete if statement --- snapcraft/parts/plugins/matter_sdk_plugin.py | 2 -- tests/unit/parts/plugins/test_matter_sdk_plugin.py | 2 -- 2 files changed, 4 deletions(-) diff --git a/snapcraft/parts/plugins/matter_sdk_plugin.py b/snapcraft/parts/plugins/matter_sdk_plugin.py index 88888f23ab..e37c04dfa4 100644 --- a/snapcraft/parts/plugins/matter_sdk_plugin.py +++ b/snapcraft/parts/plugins/matter_sdk_plugin.py @@ -82,12 +82,10 @@ def get_pull_commands(self) -> List[str]: # Clone Matter SDK repository commands.extend( [ - "if [ ! -d matter ]; then", " git init", f" git remote add origin {MATTER_SDK_REPO}", f" git fetch --depth 1 origin {options.matter_sdk_version}", " git checkout FETCH_HEAD", - "fi", ] ) diff --git a/tests/unit/parts/plugins/test_matter_sdk_plugin.py b/tests/unit/parts/plugins/test_matter_sdk_plugin.py index 6b3a84b003..1d83781173 100644 --- a/tests/unit/parts/plugins/test_matter_sdk_plugin.py +++ b/tests/unit/parts/plugins/test_matter_sdk_plugin.py @@ -43,12 +43,10 @@ def test_get_pull_commands(part_info): sdk_version = properties.matter_sdk_version # type: ignore expected_commands = [ - "if [ ! -d matter ]; then", " git init", f" git remote add origin {MATTER_SDK_REPO}", f" git fetch --depth 1 origin {sdk_version}", " git checkout FETCH_HEAD", - "fi", "scripts/checkout_submodules.py --shallow --platform linux", ] From ffcd7c6705c4f580f25f628f8d9b0323d6e3f885 Mon Sep 17 00:00:00 2001 From: Mengyi Date: Thu, 7 Mar 2024 13:54:39 +0100 Subject: [PATCH 17/22] Stage plugin env script, and flag plugin as experimental --- snapcraft/parts/lifecycle.py | 2 +- tests/spread/plugins/craft-parts/matter-sdk/snapcraft.yaml | 7 ++++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/snapcraft/parts/lifecycle.py b/snapcraft/parts/lifecycle.py index d679a345d6..ced9657a1b 100644 --- a/snapcraft/parts/lifecycle.py +++ b/snapcraft/parts/lifecycle.py @@ -50,7 +50,7 @@ import argparse -_EXPERIMENTAL_PLUGINS = ["kernel"] +_EXPERIMENTAL_PLUGINS = ["kernel, matter-sdk"] def run(command_name: str, parsed_args: "argparse.Namespace") -> None: diff --git a/tests/spread/plugins/craft-parts/matter-sdk/snapcraft.yaml b/tests/spread/plugins/craft-parts/matter-sdk/snapcraft.yaml index 2a38b859a3..995bba1bc0 100644 --- a/tests/spread/plugins/craft-parts/matter-sdk/snapcraft.yaml +++ b/tests/spread/plugins/craft-parts/matter-sdk/snapcraft.yaml @@ -28,13 +28,18 @@ parts: matter-sdk: plugin: matter-sdk matter-sdk-version: "1536ca20c5917578ca40ce509400e97b52751788" # use this commit with ptpython version fix; needs to be updated once matter sdk have a stable release + override-build: | + craftctl default + cp -r $CRAFT_PART_BUILD/matter-sdk-env.sh $CRAFT_PART_INSTALL + stage: + - matter-sdk-env.sh lighting: plugin: nil after: [matter-sdk] override-build: | # Source the Matter SDK environment variables - source ../../matter-sdk/build/matter-sdk-env.sh + source $CRAFT_STAGE/matter-sdk-env.sh # Build the lighting app for snapcraft spread testing purposes cd ../../matter-sdk/build/examples/lighting-app/linux From a9c3920e5b39a1c9598501c2cafeb4d49e1f84ca Mon Sep 17 00:00:00 2001 From: Mengyi Date: Thu, 7 Mar 2024 14:17:47 +0100 Subject: [PATCH 18/22] Revert "Stage plugin env script, and flag plugin as experimental" This reverts commit ffcd7c6705c4f580f25f628f8d9b0323d6e3f885. --- snapcraft/parts/lifecycle.py | 2 +- tests/spread/plugins/craft-parts/matter-sdk/snapcraft.yaml | 7 +------ 2 files changed, 2 insertions(+), 7 deletions(-) diff --git a/snapcraft/parts/lifecycle.py b/snapcraft/parts/lifecycle.py index ced9657a1b..d679a345d6 100644 --- a/snapcraft/parts/lifecycle.py +++ b/snapcraft/parts/lifecycle.py @@ -50,7 +50,7 @@ import argparse -_EXPERIMENTAL_PLUGINS = ["kernel, matter-sdk"] +_EXPERIMENTAL_PLUGINS = ["kernel"] def run(command_name: str, parsed_args: "argparse.Namespace") -> None: diff --git a/tests/spread/plugins/craft-parts/matter-sdk/snapcraft.yaml b/tests/spread/plugins/craft-parts/matter-sdk/snapcraft.yaml index 995bba1bc0..2a38b859a3 100644 --- a/tests/spread/plugins/craft-parts/matter-sdk/snapcraft.yaml +++ b/tests/spread/plugins/craft-parts/matter-sdk/snapcraft.yaml @@ -28,18 +28,13 @@ parts: matter-sdk: plugin: matter-sdk matter-sdk-version: "1536ca20c5917578ca40ce509400e97b52751788" # use this commit with ptpython version fix; needs to be updated once matter sdk have a stable release - override-build: | - craftctl default - cp -r $CRAFT_PART_BUILD/matter-sdk-env.sh $CRAFT_PART_INSTALL - stage: - - matter-sdk-env.sh lighting: plugin: nil after: [matter-sdk] override-build: | # Source the Matter SDK environment variables - source $CRAFT_STAGE/matter-sdk-env.sh + source ../../matter-sdk/build/matter-sdk-env.sh # Build the lighting app for snapcraft spread testing purposes cd ../../matter-sdk/build/examples/lighting-app/linux From f1c69bb530d5cb1a9828454961d73582d544991f Mon Sep 17 00:00:00 2001 From: Mengyi Date: Thu, 7 Mar 2024 15:11:26 +0100 Subject: [PATCH 19/22] Flag plugin as experimental --- snapcraft/parts/lifecycle.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/snapcraft/parts/lifecycle.py b/snapcraft/parts/lifecycle.py index d679a345d6..ced9657a1b 100644 --- a/snapcraft/parts/lifecycle.py +++ b/snapcraft/parts/lifecycle.py @@ -50,7 +50,7 @@ import argparse -_EXPERIMENTAL_PLUGINS = ["kernel"] +_EXPERIMENTAL_PLUGINS = ["kernel, matter-sdk"] def run(command_name: str, parsed_args: "argparse.Namespace") -> None: From b7b4663374b900cb21e2fa466633b504ac2b2c96 Mon Sep 17 00:00:00 2001 From: Mengyi Date: Thu, 7 Mar 2024 15:15:07 +0100 Subject: [PATCH 20/22] Stage plugin env script --- snapcraft/parts/plugins/matter_sdk_plugin.py | 6 +++--- tests/spread/plugins/craft-parts/matter-sdk/snapcraft.yaml | 2 +- tests/unit/parts/plugins/test_matter_sdk_plugin.py | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/snapcraft/parts/plugins/matter_sdk_plugin.py b/snapcraft/parts/plugins/matter_sdk_plugin.py index e37c04dfa4..4c50f12996 100644 --- a/snapcraft/parts/plugins/matter_sdk_plugin.py +++ b/snapcraft/parts/plugins/matter_sdk_plugin.py @@ -167,11 +167,11 @@ def get_build_commands(self) -> List[str]: ] ) - # Prepend the Matter SDK related PATH to the beginning of the PATH environment variable - # and save it to the matter-sdk-env.sh file + # Prepend the Matter SDK related PATH to the beginning of the PATH environment variable, + # and save it to the staging area as matter-sdk-env.sh file. commands.extend( [ - 'echo "export PATH=$MATTER_SDK_PATHS\\$PATH" >> matter-sdk-env.sh', + 'echo "export PATH=$MATTER_SDK_PATHS\\$PATH" >> $CRAFT_STAGE/matter-sdk-env.sh', ] ) diff --git a/tests/spread/plugins/craft-parts/matter-sdk/snapcraft.yaml b/tests/spread/plugins/craft-parts/matter-sdk/snapcraft.yaml index 2a38b859a3..73766084c8 100644 --- a/tests/spread/plugins/craft-parts/matter-sdk/snapcraft.yaml +++ b/tests/spread/plugins/craft-parts/matter-sdk/snapcraft.yaml @@ -34,7 +34,7 @@ parts: after: [matter-sdk] override-build: | # Source the Matter SDK environment variables - source ../../matter-sdk/build/matter-sdk-env.sh + source $CRAFT_STAGE/matter-sdk-env.sh # Build the lighting app for snapcraft spread testing purposes cd ../../matter-sdk/build/examples/lighting-app/linux diff --git a/tests/unit/parts/plugins/test_matter_sdk_plugin.py b/tests/unit/parts/plugins/test_matter_sdk_plugin.py index 1d83781173..0431fa1fd0 100644 --- a/tests/unit/parts/plugins/test_matter_sdk_plugin.py +++ b/tests/unit/parts/plugins/test_matter_sdk_plugin.py @@ -110,7 +110,7 @@ def test_get_build_commands(part_info): "set +u && source scripts/setup/bootstrap.sh --platform build && set -u", "echo 'Built Matter SDK'", 'MATTER_SDK_PATHS="${PATH%$OLD_PATH}"', - 'echo "export PATH=$MATTER_SDK_PATHS\\$PATH" >> matter-sdk-env.sh', + 'echo "export PATH=$MATTER_SDK_PATHS\\$PATH" >> $CRAFT_STAGE/matter-sdk-env.sh', ] assert plugin.get_build_commands() == expected_commands From cefacd7b0f2e92e2d5155dae689b60b9e5cc8783 Mon Sep 17 00:00:00 2001 From: Mengyi Date: Mon, 11 Mar 2024 15:45:07 +0100 Subject: [PATCH 21/22] Check experimental matter plugin in lifecycle test --- snapcraft/parts/lifecycle.py | 2 +- tests/unit/parts/test_lifecycle.py | 25 +++++++++++++++++++++++-- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/snapcraft/parts/lifecycle.py b/snapcraft/parts/lifecycle.py index ced9657a1b..2d98187b4f 100644 --- a/snapcraft/parts/lifecycle.py +++ b/snapcraft/parts/lifecycle.py @@ -50,7 +50,7 @@ import argparse -_EXPERIMENTAL_PLUGINS = ["kernel, matter-sdk"] +_EXPERIMENTAL_PLUGINS = ["kernel", "matter-sdk"] def run(command_name: str, parsed_args: "argparse.Namespace") -> None: diff --git a/tests/unit/parts/test_lifecycle.py b/tests/unit/parts/test_lifecycle.py index 773e4ffcb2..07bf75b329 100644 --- a/tests/unit/parts/test_lifecycle.py +++ b/tests/unit/parts/test_lifecycle.py @@ -30,7 +30,7 @@ from snapcraft.elf import ElfFile from snapcraft.models import MANDATORY_ADOPTABLE_FIELDS, Project from snapcraft.parts import lifecycle as parts_lifecycle -from snapcraft.parts.plugins import KernelPlugin +from snapcraft.parts.plugins import KernelPlugin, MatterSdkPlugin from snapcraft.parts.update_metadata import update_project_metadata from snapcraft.utils import get_host_architecture @@ -1044,16 +1044,37 @@ def test_lifecycle_adopt_project_vars(snapcraft_yaml, new_dir): def test_check_experimental_plugins_disabled(snapcraft_yaml, mocker): - mocker.patch("craft_parts.plugins.plugins._PLUGINS", {"kernel": KernelPlugin}) + mocker.patch( + "craft_parts.plugins.plugins._PLUGINS", + {"kernel": KernelPlugin, "matter-sdk": MatterSdkPlugin}, + ) project = Project.unmarshal( snapcraft_yaml(base="core22", parts={"foo": {"plugin": "kernel"}}) ) + with pytest.raises(errors.SnapcraftError) as raised: parts_lifecycle._check_experimental_plugins(project, False) assert str(raised.value) == ( "Plugin 'kernel' in part 'foo' is unstable and may change in the future." ) + project = Project.unmarshal( + snapcraft_yaml( + base="core22", + parts={ + "foo": { + "plugin": "matter-sdk", + "matter-sdk-version": "1536ca20c5917578ca40ce509400e97b52751788", + } + }, + ) + ) + with pytest.raises(errors.SnapcraftError) as raised: + parts_lifecycle._check_experimental_plugins(project, False) + assert str(raised.value) == ( + "Plugin 'matter-sdk' in part 'foo' is unstable and may change in the future." + ) + def test_check_experimental_plugins_enabled(snapcraft_yaml, mocker): mocker.patch("craft_parts.plugins.plugins._PLUGINS", {"kernel": KernelPlugin}) From 76df76bc1d9ecca136bc8423829f97929d3c4518 Mon Sep 17 00:00:00 2001 From: Mengyi Wang Date: Thu, 21 Mar 2024 18:05:19 +0100 Subject: [PATCH 22/22] Remove extra line Co-authored-by: Sergio Schvezov --- tests/spread/plugins/craft-parts/matter-sdk/task.yaml | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/spread/plugins/craft-parts/matter-sdk/task.yaml b/tests/spread/plugins/craft-parts/matter-sdk/task.yaml index 1a7032b17b..f293c114cd 100644 --- a/tests/spread/plugins/craft-parts/matter-sdk/task.yaml +++ b/tests/spread/plugins/craft-parts/matter-sdk/task.yaml @@ -47,5 +47,4 @@ execute: | echo "Error: matter-lighting initialization failed." exit 1 fi - \ No newline at end of file