Skip to content

Commit

Permalink
bundler: Adding allow_binary flag to input model
Browse files Browse the repository at this point in the history
Some gems have hard dependencies on binary-only gems
(at the moment of writing sorbet was one such gem).
This commit introduces a way to allow downloading binary
gems by adding an allow_binary flag along the lines
of pip's allow_binary flag.

Signed-off-by: Alexey Ovchinnikov <[email protected]>
  • Loading branch information
a-ovchinnikov committed Oct 18, 2024
1 parent 98e82df commit 3629152
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 1 deletion.
1 change: 1 addition & 0 deletions cachi2/core/models/input.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ class BundlerPackageInput(_PackageInputBase):
"""Accepted input for a bundler package."""

type: Literal["bundler"]
allow_binary: bool = False


class GenericPackageInput(_PackageInputBase):
Expand Down
8 changes: 8 additions & 0 deletions cachi2/core/models/property_semantics.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class PropertySet:
npm_bundled: bool = False
npm_development: bool = False
pip_package_binary: bool = False
bundler_package_binary: bool = False

@classmethod
def from_properties(cls, props: Iterable[Property]) -> "Self":
Expand All @@ -42,6 +43,7 @@ def from_properties(cls, props: Iterable[Property]) -> "Self":
npm_bundled = False
npm_development = False
pip_package_binary = False
bundler_package_binary = False

for prop in props:
if prop.name == "cachi2:found_by":
Expand All @@ -54,6 +56,8 @@ def from_properties(cls, props: Iterable[Property]) -> "Self":
npm_development = True
elif prop.name == "cachi2:pip:package:binary":
pip_package_binary = True
elif prop.name == "cachi2:bundler:package:binary":
bundler_package_binary = True
else:
assert_never(prop.name)

Expand All @@ -63,6 +67,7 @@ def from_properties(cls, props: Iterable[Property]) -> "Self":
npm_bundled,
npm_development,
pip_package_binary,
bundler_package_binary,
)

def to_properties(self) -> list[Property]:
Expand All @@ -80,6 +85,8 @@ def to_properties(self) -> list[Property]:
props.append(Property(name="cdx:npm:package:development", value="true"))
if self.pip_package_binary:
props.append(Property(name="cachi2:pip:package:binary", value="true"))
if self.bundler_package_binary:
props.append(Property(name="cachi2:bundler:package:binary", value="true"))

return sorted(props, key=lambda p: (p.name, p.value))

Expand All @@ -92,4 +99,5 @@ def merge(self, other: "Self") -> "Self":
npm_bundled=self.npm_bundled and other.npm_bundled,
npm_development=self.npm_development and other.npm_development,
pip_package_binary=self.pip_package_binary or other.pip_package_binary,
bundler_package_binary=self.bundler_package_binary or other.bundler_package_binary,
)
1 change: 1 addition & 0 deletions cachi2/core/models/sbom.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from cachi2.core.models.validators import unique_sorted

PropertyName = Literal[
"cachi2:bundler:package:binary",
"cachi2:found_by",
"cachi2:missing_hash:in_file",
"cachi2:pip:package:binary",
Expand Down
11 changes: 10 additions & 1 deletion cachi2/core/package_managers/bundler/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,14 @@
from cachi2.core.errors import PackageRejected, UnsupportedFeature
from cachi2.core.models.input import Request
from cachi2.core.models.output import EnvironmentVariable, ProjectFile, RequestOutput
from cachi2.core.models.property_semantics import PropertySet
from cachi2.core.models.sbom import Component
from cachi2.core.package_managers.bundler.parser import ParseResult, PathDependency, parse_lockfile
from cachi2.core.package_managers.bundler.parser import (
GemPlatformSpecificDependency,
ParseResult,
PathDependency,
parse_lockfile,
)
from cachi2.core.rooted_path import RootedPath
from cachi2.core.scm import get_repo_id

Expand All @@ -33,6 +39,7 @@ def fetch_bundler_source(request: Request) -> RequestOutput:
_resolve_bundler_package(
package_dir=path_within_root,
output_dir=request.output_dir,
allow_binary=package.allow_binary,
)
)
project_files.append(_prepare_for_hermetic_build(request.source_dir, request.output_dir))
Expand Down Expand Up @@ -68,6 +75,8 @@ def _resolve_bundler_package(
for dep in dependencies:
dep.download_to(deps_dir)
c = Component(name=dep.name, version=dep.version, purl=dep.purl)
if isinstance(dep, GemPlatformSpecificDependency):
c.properties = PropertySet(bundler_package_binary=True).to_properties()
components.append(c)

return components
Expand Down

0 comments on commit 3629152

Please sign in to comment.