Skip to content

Commit ff7a502

Browse files
author
Trong Nhan Mai
committed
refactor: move the extracting build related information into the same location as reproducible central build spec generation
1 parent 9719cb8 commit ff7a502

File tree

5 files changed

+205
-173
lines changed

5 files changed

+205
-173
lines changed

src/macaron/build_spec_generator/build_spec_generator.py

Lines changed: 4 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,7 @@
1212
from sqlalchemy.orm import Session
1313

1414
from macaron.build_spec_generator.build_command_patcher import PatchCommandBuildTool, PatchValueType
15-
from macaron.build_spec_generator.internal_build_info import InternalBuildInfo
16-
from macaron.build_spec_generator.macaron_db_extractor import (
17-
lookup_any_build_command,
18-
lookup_build_tools_check,
19-
lookup_latest_component_id,
20-
lookup_repository,
21-
)
22-
from macaron.build_spec_generator.reproducible_central import gen_reproducible_central_build_spec
23-
from macaron.errors import QueryMacaronDatabaseError
24-
from macaron.slsa_analyzer.checks.build_tool_check import BuildToolFacts
15+
from macaron.build_spec_generator.reproducible_central.reproducible_central import gen_reproducible_central_build_spec
2516

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

@@ -96,119 +87,10 @@ def gen_build_spec_str(
9687
db_engine = create_engine(f"sqlite+pysqlite:///{database_path}", echo=False)
9788

9889
with Session(db_engine) as session, session.begin():
99-
internal_build_info = get_internal_build_info(
100-
purl=purl,
101-
session=session,
102-
)
103-
104-
if not internal_build_info:
105-
logger.error(
106-
"Failed to obtain necessary data for purl %s from the database %s",
107-
purl,
108-
database_path,
109-
)
110-
return None
111-
11290
match build_spec_format:
11391
case BuildSpecFormat.REPRODUCIBLE_CENTRAL:
114-
build_spec_content = gen_reproducible_central_build_spec(
115-
build_info=internal_build_info,
116-
# TODO: update this later
92+
return gen_reproducible_central_build_spec(
93+
purl=purl,
94+
session=session,
11795
patches=CLI_COMMAND_PATCHES,
11896
)
119-
120-
return build_spec_content
121-
122-
123-
def get_internal_build_info(
124-
purl: PackageURL,
125-
session: Session,
126-
) -> InternalBuildInfo | None:
127-
"""Return an ``InternalBuildInfo`` instance that captures the build related information for a PackageURL.
128-
129-
Parameters
130-
----------
131-
purl: PackageURL
132-
The PackageURL to extract information about.
133-
session: Session
134-
The SQLAlchemy Session for the Macaron database.
135-
136-
Returns
137-
-------
138-
InternalBuildInfo | None
139-
An instance of ``InternalBuildInfo`` or None if there was an error.
140-
"""
141-
try:
142-
latest_component_id = lookup_latest_component_id(
143-
purl=purl,
144-
session=session,
145-
)
146-
except QueryMacaronDatabaseError as lookup_component_error:
147-
logger.error(
148-
"Unexpected result from querying latest component id for %s. Error: %s",
149-
purl.to_string(),
150-
lookup_component_error,
151-
)
152-
return None
153-
if not latest_component_id:
154-
logger.error(
155-
"Cannot find an analysis result for PackageURL %s in the database. "
156-
+ "Please check if an analysis for it exists in the database.",
157-
purl.to_string(),
158-
)
159-
return None
160-
logger.debug("Latest component ID: %d", latest_component_id)
161-
162-
try:
163-
build_tool_facts = lookup_build_tools_check(
164-
component_id=latest_component_id,
165-
session=session,
166-
)
167-
except QueryMacaronDatabaseError as lookup_build_tools_error:
168-
logger.error(
169-
"Unexpected result from querying build tools for %s. Error: %s",
170-
purl.to_string(),
171-
lookup_build_tools_error,
172-
)
173-
return None
174-
if not build_tool_facts:
175-
logger.error(
176-
"Cannot find any build tool for PackageURL %s in the database.",
177-
purl.to_string(),
178-
)
179-
return None
180-
logger.debug("Build tools discovered from the %s table: %s", BuildToolFacts.__tablename__, build_tool_facts)
181-
182-
try:
183-
lookup_component_repository = lookup_repository(latest_component_id, session)
184-
except QueryMacaronDatabaseError as lookup_repository_error:
185-
logger.error(
186-
"Unexpected result from querying repository information for %s. Error: %s",
187-
purl.to_string(),
188-
lookup_repository_error,
189-
)
190-
return None
191-
if not lookup_component_repository:
192-
logger.error(
193-
"Cannot find any repository information for %s in the database.",
194-
purl.to_string(),
195-
)
196-
return None
197-
198-
try:
199-
lookup_build_facts = lookup_any_build_command(latest_component_id, session)
200-
except QueryMacaronDatabaseError as lookup_build_command_error:
201-
logger.error(
202-
"Unexpected result from querying all build command information for %s. Error: %s",
203-
purl.to_string(),
204-
lookup_build_command_error,
205-
)
206-
return None
207-
208-
return InternalBuildInfo(
209-
purl=purl,
210-
repository=lookup_component_repository,
211-
latest_component_id=latest_component_id,
212-
build_tool_facts=build_tool_facts,
213-
generic_build_command_facts=lookup_build_facts,
214-
)

src/macaron/build_spec_generator/internal_build_info.py

Lines changed: 0 additions & 27 deletions
This file was deleted.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Copyright (c) 2025 - 2025, Oracle and/or its affiliates. All rights reserved.
2+
# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/.
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
# Copyright (c) 2025 - 2025, Oracle and/or its affiliates. All rights reserved.
2+
# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/.
3+
4+
"""This module contains the representation of information needed for Reproducible Central Buildspec generation."""
5+
6+
import logging
7+
from collections.abc import Sequence
8+
from dataclasses import dataclass
9+
10+
from packageurl import PackageURL
11+
from sqlalchemy.orm import Session
12+
13+
from macaron.build_spec_generator.macaron_db_extractor import (
14+
GenericBuildCommandInfo,
15+
lookup_any_build_command,
16+
lookup_build_tools_check,
17+
lookup_latest_component_id,
18+
lookup_repository,
19+
)
20+
from macaron.database.table_definitions import Repository
21+
from macaron.errors import QueryMacaronDatabaseError
22+
from macaron.slsa_analyzer.checks.build_tool_check import BuildToolFacts
23+
24+
logger: logging.Logger = logging.getLogger(__name__)
25+
26+
27+
@dataclass
28+
class RcInternalBuildInfo:
29+
"""An internal representation of the information obtained from the database for a PURL.
30+
31+
This is only used for generating the Reproducible Central build spec.
32+
"""
33+
34+
purl: PackageURL
35+
repository: Repository
36+
generic_build_command_facts: Sequence[GenericBuildCommandInfo] | None
37+
latest_component_id: int
38+
build_tool_facts: Sequence[BuildToolFacts]
39+
40+
41+
def get_rc_internal_build_info(
42+
purl: PackageURL,
43+
session: Session,
44+
) -> RcInternalBuildInfo | None:
45+
"""Return an ``RcInternalBuildInfo`` instance that captures the build related information for a PackageURL.
46+
47+
Parameters
48+
----------
49+
purl: PackageURL
50+
The PackageURL to extract information about.
51+
session: Session
52+
The SQLAlchemy Session for the Macaron database.
53+
54+
Returns
55+
-------
56+
RcInternalBuildInfo | None
57+
An instance of ``RcInternalBuildInfo`` or None if there was an error.
58+
"""
59+
try:
60+
latest_component_id = lookup_latest_component_id(
61+
purl=purl,
62+
session=session,
63+
)
64+
except QueryMacaronDatabaseError as lookup_component_error:
65+
logger.error(
66+
"Unexpected result from querying latest component id for %s. Error: %s",
67+
purl.to_string(),
68+
lookup_component_error,
69+
)
70+
return None
71+
if not latest_component_id:
72+
logger.error(
73+
"Cannot find an analysis result for PackageURL %s in the database. "
74+
+ "Please check if an analysis for it exists in the database.",
75+
purl.to_string(),
76+
)
77+
return None
78+
logger.debug("Latest component ID: %d", latest_component_id)
79+
80+
try:
81+
build_tool_facts = lookup_build_tools_check(
82+
component_id=latest_component_id,
83+
session=session,
84+
)
85+
except QueryMacaronDatabaseError as lookup_build_tools_error:
86+
logger.error(
87+
"Unexpected result from querying build tools for %s. Error: %s",
88+
purl.to_string(),
89+
lookup_build_tools_error,
90+
)
91+
return None
92+
if not build_tool_facts:
93+
logger.error(
94+
"Cannot find any build tool for PackageURL %s in the database.",
95+
purl.to_string(),
96+
)
97+
return None
98+
logger.debug("Build tools discovered from the %s table: %s", BuildToolFacts.__tablename__, build_tool_facts)
99+
100+
try:
101+
lookup_component_repository = lookup_repository(latest_component_id, session)
102+
except QueryMacaronDatabaseError as lookup_repository_error:
103+
logger.error(
104+
"Unexpected result from querying repository information for %s. Error: %s",
105+
purl.to_string(),
106+
lookup_repository_error,
107+
)
108+
return None
109+
if not lookup_component_repository:
110+
logger.error(
111+
"Cannot find any repository information for %s in the database.",
112+
purl.to_string(),
113+
)
114+
return None
115+
116+
try:
117+
lookup_build_facts = lookup_any_build_command(latest_component_id, session)
118+
except QueryMacaronDatabaseError as lookup_build_command_error:
119+
logger.error(
120+
"Unexpected result from querying all build command information for %s. Error: %s",
121+
purl.to_string(),
122+
lookup_build_command_error,
123+
)
124+
return None
125+
126+
return RcInternalBuildInfo(
127+
purl=purl,
128+
repository=lookup_component_repository,
129+
latest_component_id=latest_component_id,
130+
build_tool_facts=build_tool_facts,
131+
generic_build_command_facts=lookup_build_facts,
132+
)

0 commit comments

Comments
 (0)