Skip to content

Commit 543246e

Browse files
Trong Nhan Maibehnazh-w
authored andcommitted
feat: generate build spec into a purl-based path in the output directory
1 parent 2bfc3b2 commit 543246e

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
@@ -272,36 +272,13 @@ def gen_build_spec(gen_build_spec_args: argparse.Namespace) -> int:
272272
gen_build_spec_args.database,
273273
)
274274

275-
build_spec_content = gen_build_spec_str(
275+
return gen_build_spec_for_purl(
276276
purl=purl,
277277
database_path=gen_build_spec_args.database,
278278
build_spec_format=build_spec_format,
279+
output_path=global_config.output_path,
279280
)
280281

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

306283
def find_source(find_args: argparse.Namespace) -> int:
307284
"""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)