-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
kraken-build/: feature: Add
kraken.build
module which allows import…
…ing the current `context` and `project` from a build script as if calling `Context.current()` or `Project.current()`
- Loading branch information
1 parent
8d0b978
commit a33acfe
Showing
6 changed files
with
81 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
[[entries]] | ||
id = "f0b6da17-e82e-451b-b919-81529b97cd30" | ||
type = "feature" | ||
description = "Add `kraken.build` module which allows importing the current `context` and `project` from a build script as if calling `Context.current()` or `Project.current()`" | ||
author = "@NiklasRosenstein" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
from kraken.core import Context, Project | ||
from .utils.import_helper import _KrakenBuildModuleWrapper | ||
|
||
# Install a wrapper around the module object to allow build-scripts to always import the current (i.e. their own) | ||
# project and the Kraken build context. | ||
context: Context | ||
project: Project | ||
|
||
_KrakenBuildModuleWrapper.install(__name__) | ||
|
||
__all__ = ["context", "project"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import pytest | ||
from kraken.core import Context, Project | ||
from pathlib import Path | ||
|
||
|
||
def test_import_current_context_and_project_from_kraken_build() -> None: | ||
""" Test that you can import the current Kraken build context and project from `kraken.build`. """ | ||
|
||
with pytest.raises(RuntimeError): | ||
from kraken.build import context | ||
|
||
with pytest.raises(RuntimeError): | ||
from kraken.build import project | ||
|
||
with Context(Path.cwd()).as_current() as ctx: | ||
with Project("test", Path.cwd(), None, ctx).as_current() as proj: | ||
from kraken.build import context, project # noqa: F811 | ||
|
||
assert context is ctx | ||
assert project is proj | ||
|
||
with Project("subproject", Path.cwd() / "subproject", None, ctx).as_current() as subproj: | ||
from kraken.build import context, project # noqa: F811 | ||
|
||
assert context is ctx | ||
assert project is subproj | ||
|
||
from kraken.build import context | ||
|
||
assert context is ctx | ||
|
||
with pytest.raises(RuntimeError): | ||
from kraken.build import project | ||
|
||
with pytest.raises(RuntimeError): | ||
from kraken.build import context |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import sys | ||
import types | ||
from typing import Any | ||
from kraken.core import Context, Project | ||
|
||
|
||
class _KrakenBuildModuleWrapper: | ||
""" A wrapper for the `kraken.build` module to allow build-scripts to always import the current (i.e. their own) | ||
project and the Kraken build context. """ | ||
|
||
def __init__(self, module: types.ModuleType) -> None: | ||
self.__module = module | ||
|
||
def __getattr__(self, name: str) -> Any: | ||
if name == "context": | ||
return Context.current() | ||
elif name == "project": | ||
return Project.current() | ||
else: | ||
return getattr(self.__module, name) | ||
|
||
@staticmethod | ||
def install(module_name: str) -> None: | ||
""" Install the wrapper around the given module. """ | ||
sys.modules[module_name] = _KrakenBuildModuleWrapper(sys.modules[module_name]) # type: ignore[assignment] |