From f54994e10c5d0cd73abd845036aed3840b20eaf8 Mon Sep 17 00:00:00 2001 From: Claudio Matsuoka Date: Wed, 3 Jan 2024 14:51:39 -0300 Subject: [PATCH 1/5] fix(plugins): add proxy support to ant plugin (#579) (#618) Honor the proxy environment variable in ant projects. Signed-off-by: Ondrej Kubik --- craft_parts/plugins/ant_plugin.py | 36 +++++++++++++++++++- tests/unit/plugins/test_ant_plugin.py | 49 +++++++++++++++++++++++++++ 2 files changed, 84 insertions(+), 1 deletion(-) diff --git a/craft_parts/plugins/ant_plugin.py b/craft_parts/plugins/ant_plugin.py index 53b93ac33..5db0a94e2 100644 --- a/craft_parts/plugins/ant_plugin.py +++ b/craft_parts/plugins/ant_plugin.py @@ -17,7 +17,10 @@ """The Ant plugin.""" import logging -from typing import Any, Dict, List, Optional, Set, cast +import os +import shlex +from typing import Any, Dict, Iterator, List, Optional, Set, cast +from urllib.parse import urlsplit from overrides import override @@ -142,6 +145,15 @@ def get_build_packages(self) -> Set[str]: @override def get_build_environment(self) -> Dict[str, str]: """Return a dictionary with the environment to use in the build step.""" + # Getting ant to use a proxy requires a little work; the JRE doesn't + # help as much as it should. (java.net.useSystemProxies=true ought + # to do the trick, but it relies on desktop configuration rather + # than using the standard environment variables.) + ant_opts = [] # type: List[str] + ant_opts.extend(_get_proxy_options("http")) + ant_opts.extend(_get_proxy_options("https")) + if ant_opts: + return {"ANT_OPTS": _shlex_join(ant_opts)} return {} @override @@ -162,3 +174,25 @@ def get_build_commands(self) -> List[str]: return [ " ".join(command), ] + self._get_java_post_build_commands() + + +def _get_proxy_options(scheme: str) -> Iterator[str]: + proxy = os.environ.get(f"{scheme}_proxy") + if proxy: + parsed = urlsplit(proxy) + if parsed.hostname is not None: + yield f"-D{scheme}.proxyHost={parsed.hostname}" + if parsed.port is not None: + yield f"-D{scheme}.proxyPort={parsed.port}" + if parsed.username is not None: + yield f"-D{scheme}.proxyUser={parsed.username}" + if parsed.password is not None: + yield f"-D{scheme}.proxyPassword={parsed.password}" + + +def _shlex_join(elements: List[str]) -> str: + try: + return shlex.join(elements) + except AttributeError: + # Python older than 3.8 does not have shlex.join + return " ".join(elements) diff --git a/tests/unit/plugins/test_ant_plugin.py b/tests/unit/plugins/test_ant_plugin.py index 5767eefb5..0e1188e9f 100644 --- a/tests/unit/plugins/test_ant_plugin.py +++ b/tests/unit/plugins/test_ant_plugin.py @@ -14,6 +14,9 @@ # You should have received a copy of the GNU Lesser General Public License # along with this program. If not, see . +import os +from unittest import mock + import pytest from pydantic import ValidationError @@ -124,6 +127,52 @@ def test_get_build_environment(part_info): assert plugin.get_build_environment() == {} +@pytest.mark.parametrize( + ("env", "opts"), + [ + ({"http_proxy": "http://my-host"}, "-Dhttp.proxyHost=my-host"), + ( + {"http_proxy": "http://my-host:3128"}, + "-Dhttp.proxyHost=my-host -Dhttp.proxyPort=3128", + ), + ( + {"http_proxy": "http://user@my-host:3128"}, + "-Dhttp.proxyHost=my-host -Dhttp.proxyPort=3128 -Dhttp.proxyUser=user", + ), + ( + {"http_proxy": "http://user:pass@my-host:3128"}, + "-Dhttp.proxyHost=my-host -Dhttp.proxyPort=3128 -Dhttp.proxyUser=user -Dhttp.proxyPassword=pass", + ), + ( + {"http_proxy": "http://user:pass@my-host"}, + "-Dhttp.proxyHost=my-host -Dhttp.proxyUser=user -Dhttp.proxyPassword=pass", + ), + ({"https_proxy": "https://my-host"}, "-Dhttps.proxyHost=my-host"), + ( + {"https_proxy": "https://my-host:3128"}, + "-Dhttps.proxyHost=my-host -Dhttps.proxyPort=3128", + ), + ( + {"https_proxy": "https://user@my-host:3128"}, + "-Dhttps.proxyHost=my-host -Dhttps.proxyPort=3128 -Dhttps.proxyUser=user", + ), + ( + {"https_proxy": "https://user:pass@my-host:3128"}, + "-Dhttps.proxyHost=my-host -Dhttps.proxyPort=3128 -Dhttps.proxyUser=user -Dhttps.proxyPassword=pass", + ), + ( + {"https_proxy": "https://user:pass@my-host"}, + "-Dhttps.proxyHost=my-host -Dhttps.proxyUser=user -Dhttps.proxyPassword=pass", + ), + ], +) +def test_get_build_environment_proxy(part_info, env, opts): + properties = AntPlugin.properties_class.unmarshal({"source": "."}) + plugin = AntPlugin(properties=properties, part_info=part_info) + with mock.patch.dict(os.environ, env): + assert plugin.get_build_environment() == {"ANT_OPTS": opts} + + def test_missing_parameters(): with pytest.raises(ValidationError) as raised: AntPlugin.properties_class.unmarshal({}) From ef9bbef643f3aa6e7c7d84b45c3981b200ac5d18 Mon Sep 17 00:00:00 2001 From: Claudio Matsuoka Date: Thu, 1 Feb 2024 17:03:51 -0300 Subject: [PATCH 2/5] fix(parts): set field defaults using default_factory (#629) (#641) * fix(parts): set field defaults using default_factory (#629) * chore(lint): keep black happy * ci: update Chisel version again (#642) * test: fix feature initialization in logging tests Signed-off-by: Claudio Matsuoka Co-authored-by: thp-canonical Co-authored-by: Tiago Nobrega --- .github/workflows/tests.yaml | 2 +- craft_parts/executor/part_handler.py | 6 ++---- craft_parts/parts.py | 8 ++++---- tests/integration/test_logging.py | 10 ++++++++++ 4 files changed, 17 insertions(+), 9 deletions(-) diff --git a/.github/workflows/tests.yaml b/.github/workflows/tests.yaml index c636b853f..73c7cc99b 100644 --- a/.github/workflows/tests.yaml +++ b/.github/workflows/tests.yaml @@ -87,7 +87,7 @@ jobs: # Build Chisel: needs a newer version of go than is available in Ubuntu 20.04 sudo snap install --classic --channel=latest/stable go git clone https://github.com/canonical/chisel.git chisel-tmp - cd chisel-tmp && git checkout f0bff5a30dfdcb400b # known "good" commit until Chisel has versions + cd chisel-tmp && git checkout v0.9.0 go mod download sudo go build -o /usr/bin ./... chisel --help diff --git a/craft_parts/executor/part_handler.py b/craft_parts/executor/part_handler.py index 88bc7c46a..2bc184e82 100644 --- a/craft_parts/executor/part_handler.py +++ b/craft_parts/executor/part_handler.py @@ -57,8 +57,7 @@ def __call__( *, stdout: Stream, stderr: Stream, - ) -> StepState: - ... + ) -> StepState: ... class _UpdateHandler(Protocol): @@ -68,8 +67,7 @@ def __call__( *, stdout: Stream, stderr: Stream, - ) -> None: - ... + ) -> None: ... class PartHandler: diff --git a/craft_parts/parts.py b/craft_parts/parts.py index 6682e977a..e1cfbbb0f 100644 --- a/craft_parts/parts.py +++ b/craft_parts/parts.py @@ -54,10 +54,10 @@ class PartSpec(BaseModel): build_packages: List[str] = [] build_environment: List[Dict[str, str]] = [] build_attributes: List[str] = [] - organize_files: Dict[str, str] = Field({}, alias="organize") - overlay_files: List[str] = Field(["*"], alias="overlay") - stage_files: List[str] = Field(["*"], alias="stage") - prime_files: List[str] = Field(["*"], alias="prime") + organize_files: Dict[str, str] = Field(default_factory=lambda: {}, alias="organize") + overlay_files: List[str] = Field(default_factory=lambda: ["*"], alias="overlay") + stage_files: List[str] = Field(default_factory=lambda: ["*"], alias="stage") + prime_files: List[str] = Field(default_factory=lambda: ["*"], alias="prime") override_pull: Optional[str] = None overlay_script: Optional[str] = None override_build: Optional[str] = None diff --git a/tests/integration/test_logging.py b/tests/integration/test_logging.py index e525a9d5b..fd04e84f6 100644 --- a/tests/integration/test_logging.py +++ b/tests/integration/test_logging.py @@ -18,8 +18,18 @@ import textwrap from pathlib import Path +import craft_parts from craft_parts import main + +def setup_function(): + craft_parts.Features.reset() + + +def teardown_function(): + craft_parts.Features.reset() + + parts_yaml = textwrap.dedent( """ parts: From ad069f74c40c9e6d21290f4603209d5df77c1ea5 Mon Sep 17 00:00:00 2001 From: Claudio Matsuoka Date: Wed, 7 Feb 2024 18:58:39 -0300 Subject: [PATCH 3/5] Release 1.26.2 (#653) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * docs: update changelog * Bump version: 1.26.1 → 1.26.2 Signed-off-by: Claudio Matsuoka --- .bumpversion.cfg | 2 +- craft_parts/__init__.py | 2 +- docs/changelog.rst | 6 ++++++ docs/conf.py | 2 +- setup.py | 2 +- 5 files changed, 10 insertions(+), 4 deletions(-) diff --git a/.bumpversion.cfg b/.bumpversion.cfg index 75f090dbd..16f0b5f53 100644 --- a/.bumpversion.cfg +++ b/.bumpversion.cfg @@ -1,5 +1,5 @@ [bumpversion] -current_version = 1.26.1 +current_version = 1.26.2 commit = True tag = True diff --git a/craft_parts/__init__.py b/craft_parts/__init__.py index d0efe2d9a..738171aca 100644 --- a/craft_parts/__init__.py +++ b/craft_parts/__init__.py @@ -16,7 +16,7 @@ """Craft a project from several parts.""" -__version__ = "1.26.1" +__version__ = "1.26.2" from . import plugins from .actions import Action, ActionProperties, ActionType diff --git a/docs/changelog.rst b/docs/changelog.rst index 7321eec77..bc21833ef 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -2,6 +2,12 @@ Changelog ********* +1.26.2 (2024-02-07) +------------------- + +- Fix default setting in aliased part fields +- Fix proxy setting in ant plugin + 1.26.1 (2023-12-13) ------------------- diff --git a/docs/conf.py b/docs/conf.py index 23c20e27d..48a5bca6a 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -38,7 +38,7 @@ author = "Canonical Ltd." # The full version, including alpha/beta/rc tags -release = "1.26.1" +release = "1.26.2" # Open Graph configuration - defines what is displayed in the website preview ogp_site_url = "https://canonical-craft-parts.readthedocs-hosted.com" diff --git a/setup.py b/setup.py index 3761ca673..2ed60cf30 100644 --- a/setup.py +++ b/setup.py @@ -21,7 +21,7 @@ from setuptools import find_packages, setup # type: ignore -VERSION = "1.26.1" +VERSION = "1.26.2" with open("README.md") as readme_file: readme = readme_file.read() From 0289c18048f66eabdcfba439889eb52b600ee4c8 Mon Sep 17 00:00:00 2001 From: Claudio Matsuoka Date: Fri, 23 Feb 2024 14:05:04 -0300 Subject: [PATCH 4/5] chore(lint): address black formatting error Signed-off-by: Claudio Matsuoka --- craft_parts/executor/part_handler.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/craft_parts/executor/part_handler.py b/craft_parts/executor/part_handler.py index 39e4f38f5..2cfd6fd27 100644 --- a/craft_parts/executor/part_handler.py +++ b/craft_parts/executor/part_handler.py @@ -57,8 +57,7 @@ def __call__( *, stdout: Stream, stderr: Stream, - ) -> StepState: - ... + ) -> StepState: ... class _UpdateHandler(Protocol): From 5e3a2230f2ffc59860b3a553c959464479e04a2b Mon Sep 17 00:00:00 2001 From: Claudio Matsuoka Date: Fri, 23 Feb 2024 14:29:10 -0300 Subject: [PATCH 5/5] chore(lint): address black formatting error Signed-off-by: Claudio Matsuoka --- craft_parts/executor/part_handler.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/craft_parts/executor/part_handler.py b/craft_parts/executor/part_handler.py index 2cfd6fd27..abc5892e7 100644 --- a/craft_parts/executor/part_handler.py +++ b/craft_parts/executor/part_handler.py @@ -67,8 +67,7 @@ def __call__( *, stdout: Stream, stderr: Stream, - ) -> None: - ... + ) -> None: ... class PartHandler: