kraken.common
kraken.common
AsciiTable
A very simple ASCII table formatter.
Supports correctly calculating the width of cells even if they are already ANSI formatted.
Source code in kraken/common/_asciitable.py
BuildscriptMetadata
dataclass
Metadata for a Kraken build and its runtime environment.
Source code in kraken/common/_buildscript.py
callback
staticmethod
callback(
func: Callable[[BuildscriptMetadata], Any]
) -> Iterator[None]
A context manager that will ensure calling the given func after :func:buildscript
is run.
This is used to retrieve and react upon the metadata in the Kraken build system.
Source code in kraken/common/_buildscript.py
capture
staticmethod
capture() -> Iterator[Future[BuildscriptMetadata]]
A context manager that will ensure calling :func:buildscript
will raise a
:class:BuildscriptMetadataException
and catch that exception to return the metadata.
This is used to retrieve the metadata in Kraken wrapper.
Source code in kraken/common/_buildscript.py
BuildscriptMetadataException
Bases: BaseException
This exception is raised by the :func:metadata
function.
Source code in kraken/common/_buildscript.py
ColorOptions
dataclass
Adds a --no-color
option to the argument parser. Use [init_color] to monkey-patch the [termcolor] module
to force color output unless the --no-color
option is set. This ensures we have colored output even in CI
environments by default.
Source code in kraken/common/_option_sets.py
CurrentDirectoryProjectFinder
Bases: ProjectFinder
Goes through a list of script finders and returns the first one matching.
Source code in kraken/common/_runner.py
default
classmethod
default() -> CurrentDirectoryProjectFinder
Returns the default instance that contains the known :class:ScriptRunner
implementations.
EnvironmentType
Bases: Enum
This enumeration describes the type of environment that is being used to run Kraken in.
Source code in kraken/common/_environment.py
GitAwareProjectFinder
Bases: ProjectFinder
Finds the root of a project by picking the highest-up build script that does not cross a Git repository boundary or a home directory boundary. Starts from a directory and works it's way up until a stop condition is encountered.
If any build script contains the string # ::krakenw-root
, then the directory containing that build script is
considered the root of the project. This is useful for projects that have multiple build scripts in different
directories, but they should not be considered part of the same project.
Source code in kraken/common/_runner.py
__init__
__init__(
delegate: ProjectFinder,
home_boundary: Path | None | NotSet = None,
) -> None
:param delegate: The project finder to delegate to in any of the directories that this class
looks through. The first project returned by this finder in any of the directories is used.
:param home_boundary: A directory which contains a boundary that should not be crossed when
searching for projects. For example, if this is /home/foo
, then this class will stop searching
for projects as soon as it reaches /home/foo
or any sibling directory (such as /home/bar
).
If a path does not live within the home boundary or any of its siblings, the boundary is not
taken into account.
When the parameter is set to :const:`NotSet.Value`, it will default to the user's home directory.
The value can be set to :const:`None` to disable the home boundary check all together.
Source code in kraken/common/_runner.py
default
classmethod
default() -> GitAwareProjectFinder
Returns the default instance that contains a default :class:CurrentDirectoryProjectFinder
.
LocalRequirement
dataclass
Bases: Requirement
Represents a requirement on a local project on the filesystem.
The string format of a local requirement is name@path
. The name
must match the distribution name.
Source code in kraken/common/_requirements.py
PipRequirement
dataclass
Bases: Requirement
Represents a Pip requriement.
Source code in kraken/common/_requirements.py
ProjectFinder
Bases: ABC
Base class for finding a Kraken project starting from any directory.
Source code in kraken/common/_runner.py
PythonScriptRunner
Bases: ScriptPicker
A finder and runner for Python based Kraken build scripts called .kraken.py
.
Note
We can't call the script kraken.py
(without the leading dot), as otherwise under most circumstances the
script will try to import itself when doing import kraken
or from kraken import ...
.
Source code in kraken/common/_runner.py
Requirement
Bases: ABC
Source code in kraken/common/_requirements.py
RequirementSpec
dataclass
Represents the requirements for a kraken build script.
Source code in kraken/common/_requirements.py
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 |
|
from_args
staticmethod
from_args(args: list[str]) -> RequirementSpec
Parses the arguments using :mod:argparse
as if they are Pip install arguments.
:raise ValueError: If an invalid argument is encountered.
Source code in kraken/common/_requirements.py
to_args
to_args(
base_dir: Path = Path("."),
with_options: bool = True,
with_requirements: bool = True,
) -> list[str]
Converts the requirements back to Pip install arguments.
:param base_dir: The base directory that relative :class:LocalRequirement
s should be considered relative to.
:param with_requirements: Can be set to False
to not return requirements in the argument, just the index URLs.
Source code in kraken/common/_requirements.py
to_hash
Hash the requirements spec to a hexdigest.
Source code in kraken/common/_requirements.py
with_pythonpath
with_pythonpath(path: Iterable[str]) -> RequirementSpec
Adds the given pythonpath and returns a new instance.
with_requirements
with_requirements(
reqs: Iterable[str | Requirement],
) -> RequirementSpec
Adds the given requirements and returns a new instance.
Source code in kraken/common/_requirements.py
ScriptPicker
Bases: ScriptRunner
Base class for picking the right script file in a directory based on a few criteria.
Source code in kraken/common/_runner.py
ScriptRunner
Bases: ABC
Abstract class for script runners. Implementations of this class are used to detect a script in a directory and to actually run it. The Kraken wrapper and build system both use this to run a build script, which for the wrapper is needed to extract the build script metadata.
Source code in kraken/common/_runner.py
get_buildscript_call_recommendation
abstractmethod
get_buildscript_call_recommendation(
metadata: BuildscriptMetadata,
) -> str
Make a recommendation to the user for the code the user should put into their build script for the
:func:buildscript
call that is required by Kraken wrapper.
Source code in kraken/common/_runner.py
has_buildscript_call
abstractmethod
Implement a heuristic to check if the script implements a call to the :func:buildscript
function.
Supplier
Bases: Generic[T]
, ABC
Base class for value suppliers.
Source code in kraken/common/supplier.py
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
|
Empty
Bases: Exception
Raised when a supplier cannot provide a value.
Source code in kraken/common/supplier.py
derived_from
abstractmethod
derived_from() -> Iterable[Supplier[Any]]
get
abstractmethod
Return the value of the supplier. Depending on the implemenmtation, this may defer to other suppliers.
get_or
Return the value of the supplier, or the fallback value if the supplier is empty.
get_or_raise
Return the value of the supplier, or raise the exception provided by get_exception if empty.
is_empty
is_filled
lineage
Iterates over all suppliers in the lineage.
Yields:
Type | Description |
---|---|
Iterable[tuple[Supplier[Any], list[Supplier[Any]]]]
|
A supplier and the suppliers it is derived from. |
Source code in kraken/common/supplier.py
map
map(func: Callable[[T], U]) -> Supplier[U]
once
once() -> Supplier[T]
TomlConfigFile
Bases: MutableMapping[str, Any]
A helper class that reads and writes a TOML configuration file.
Source code in kraken/common/_tomlconfig.py
lazy_str
Delegates to a function to convert to a string.
Source code in kraken/common/_text.py
appending_to_sys_path
A context manager to temporarily append to sys.path
.
Source code in kraken/common/_importlib.py
atomic_file_swap
atomic_file_swap(
path: str | Path,
mode: Literal["w", "wb"],
always_revert: bool = False,
create_dirs: bool = False,
) -> Iterator[IO[AnyStr]]
Performs an atomic write to a file while temporarily moving the original file to a different random location.
:param path: The path to replace. :param mode: The open mode for the file (text or binary). :param always_revert: If enabled, swap the old file back into place even if the with context has no errors. :param create_dirs: If the file does not exist, and neither do its parent directories, create the directories. The directory will be removed if the operation is reverted.
Source code in kraken/common/_fs.py
buildscript
buildscript(
*,
index_url: str | None = None,
extra_index_urls: Sequence[str] | None = None,
requirements: Sequence[str] | None = None,
additional_sys_paths: Sequence[str] | None = None,
interpreter_constraint: str | None = None
) -> BuildscriptMetadata
Use this function to the dependencies and additional install options for the build environment of your Kraken
build script that is installed and managed by Kraken-wrapper. This function must be called at the very beginning
of your .kraken.py
build script at the root of your project.
Example:
from kraken.common import buildscript
buildscript(
requirements=["kraken-build"],
)
from kraken.std import ...
You can depend on local dependencies and Python packages from URLs by prefixing them with <package name> @
:
buildscript(requirements=[
"kraken-build @ git+https://github.com/kraken-build/kraken.git@nr/python-project#subdirectory=kraken-build"
])
Parameters:
Name | Type | Description | Default |
---|---|---|---|
index_url |
str | None
|
The index URL for Python packages to install from. If this is a private package registry, the
credentials can be configured with the |
None
|
extra_index_urls |
Sequence[str] | None
|
Additional index URLs for Python packages to install from. |
None
|
requirements |
Sequence[str] | None
|
A list of Python package requirements to install. This usually contains at least |
None
|
additional_sys_paths |
Sequence[str] | None
|
Additional system paths to add to the Python environment. |
None
|
interpreter_constraint |
str | None
|
Constraints for the Python interpreter that the build must be run with. |
None
|
Source code in kraken/common/_buildscript.py
flatten
get_terminal_width
Returns the terminal width through :func:os.get_terminal_size
, falling back to the COLUMNS
environment
variable. If neither is available, return default.
Source code in kraken/common/_terminal.py
inline_text
A helper that dedents text and replaces a single newline with a single whitespace, yet double newlines are kept in place. To enforce a normal linebreak, add a backslash before a single newline.
Source code in kraken/common/_text.py
not_none
Raise a :class:RuntimeError
if v is None
, otherwise return v.
Source code in kraken/common/_generic.py
parse_requirement
parse_requirement(
value: str,
) -> PipRequirement | LocalRequirement | UrlRequirement
Parse a string as a requirement. Return a :class:PipRequirement
or :class:LocalRequirement
.
Source code in kraken/common/_requirements.py
pluralize
Very naive implementation to pluralize english words (simply appends an s).
propagate_argparse_formatter_to_subparser
Propagates the formatter on parser to all subparsers.
Source code in kraken/common/_argparse.py
safe_rmpath
Removes the specified path from the file system. If it is a directory, :func:shutil.rmtree
will be used
with ignore_errors
enabled.