Skip to content

Commit 5d6a99c

Browse files
author
Trong Nhan Mai
committed
feat: generate build spec into a purl-based path in the output directory
1 parent 3734f6e commit 5d6a99c

File tree

4 files changed

+56
-33
lines changed

4 files changed

+56
-33
lines changed

src/macaron/__main__.py

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
import macaron
1717
from macaron.build_spec_generator.build_spec_generator import (
1818
BuildSpecFormat,
19-
gen_build_spec_str,
19+
gen_build_spec_for_purl,
2020
)
2121
from macaron.config.defaults import create_defaults, load_defaults
2222
from macaron.config.global_config import global_config
@@ -273,36 +273,13 @@ def gen_build_spec(gen_build_spec_args: argparse.Namespace) -> int:
273273
gen_build_spec_args.database,
274274
)
275275

276-
build_spec_content = gen_build_spec_str(
276+
return gen_build_spec_for_purl(
277277
purl=purl,
278278
database_path=gen_build_spec_args.database,
279279
build_spec_format=build_spec_format,
280+
output_path=global_config.output_path,
280281
)
281282

282-
if not build_spec_content:
283-
logger.error("Error while generate reproducible central build spec.")
284-
return os.EX_DATAERR
285-
286-
logger.debug("Build spec content: \n%s", build_spec_content)
287-
build_spec_filepath = os.path.join(global_config.output_path, "macaron.buildspec")
288-
try:
289-
with open(build_spec_filepath, mode="w", encoding="utf-8") as file:
290-
logger.info(
291-
"Generating the %s format build spec to %s.",
292-
build_spec_format.value,
293-
os.path.relpath(build_spec_filepath, os.getcwd()),
294-
)
295-
file.write(build_spec_content)
296-
except OSError as error:
297-
logger.error(
298-
"Could not generate the Buildspec to %s. Error: %s",
299-
os.path.relpath(build_spec_filepath, os.getcwd()),
300-
error,
301-
)
302-
return os.EX_DATAERR
303-
304-
return os.EX_OK
305-
306283

307284
def find_source(find_args: argparse.Namespace) -> int:
308285
"""Perform repo and commit finding for a passed PURL, or commit finding for a passed PURL and repo."""

src/macaron/build_spec_generator/build_spec_generator.py

Lines changed: 51 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"""This module contains the functions used for generating build specs from the Macaron database."""
55

66
import logging
7+
import os
78
from collections.abc import Mapping
89
from enum import Enum
910

@@ -13,6 +14,7 @@
1314

1415
from macaron.build_spec_generator.build_command_patcher import PatchCommandBuildTool, PatchValueType
1516
from macaron.build_spec_generator.reproducible_central.reproducible_central import gen_reproducible_central_build_spec
17+
from macaron.path_utils.purl_based_path import get_purl_based_dir
1618

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

@@ -63,11 +65,12 @@ class BuildSpecFormat(str, Enum):
6365
}
6466

6567

66-
def gen_build_spec_str(
68+
def gen_build_spec_for_purl(
6769
purl: PackageURL,
6870
database_path: str,
6971
build_spec_format: BuildSpecFormat,
70-
) -> str | None:
72+
output_path: str,
73+
) -> int:
7174
"""Return the content of a build spec file from a given PURL.
7275
7376
Parameters
@@ -81,16 +84,59 @@ def gen_build_spec_str(
8184
8285
Returns
8386
-------
84-
str | None
85-
The build spec content as a string, or None if there is an error.
87+
int
88+
The exit code for this function. ``os.EX_OK`` if everything is fine, ``os.EX_OSERR`` if the
89+
buildspec file cannot be created in the local filesystem, ``os.EX_DATAERR`` if there was an
90+
error in generate the content for the buildspec file.
8691
"""
8792
db_engine = create_engine(f"sqlite+pysqlite:///{database_path}", echo=False)
8893

8994
with Session(db_engine) as session, session.begin():
95+
build_spec_content = None
9096
match build_spec_format:
9197
case BuildSpecFormat.REPRODUCIBLE_CENTRAL:
92-
return gen_reproducible_central_build_spec(
98+
build_spec_content = gen_reproducible_central_build_spec(
9399
purl=purl,
94100
session=session,
95101
patches=CLI_COMMAND_PATCHES,
96102
)
103+
104+
if not build_spec_content:
105+
logger.error("Error while generate reproducible central build spec.")
106+
return os.EX_DATAERR
107+
108+
logger.debug("Build spec content: \n%s", build_spec_content)
109+
110+
build_spec_filepath = os.path.join(
111+
output_path,
112+
"buildspec",
113+
get_purl_based_dir(
114+
purl_name=purl.name,
115+
purl_namespace=purl.namespace,
116+
purl_type=purl.type,
117+
),
118+
"macaron.buildspec",
119+
)
120+
121+
os.makedirs(
122+
name=os.path.dirname(build_spec_filepath),
123+
exist_ok=True,
124+
)
125+
126+
try:
127+
with open(build_spec_filepath, mode="w", encoding="utf-8") as file:
128+
logger.info(
129+
"Generating the %s format build spec to %s.",
130+
build_spec_format.value,
131+
os.path.relpath(build_spec_filepath, os.getcwd()),
132+
)
133+
file.write(build_spec_content)
134+
except OSError as error:
135+
logger.error(
136+
"Could not generate the Buildspec to %s. Error: %s",
137+
os.path.relpath(build_spec_filepath, os.getcwd()),
138+
error,
139+
)
140+
return os.EX_OSERR
141+
142+
return os.EX_OK

tests/integration/cases/behnazh-w_example-maven-app_gen_rc_build_spec/test.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,5 @@ steps:
2929
kind: compare
3030
options:
3131
kind: rc_build_spec
32-
result: ./output/macaron.buildspec
32+
result: ./output/buildspec/github/behnazh-w/example-maven-app/macaron.buildspec
3333
expected: expected_macaron.buildspec

tests/integration/cases/micronaut-projects_micronaut-core/test.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,5 +44,5 @@ steps:
4444
kind: compare
4545
options:
4646
kind: rc_build_spec
47-
result: ./output/macaron.buildspec
47+
result: ./output/buildspec/maven/io_micronaut/micronaut-core/macaron.buildspec
4848
expected: expected_macaron.buildspec

0 commit comments

Comments
 (0)