Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions src/macaron/build_spec_generator/build_command_patcher.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2025 - 2025, Oracle and/or its affiliates. All rights reserved.
# Copyright (c) 2025 - 2026, Oracle and/or its affiliates. All rights reserved.
# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/.

"""This module contains the implementation of the build command patching."""
Expand Down Expand Up @@ -83,23 +83,26 @@ def _patch_commands(
which just holds the original command as a list of string, without any changes.
"""
result: list[CLICommand] = []
for cmds in cmds_sequence:
for cmd in cmds_sequence:
# Checking if the command is a valid non-empty list.
if not cmd:
continue
effective_cli_parser = None
for cli_parser in cli_parsers:
if cli_parser.is_build_tool(cmds[0]):
if cli_parser.is_build_tool(cmd[0]):
effective_cli_parser = cli_parser
break

if not effective_cli_parser:
result.append(UnparsedCLICommand(original_cmds=cmds))
result.append(UnparsedCLICommand(original_cmds=cmd))
continue

try:
cli_command = effective_cli_parser.parse(cmds)
cli_command = effective_cli_parser.parse(cmd)
except CommandLineParseError as error:
logger.error(
"Failed to patch the cli command %s. Error %s.",
" ".join(cmds),
" ".join(cmd),
error,
)
return None
Expand All @@ -117,7 +120,7 @@ def _patch_commands(
except PatchBuildCommandError as error:
logger.error(
"Failed to patch the build command %s. Error %s.",
" ".join(cmds),
" ".join(cmd),
error,
)
return None
Expand Down
16 changes: 5 additions & 11 deletions src/macaron/build_spec_generator/common_spec/maven_spec.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2025 - 2025, Oracle and/or its affiliates. All rights reserved.
# Copyright (c) 2025 - 2026, Oracle and/or its affiliates. All rights reserved.
# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/.

"""This module includes build specification and helper classes for Maven packages."""
Expand All @@ -12,7 +12,6 @@
from macaron.build_spec_generator.common_spec.base_spec import BaseBuildSpec, BaseBuildSpecDict
from macaron.build_spec_generator.common_spec.jdk_finder import find_jdk_version_from_central_maven_repo
from macaron.build_spec_generator.common_spec.jdk_version_normalizer import normalize_jdk_version
from macaron.errors import GenerateBuildSpecError

logger: logging.Logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -46,11 +45,6 @@ def get_default_build_commands(
-------
list[list[str]]
The build command as a list[list[str]].

Raises
------
GenerateBuildSpecError
If there is no default build command available for the specified build tool.
"""
default_build_commands = []

Expand All @@ -65,11 +59,10 @@ def get_default_build_commands(
pass

if not default_build_commands:
logger.critical(
logger.debug(
"There is no default build command available for the build tools %s.",
build_tool_names,
)
raise GenerateBuildSpecError("Unable to find a default build command.")

return default_build_commands

Expand Down Expand Up @@ -118,12 +111,13 @@ def resolve_fields(self, purl: PackageURL) -> None:
selected_build_commands = self.data["build_commands"] or self.get_default_build_commands(
self.data["build_tools"]
)

patched_build_commands = patch_commands(
cmds_sequence=selected_build_commands,
patches=CLI_COMMAND_PATCHES,
)
if not patched_build_commands:
raise GenerateBuildSpecError(f"Failed to patch command sequences {selected_build_commands}.")
logger.debug("Failed to patch build command sequences %s", selected_build_commands)
self.data["build_commands"] = []
return

self.data["build_commands"] = patched_build_commands
13 changes: 4 additions & 9 deletions src/macaron/build_spec_generator/common_spec/pypi_spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

from macaron.build_spec_generator.common_spec.base_spec import BaseBuildSpec, BaseBuildSpecDict
from macaron.config.defaults import defaults
from macaron.errors import GenerateBuildSpecError, SourceCodeError, WheelTagError
from macaron.errors import SourceCodeError, WheelTagError
from macaron.json_tools import json_extract
from macaron.slsa_analyzer.package_registry import pypi_registry
from macaron.slsa_analyzer.specs.package_registry_spec import PackageRegistryInfo
Expand Down Expand Up @@ -55,11 +55,6 @@ def get_default_build_commands(
-------
list[list[str]]
The build command as a list[list[str]].

Raises
------
GenerateBuildSpecError
If there is no default build command available for the specified build tool.
"""
default_build_commands = []

Expand All @@ -77,16 +72,16 @@ def get_default_build_commands(
case "hatch":
default_build_commands.append("hatch build".split())
case "conda":
default_build_commands.append('echo("Not supported")'.split())
# TODO: update this if a build command can be used for conda.
pass
case _:
pass

if not default_build_commands:
logger.critical(
logger.debug(
"There is no default build command available for the build tools %s.",
build_tool_names,
)
raise GenerateBuildSpecError("Unable to find a default build command.")

return default_build_commands

Expand Down
98 changes: 97 additions & 1 deletion tests/build_spec_generator/common_spec/test_core.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
# Copyright (c) 2025 - 2025, Oracle and/or its affiliates. All rights reserved.
# Copyright (c) 2025 - 2026, Oracle and/or its affiliates. All rights reserved.
# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/.

"""This module contains the tests for build spec generation"""

import pytest
from packageurl import PackageURL

from macaron.build_spec_generator.common_spec.base_spec import BaseBuildSpecDict
from macaron.build_spec_generator.common_spec.core import (
ECOSYSTEMS,
LANGUAGES,
MacaronBuildToolName,
compose_shell_commands,
get_language_version,
Expand Down Expand Up @@ -141,3 +145,95 @@ def test_get_language_version(
) -> None:
"""Test the get_language_version function."""
assert get_language_version(build_command_info) == expected


@pytest.mark.parametrize(
("base_build_spec_dict"),
[
pytest.param(
BaseBuildSpecDict(
{
"macaron_version": "0.20.0",
"group_id": "foo",
"artifact_id": "bar",
"version": "1.0.0",
"git_repo": "bla",
"git_tag": "bla",
"newline": "lf",
"language_version": [],
"ecosystem": "maven",
"purl": "pkg:maven/foo/bar@1.0.0",
"language": LANGUAGES.MAVEN.value,
"build_tools": [MacaronBuildToolName.MAVEN],
"build_commands": [],
}
),
id="empty build command for maven",
),
pytest.param(
BaseBuildSpecDict(
{
"macaron_version": "0.20.0",
"group_id": "foo",
"artifact_id": "bar",
"version": "1.0.0",
"git_repo": "bla",
"git_tag": "bla",
"newline": "lf",
"language_version": [],
"ecosystem": "maven",
"purl": "pkg:maven/foo/bar@1.0.0",
"language": LANGUAGES.MAVEN.value,
"build_tools": ["ant"],
"build_commands": [["ant", "dist"]],
}
),
id="unsupported build tool for maven",
),
pytest.param(
BaseBuildSpecDict(
{
"macaron_version": "0.20.0",
"group_id": None,
"artifact_id": "bar",
"version": "1.0.0",
"git_repo": "bla",
"git_tag": "bla",
"newline": "lf",
"language_version": [],
"ecosystem": "pypi",
"purl": "pkg:pypi/bar@1.0.0",
"language": LANGUAGES.PYPI.value,
"build_tools": [MacaronBuildToolName.FLIT],
"build_commands": [],
}
),
id="empty build command for pypi",
),
pytest.param(
BaseBuildSpecDict(
{
"macaron_version": "0.20.0",
"group_id": None,
"artifact_id": "bar",
"version": "1.0.0",
"git_repo": "bla",
"git_tag": "bla",
"newline": "lf",
"language_version": [],
"ecosystem": "pypi",
"purl": "pkg:pypi/bar@1.0.0",
"language": LANGUAGES.PYPI.value,
"build_tools": ["uv"],
"build_commands": [["python", "-m", "build"]],
}
),
id="unsupported build tool for pypi",
),
],
)
def test_resolve_fields(base_build_spec_dict: BaseBuildSpecDict) -> None:
"""Test the buildspec field resolution for each ecosystem."""
ECOSYSTEMS[base_build_spec_dict["ecosystem"].upper()].value(base_build_spec_dict).resolve_fields(
PackageURL.from_string(base_build_spec_dict["purl"])
)
25 changes: 24 additions & 1 deletion tests/build_spec_generator/test_build_command_patcher.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2025 - 2025, Oracle and/or its affiliates. All rights reserved.
# Copyright (c) 2025 - 2026, Oracle and/or its affiliates. All rights reserved.
# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/.

"""This module contains the test for the build command patcher."""
Expand Down Expand Up @@ -559,3 +559,26 @@ def test_patching_multiple_commands_error(
)
is None
)


@pytest.mark.parametrize(
("original_cmd_sequence"),
[
pytest.param(
[],
id="empty sequence",
),
pytest.param(
[[]],
id="empty command",
),
],
)
def test_empty_command(maven_cli_parser: MavenCLICommandParser, original_cmd_sequence: list[list[str]]) -> None:
"""Test the patch command for empty commands."""
patch_cmds = _patch_commands(
cmds_sequence=original_cmd_sequence,
cli_parsers=[maven_cli_parser],
patches={PatchCommandBuildTool.MAVEN: {}},
)
assert patch_cmds == []
Loading