|
| 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