Skip to content

Commit

Permalink
Package: add initial base class w/ abstract factory
Browse files Browse the repository at this point in the history
  • Loading branch information
fvalette-ledger committed Oct 9, 2024
1 parent f447e24 commit 88adf65
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 6 deletions.
10 changes: 6 additions & 4 deletions src/outpost/barbican/barbican.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
from .console import console
from .logger import logger, log_config
from . import config
from .package import Package
from .package import Package, create_package
from .package.meson import Meson

from .buildsys import ninja_backend
from .utils import pathhelper

Expand Down Expand Up @@ -56,13 +58,13 @@ def __init__(self, project_dir: pathlib.Path) -> None:

# Instantiate Sentry kernel
self._packages.append(
Package(
Meson(
"kernel", self, self._toml["kernel"], Package.Type.Kernel # type: ignore[arg-type]
)
)
# Instantiate libshield
self._packages.append(
Package(
Meson(
"runtime",
self,
self._toml["runtime"],
Expand All @@ -74,7 +76,7 @@ def __init__(self, project_dir: pathlib.Path) -> None:
self._noapp = False
for app, node in self._toml["application"].items():
self._packages.append(
Package(app, self, node, Package.Type.Application) # type: ignore[arg-type]
create_package(app, self, node, Package.Type.Application) # type: ignore[arg-type]
)
else:
self._noapp = True
Expand Down
4 changes: 2 additions & 2 deletions src/outpost/barbican/package/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
#
# SPDX-License-Identifier: Apache-2.0

from .package import Package
from .package import Package, create_package

__all__ = ["Package"]
__all__ = ["Package", "create_package"]
10 changes: 10 additions & 0 deletions src/outpost/barbican/package/meson.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# SPDX-FileCopyrightText: 2024 Ledger SAS
#
# SPDX-License-Identifier: Apache-2.0

from .package import Package


class Meson(Package):
def __init__(self, name: str, parent_project, config_node: dict, type):
super().__init__(name, parent_project, config_node, type)
46 changes: 46 additions & 0 deletions src/outpost/barbican/package/package.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
#
# SPDX-License-Identifier: Apache-2.0

from abc import ABC, abstractmethod
import collections.abc
from pathlib import Path
import subprocess
from functools import lru_cache
Expand All @@ -16,7 +18,36 @@
from ..barbican import Project


@unique
class Backend(StrEnum):
Cargo = auto()
Meson = auto()


class BackendFactoryMap(collections.abc.Mapping[Backend, collections.abc.Callable]):
def __init__(self) -> None:
self._key_type = Backend

def __len__(self):
return len(self._key_type)

def __iter__(self):
yield from [k.value for k in list(self._key_type)]

def __getitem__(self, key):
method = self._key_type(key)

from importlib import import_module
import sys

return getattr(
import_module("." + method.value, sys.modules[__name__].__package__), method.name
)


class Package:
__backend_factories: T.ClassVar[BackendFactoryMap] = BackendFactoryMap()

@unique
class Type(StrEnum):
"""Package type enumerate."""
Expand Down Expand Up @@ -90,6 +121,10 @@ def is_runtime(self) -> bool:
def is_sys_package(self) -> bool:
return self.is_kernel or self.is_runtime

@property
def backend(self) -> Backend:
return Backend(self.__class__.__name__.lower())

@property
@lru_cache
def src_dir(self) -> Path:
Expand Down Expand Up @@ -176,6 +211,10 @@ def build_opts(self):
build_opts.append(self._config["build_opts"] if "build_opts" in self._config else list())
return build_opts

@classmethod
def get_backend_factory(cls, backend: str) -> T.Type["Package"]:
return cls.__backend_factories[Backend(backend)]

def download(self) -> None:
self._scm.download()

Expand All @@ -193,3 +232,10 @@ def post_download_hook(self):
def post_update_hook(self):
subprocess.run(["meson", "subprojects", "download"], capture_output=True)
subprocess.run(["meson", "subprojects", "update"], capture_output=True)


def create_package(
name: str, parent_project: "Project", config_node: dict, type: Package.Type
) -> Package:
PackageCls = Package.get_backend_factory(config_node["build"]["backend"])
return PackageCls(name, parent_project, config_node, type)

0 comments on commit 88adf65

Please sign in to comment.