generated from oracle/template-repo
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add maven local artifact finding
- Loading branch information
Showing
8 changed files
with
324 additions
and
121 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
# Copyright (c) 2024 - 2024, 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 declares types and utilities for handling local artifacts.""" | ||
|
||
import os | ||
from collections.abc import Mapping | ||
|
||
from packageurl import PackageURL | ||
|
||
from macaron.artifact.maven import construct_maven_repository_path | ||
from macaron.config.global_config import global_config | ||
|
||
|
||
def get_local_artifact_repo_mapper() -> Mapping[str, str]: | ||
"""Get A.""" | ||
local_artifact_mapper: dict[str, str] = {} | ||
|
||
if global_config.local_maven_repo: | ||
local_artifact_mapper["maven"] = global_config.local_maven_repo | ||
|
||
if global_config.python_venv_path: | ||
local_artifact_mapper["pypi"] = global_config.python_venv_path | ||
|
||
return local_artifact_mapper | ||
|
||
|
||
def construct_local_artifact_path_from_purl( | ||
build_purl_type: str, | ||
component_purl: PackageURL, | ||
local_artifact_repo_mapper: Mapping[str, str], | ||
) -> str | None: | ||
"""Get B.""" | ||
local_artifact_repo = local_artifact_repo_mapper.get(build_purl_type) | ||
if local_artifact_repo is None: | ||
return None | ||
|
||
artifact_path = None | ||
match build_purl_type: | ||
case "maven": | ||
group = component_purl.namespace | ||
artifact = component_purl.name | ||
version = component_purl.version | ||
|
||
if group is None or version is None: | ||
return None | ||
|
||
artifact_path = os.path.join( | ||
local_artifact_repo, | ||
"repository", | ||
construct_maven_repository_path(group, artifact, version), | ||
) | ||
case "pypi": | ||
# TODO: implement this. | ||
pass | ||
case _: | ||
return None | ||
|
||
return artifact_path | ||
|
||
|
||
def get_local_artifact_paths( | ||
purl: PackageURL, | ||
build_tool_purl_types: list[str], | ||
local_artifact_repo_mapper: Mapping[str, str], | ||
) -> dict[str, str]: | ||
"""Get C.""" | ||
result = {} | ||
|
||
for build_purl_type in build_tool_purl_types: | ||
local_artfiact_path = construct_local_artifact_path_from_purl( | ||
build_purl_type=build_purl_type, | ||
component_purl=purl, | ||
local_artifact_repo_mapper=local_artifact_repo_mapper, | ||
) | ||
|
||
if local_artfiact_path and os.path.isdir(local_artfiact_path): | ||
result[build_purl_type] = local_artfiact_path | ||
|
||
return result |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
# Copyright (c) 2024 - 2024, 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/. | ||
|
||
"""Test the local artifact utilities.""" | ||
|
||
import tempfile | ||
from collections.abc import Mapping | ||
|
||
import pytest | ||
from packageurl import PackageURL | ||
|
||
from macaron.artifact.local_artifact import construct_local_artifact_path_from_purl, get_local_artifact_paths | ||
|
||
|
||
@pytest.mark.parametrize( | ||
("build_purl_type", "purl_str", "local_artifact_repo_mapper", "expectation"), | ||
[ | ||
pytest.param( | ||
"maven", | ||
"pkg:maven/com.google.guava/[email protected]", | ||
{"maven": "/home/foo/.m2"}, | ||
"/home/foo/.m2/repository/com/google/guava/guava/33.2.1-jre", | ||
id="A maven type PURL with available local maven repo", | ||
), | ||
pytest.param( | ||
"maven", | ||
"pkg:maven/com.google.guava/[email protected]", | ||
{}, | ||
None, | ||
id="A maven type PURL without an available local maven repo", | ||
), | ||
pytest.param( | ||
"maven", | ||
"pkg:maven/com.google.guava/[email protected]", | ||
{"pypi": "/home/foo/.venv"}, | ||
None, | ||
id="A maven type PURL without an available local maven repo but there is a Python venv", | ||
), | ||
pytest.param( | ||
"maven", | ||
"pkg:maven/com.google.guava/guava", | ||
{"maven": "/home/foo/.m2"}, | ||
None, | ||
id="A maven type PURL with missing version and an available local maven repo", | ||
), | ||
pytest.param( | ||
"maven", | ||
"pkg:maven/guava", | ||
{"maven": "/home/foo/.m2"}, | ||
None, | ||
id="A maven type PURL with missing groupd Id and an available local maven repo", | ||
), | ||
pytest.param( | ||
"maven", | ||
"pkg:github/oracle/macaron", | ||
{"maven": "/home/foo/.m2"}, | ||
None, | ||
id="A git type PURL and an available local maven repo", | ||
), | ||
], | ||
) | ||
def test_construct_local_artifact_path_from_purl( | ||
build_purl_type: str, | ||
purl_str: str, | ||
local_artifact_repo_mapper: Mapping[str, str], | ||
expectation: str, | ||
) -> None: | ||
"""Test constructing a local artifact path from a given purl.""" | ||
component_purl = PackageURL.from_string(purl_str) | ||
assert ( | ||
construct_local_artifact_path_from_purl( | ||
build_purl_type=build_purl_type, | ||
component_purl=component_purl, | ||
local_artifact_repo_mapper=local_artifact_repo_mapper, | ||
) | ||
== expectation | ||
) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
("purl_str", "build_tool_purl_types"), | ||
[ | ||
pytest.param( | ||
"pkg:maven/com.google.guava/[email protected]", | ||
["maven", "pypi"], | ||
id="A maven type PURL where multiple build tool types are discovered", | ||
), | ||
], | ||
) | ||
def test_get_local_artifact_paths_non_existing( | ||
purl_str: str, | ||
build_tool_purl_types: list[str], | ||
) -> None: | ||
"""Test getting local artifact paths of non existing artifacts. | ||
The local artifact repos are available. | ||
""" | ||
purl = PackageURL.from_string(purl_str) | ||
with tempfile.TemporaryDirectory() as temp_dir: | ||
local_artifact_repo_mapper = { | ||
"maven": temp_dir, | ||
"pypi": temp_dir, | ||
} | ||
assert not get_local_artifact_paths( | ||
purl=purl, | ||
build_tool_purl_types=build_tool_purl_types, | ||
local_artifact_repo_mapper=local_artifact_repo_mapper, | ||
) |
Oops, something went wrong.