macaron.build_spec_generator.common_spec package
Submodules
macaron.build_spec_generator.common_spec.base_spec module
This module includes base build specification and helper classes.
- class macaron.build_spec_generator.common_spec.base_spec.BaseBuildSpecDict
Bases:
TypedDictInitialize base build specification.
It supports multiple languages, build tools, and additional metadata for enhanced traceability.
-
build_tools:
Required[list[str]] The build tools or package managers, e.g., ‘maven’, ‘gradle’, ‘pip’, ‘poetry’, ‘npm’, ‘yarn’.
-
language_version:
Required[list[str]] The version of the programming language or runtime, e.g., ‘11’ for JDK, ‘3.11’ for Python.
-
build_tools:
- class macaron.build_spec_generator.common_spec.base_spec.BaseBuildSpec
Bases:
ABCAbstract base class for build specification behavior and field resolution.
- abstractmethod resolve_fields(purl)
Resolve fields that require special logic for a specific build ecosystem.
- Return type:
Notes
This method should be implemented by subclasses to handle logic specific to a given package ecosystem, such as Maven or PyPI.
- abstractmethod get_default_build_commands(build_tool_names)
Return the default build commands for the build tools.
macaron.build_spec_generator.common_spec.core module
This module contains the logic to generate a build spec in a generic format that can be transformed if needed.
- class macaron.build_spec_generator.common_spec.core.ECOSYSTEMS(value)
Bases:
EnumThis Enum provides implementation mappings for supported ecosystems.
- MAVEN(data) = <class 'macaron.build_spec_generator.common_spec.maven_spec.MavenBuildSpec'>
The Maven build specification.
- PYPI(data) = <class 'macaron.build_spec_generator.common_spec.pypi_spec.PyPIBuildSpec'>
The PyPI build specification.
- class macaron.build_spec_generator.common_spec.core.LANGUAGES(value)
Bases:
EnumThis Enum provides mappings for supported languages.
- MAVEN = 'java'
The language used in the Maven build ecosystem.
- PYPI = 'python'
The language used in the PyPI build ecosystem.
- class macaron.build_spec_generator.common_spec.core.MacaronBuildToolName(value)
-
Represent the name of a build tool that Macaron stores in the database.
This doesn’t cover all build tools that Macaron supports, and ONLY includes the ones that we support generating build spec for.
- MAVEN = 'maven'
- GRADLE = 'gradle'
- PIP = 'pip'
- POETRY = 'poetry'
- FLIT = 'flit'
- HATCH = 'hatch'
- CONDA = 'conda'
- macaron.build_spec_generator.common_spec.core.format_build_command_info(build_command_info)
Return the prettified str format for a list of GenericBuildCommandInfo instances.
- Parameters:
build_command_info (GenericBuildCommandInfo) – A list of
GenericBuildCommandInfoinstances.- Returns:
The prettified output.
- Return type:
- macaron.build_spec_generator.common_spec.core.remove_shell_quote(cmd)
Remove shell quotes from a shell command.
- Parameters:
- Returns:
The shell command with all quote removed.
- Return type:
Examples
>>> cmd = "mvn -f fit/core-reference/pom.xml verify '-Dit.test=RESTITCase' '-Dmodernizer.skip=true' '-Drat.skip=true'" >>> remove_shell_quote(cmd.split()) ['mvn', '-f', 'fit/core-reference/pom.xml', 'verify', '-Dit.test=RESTITCase', '-Dmodernizer.skip=true', '-Drat.skip=true']
- macaron.build_spec_generator.common_spec.core.compose_shell_commands(cmds_sequence)
Combine a sequence of command fragments into a single shell command suitable for a build spec.
- macaron.build_spec_generator.common_spec.core.get_macaron_build_tool_names(build_tool_facts, target_language)
Retrieve the Macaron build tool names for supported projects from the database facts.
Iterates over the provided build tool facts and returns the list of valid MacaronBuildToolName for a supported language.
- Parameters:
build_tool_facts (Sequence[BuildToolFacts]) – A sequence of build tool fact records to be searched.
target_language (str) – The target build language.
- Returns:
The corresponding Macaron build tool names, or None otherwise.
- Return type:
list[MacaronBuildToolName] None
- macaron.build_spec_generator.common_spec.core.get_build_tool_names(component_id, session, target_language)
Retrieve the Macaron build tool names for a given component.
Queries the database for build tool facts associated with the specified component ID. It returns the corresponding list of MacaronBuildToolName if found.
- Parameters:
- Returns:
The corresponding build tool name for the component if available, otherwise None.
- Return type:
list[MacaronBuildToolName] | None
- macaron.build_spec_generator.common_spec.core.get_build_command_info(component_id, session)
Return the highest confidence build command information from the database for a component.
The build command is found by looking up CheckFacts for build-related checks.
- Parameters:
component_id (int) – The id of the component we are finding the build command for.
session (sqlalchemy.orm.Session) – The SQLAlchemy Session opened for the database to extract build information.
- Returns:
The GenericBuildCommandInfo object for the highest confidence build command; or None if there was an error, or no build command is found from the database.
- Return type:
GenericBuildCommandInfo | None
- macaron.build_spec_generator.common_spec.core.get_language_version(build_command_info)
Retrieve the language version from a GenericBuildCommandInfo object.
If available, returns a language version from the language_versions list associated with the provided GenericBuildCommandInfo object. Currently, this function returns the last element in the list. If the list is empty, returns None.
Notes
The selection of the last element from language_versions is a temporary strategy, as more robust selection logic may be implemented in the future depending on requirements for specific language/runtime versions (e.g., multiple JDK versions).
- Parameters:
build_command_info (GenericBuildCommandInfo) – The object containing language version information.
- Returns:
The selected language version as a string, or None if not available.
- Return type:
str | None
- macaron.build_spec_generator.common_spec.core.gen_generic_build_spec(purl, session)
Generate and return the Buildspec file.
- Parameters:
purl (PackageURL) – The PackageURL to generate build spec for.
session (sqlalchemy.orm.Session) – The SQLAlchemy Session opened for the database to extract build information.
- Returns:
The generated build spec.
- Return type:
- Raises:
GenerateBuildSpecError – Raised if generation of the build spec fails due to any of the following reasons: 1. The input PURL is invalid. 2. There is no supported build tool for this PURL. 3. Failed to patch the build commands using the provided
patches. 4. The database fromsessiondoesn’t contain enough information.
macaron.build_spec_generator.common_spec.jdk_finder module
This module includes the functions for obtaining the JDK version from a Java artifact.
- class macaron.build_spec_generator.common_spec.jdk_finder.JavaArtifactExt(value)
-
The extensions for Java artifacts.
- JAR = '.jar'
- class macaron.build_spec_generator.common_spec.jdk_finder.CacheStrategy(value)
Bases:
EnumThe strategy for caching the downloaded artifacts for JDK version finding.
- DISABLE = 0
- MAVEN_LAYOUT = 1
- macaron.build_spec_generator.common_spec.jdk_finder.download_file(url, dest)
Stream a file into a local destination.
- Parameters:
- Raises:
InvalidHTTPResponseError – If an error happens while streaming the file.
OSError – If the parent directory of
destdoesn’t exist.
- Return type:
- macaron.build_spec_generator.common_spec.jdk_finder.join_remote_maven_repo_url(remote_maven_url, maven_repo_path)
Join the base remote maven URL with a maven repository path.
- Parameters:
remote_maven_url (str) – The url to a remove maven layout repository. For example: https://repo1.maven.org/maven2
maven_repo_path (str) – The maven repository path for a GAV coordinate or an artifact from the root of the remote maven layout repository.
- Returns:
The joined path.
- Return type:
Examples
>>> remote_maven_repo = "https://repo1.maven.org/maven2" >>> artifact_path = "io/liftwizard/liftwizard-checkstyle/2.1.22/liftwizard-checkstyle-2.1.22.jar" >>> join_remote_maven_repo_url(remote_maven_repo, artifact_path) 'https://repo1.maven.org/maven2/io/liftwizard/liftwizard-checkstyle/2.1.22/liftwizard-checkstyle-2.1.22.jar' >>> join_remote_maven_repo_url(remote_maven_repo, "io/liftwizard/liftwizard-checkstyle/2.1.22/") 'https://repo1.maven.org/maven2/io/liftwizard/liftwizard-checkstyle/2.1.22/' >>> join_remote_maven_repo_url(f"{remote_maven_repo}/", artifact_path) 'https://repo1.maven.org/maven2/io/liftwizard/liftwizard-checkstyle/2.1.22/liftwizard-checkstyle-2.1.22.jar'
- macaron.build_spec_generator.common_spec.jdk_finder.get_jdk_version_from_jar(artifact_path)
Return the JDK version obtained from a Java artifact.
- macaron.build_spec_generator.common_spec.jdk_finder.find_jdk_version_from_remote_maven_repo_standalone(group_id, artifact_id, version, asset_name, remote_maven_repo_url)
Return the JDK version string from an artifact matching a given GAV from a remote maven layout repository.
This function doesn’t cache the downloaded artifact, and removes it after the function exits. We assume that the remote maven layout repository supports downloading a file through a HTTPS URL.
- Parameters:
group_id (str) – The group ID part of the GAV coordinate.
artifact_id (str) – The artifact ID part of the GAV coordinate.
version (str) – The version part of the GAV coordinate.
asset_name (str) – The name of artifact to download and extract the jdk version.
remote_maven_repo_url (str) – The URL to the remote maven layout repository.
- Returns:
The version string extracted from the artifact (as is); or None if there is an error, or if we couldn’t find any jdk version.
- Return type:
str | None
- macaron.build_spec_generator.common_spec.jdk_finder.find_jdk_version_from_remote_maven_repo_cache(group_id, artifact_id, version, asset_name, remote_maven_repo_url, local_cache_repo)
Return the JDK version string from an artifact matching a given GAV from a remote maven layout repository.
This function caches the downloaded artifact in a maven layout https://maven.apache.org/repository/layout.html under
local_cache_repo. We assume that the remote maven layout repository supports downloading a file through a HTTPS URL.- Parameters:
group_id (str) – The group ID part of the GAV coordinate.
artifact_id (str) – The artifact ID part of the GAV coordinate.
version (str) – The version part of the GAV coordinate.
asset_name (str) – The name of artifact to download and extract the jdk version.
remote_maven_repo_url (str) – The URL to the remote maven layout repository.
local_cache_repo (str) – The path to a local directory for caching the downloaded artifact used in JDK version extraction.
- Returns:
The version string extracted from the artifact (as is); or None if there is an error, or if we couldn’t find any jdk version.
- Return type:
str | None
- macaron.build_spec_generator.common_spec.jdk_finder.find_jdk_version_from_central_maven_repo(group_id, artifact_id, version, cache_strat=CacheStrategy.MAVEN_LAYOUT)
Return the JDK version string from an artifact matching a given GAV from Maven Central repository.
The artifacts will be downloaded from https://repo1.maven.org/maven2/ for JDK version extraction.
We now only support JAR files.
- Parameters:
group_id (str) – The group ID part of the GAV coordinate.
artifact_id (str) – The artifact ID part of the GAV coordinate.
version (str) – The version part of the GAV coordinate.
cache_strat (CacheStrategy) – Specify how artifacts from maven central are persisted.
- Returns:
The version string extract from the artifact (as is); or None if there is an error, or if we couldn’t find any jdk version.
- Return type:
str | None
macaron.build_spec_generator.common_spec.jdk_version_normalizer module
This module contains the logic to normalize a JDK version string as a major version number.
- macaron.build_spec_generator.common_spec.jdk_version_normalizer.normalize_jdk_version(jdk_version_str)
Return the major JDK version number.
We assume that the JDK version string is already valid (e.g Using a JDK version that is available in the real world).
For 1.x versions, we return the major version as
x.- Parameters:
jdk_version_str (str) – The JDK version string.
- Returns:
The major JDK version number as a string, or None if there is an error.
- Return type:
str | None
Examples
>>> normalize_jdk_version("19") '19' >>> normalize_jdk_version("19-ea") '19' >>> normalize_jdk_version("11.0.1") '11' >>> normalize_jdk_version("1.8") '8' >>> normalize_jdk_version("25.0.1")
macaron.build_spec_generator.common_spec.maven_spec module
This module includes build specification and helper classes for Maven packages.
- class macaron.build_spec_generator.common_spec.maven_spec.MavenBuildSpec(data)
Bases:
BaseBuildSpecThis class implements build spec inferences for Maven packages.
- __init__(data)
Initialize the object.
- Parameters:
data (BaseBuildSpecDict) – The data object containing the build configuration fields.
- get_default_build_commands(build_tool_names)
Return the default build commands for the build tools.
macaron.build_spec_generator.common_spec.pypi_spec module
This module includes build specification and helper classes for PyPI packages.
- class macaron.build_spec_generator.common_spec.pypi_spec.PyPIBuildSpec(data)
Bases:
BaseBuildSpecThis class implements build spec inferences for PyPI packages.
- __init__(data)
Initialize the object.
- Parameters:
data (BaseBuildSpecDict) – The data object containing the build configuration fields.
- get_default_build_commands(build_tool_names)
Return the default build commands for the build tools.
- resolve_fields(purl)
Resolve PyPI-specific fields in the build specification.
- read_directory(wheel_path, purl)
Read in the WHEEL and METADATA file from the .dist_info directory.
- Parameters:
wheel_path (str) – Path to the temporary directory where the wheel was downloaded into.
purl (PackageURL) – PURL corresponding to the package being analyzed.
- Returns:
Tuple where the first element is a string of the .dist-info/WHEEL contents and the second element is a string of the .dist-info/METADATA contents
- Return type:
- read_generator_line(wheel_contents)
Parse through the “Generator: {build backend} {version}” line of .dist_info/WHEEL.