Skip to content

Commit

Permalink
Format the remaining files
Browse files Browse the repository at this point in the history
  • Loading branch information
arichardson committed Feb 9, 2024
1 parent b64ed87 commit b92aece
Show file tree
Hide file tree
Showing 14 changed files with 1,694 additions and 775 deletions.
4 changes: 2 additions & 2 deletions pycheribuild/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def _update_check(config: DefaultCheriConfig, d: Path) -> None:
if query_yes_no(
config,
f"The local {branch_info.local_branch} branch is tracking the obsolete remote 'master'"
f" branch, would you like to switch to 'main'?",
" branch, would you like to switch to 'main'?",
force_result=False,
):
# Update the remote ref to point to "main".
Expand Down Expand Up @@ -140,7 +140,7 @@ def get_config_option_value(handle: ConfigOptionHandle, config: DefaultCheriConf
if option.is_fallback_only:
raise LookupError(
f"Option '{option.full_option_name}' cannot be queried since it is a generic fallback value"
f"for a target-specific option. Please use the target-suffixed on instead."
"for a target-specific option. Please use the target-suffixed on instead."
)
if option._owning_class is not None:
project_cls: "type[SimpleProject]" = option._owning_class
Expand Down
728 changes: 505 additions & 223 deletions pycheribuild/boot_cheribsd/__init__.py

Large diffs are not rendered by default.

609 changes: 398 additions & 211 deletions pycheribuild/config/chericonfig.py

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion pycheribuild/config/compilation_targets.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,8 @@ def triple_for_target(
config: "CheriConfig",
*,
include_version: bool,
) -> str: ...
) -> str:
...

def get_target_triple(self, *, include_version: bool) -> str:
return self.triple_for_target(self.target, self.config, include_version=include_version)
Expand Down
19 changes: 15 additions & 4 deletions pycheribuild/config/computed_default_value.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,26 @@
T = typing.TypeVar("T")
if typing.TYPE_CHECKING: # no-combine
from ..utils import ConfigBase # no-combine

ConfigTy = typing.TypeVar("ConfigTy", bound=ConfigBase) # no-combine


class ComputedDefaultValue(typing.Generic[T]):
def __init__(self, function: "Callable[[ConfigTy, Any], T]",
as_string: "Union[str, Callable[[Any], str]]",
as_readme_string: "Union[str, Callable[[Any], str], None]" = None,
inherit: "typing.Optional[ComputedDefaultValue[T]]" = None):
def __init__(
self,
function: "Callable[[ConfigTy, Any], T]",
as_string: "Union[str, Callable[[Any], str]]",
as_readme_string: "Union[str, Callable[[Any], str], None]" = None,
inherit: "typing.Optional[ComputedDefaultValue[T]]" = None,
):
if inherit is not None:

def inheriting_function(config, project):
val = function(config, project)
if val is None:
val = inherit.function(config, project)
return val

self.function = inheriting_function
else:
assert function is not None, "Must provide function or inherit"
Expand All @@ -56,8 +62,10 @@ def inheriting_function(config, project):
assert callable(as_string), "Inheriting only makes sense with callable as_string"

if not callable(inherit.as_string):

def inherit_as_string_wrapper(cls):
return inherit.as_string

inherited_as_string = inherit_as_string_wrapper
else:
inherited_as_string = inherit.as_string
Expand All @@ -79,11 +87,14 @@ def inheriting_as_string(cls):

def as_readme_string_none_wrapper(cls):
return None

as_readme_string = as_readme_string_none_wrapper

if not callable(inherit.as_readme_string):

def inherit_as_readme_string_wrapper(cls):
return inherit.as_readme_string

inherited_as_readme_string = inherit_as_readme_string_wrapper
else:
inherited_as_readme_string = inherit.as_readme_string
Expand Down
130 changes: 94 additions & 36 deletions pycheribuild/config/config_loader_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
from .computed_default_value import ComputedDefaultValue
from ..utils import ConfigBase, fatal_error, warning_message

T = typing.TypeVar('T')
T = typing.TypeVar("T")

if typing.TYPE_CHECKING:
import argparse
Expand Down Expand Up @@ -73,8 +73,9 @@ class ConfigLoaderBase(ABC):

# argparse groups used in the command line loader

def __init__(self, *, option_cls: "type[ConfigOptionBase]",
command_line_only_options_cls: "type[ConfigOptionBase]"):
def __init__(
self, *, option_cls: "type[ConfigOptionBase]", command_line_only_options_cls: "type[ConfigOptionBase]"
):
self.__option_cls: "type[ConfigOptionBase]" = option_cls
self.__command_line_only_options_cls: "type[ConfigOptionBase]" = command_line_only_options_cls
self.unknown_config_option_is_error = False
Expand All @@ -84,7 +85,8 @@ def __init__(self, *, option_cls: "type[ConfigOptionBase]",
self.dependencies_group = self.add_argument_group("Selecting which dependencies are built")
self.path_group = self.add_argument_group("Configuration of default paths")
self.cross_compile_options_group = self.add_argument_group(
"Adjust flags used when compiling MIPS/CHERI projects")
"Adjust flags used when compiling MIPS/CHERI projects"
)
self.tests_group = self.add_argument_group("Configuration for running tests")
self.benchmark_group = self.add_argument_group("Configuration for running benchmarks")
self.run_group = self.add_argument_group("Configuration for launching QEMU (and other simulators)")
Expand All @@ -101,15 +103,30 @@ def add_commandline_only_option(self, *args, type: "Callable[[str], T]" = str, *

def add_commandline_only_bool_option(self, *args, default=False, **kwargs) -> bool:
assert default is False or kwargs.get("negatable") is True
return self.add_option(*args, option_cls=self.__command_line_only_options_cls, default=default,
negatable=kwargs.pop("negatable", False), type=bool, **kwargs)
return self.add_option(
*args,
option_cls=self.__command_line_only_options_cls,
default=default,
negatable=kwargs.pop("negatable", False),
type=bool,
**kwargs,
)

# noinspection PyShadowingBuiltins
def add_option(self, name: str, shortname=None, *, type: "Union[type[T], Callable[[str], T]]" = str,
default: "Union[ComputedDefaultValue[T], Optional[T], Callable[[ConfigBase, typing.Any], T]]" = None,
_owning_class: "Optional[type]" = None, _fallback_names: "Optional[list[str]]" = None,
option_cls: "Optional[type[ConfigOptionBase[T]]]" = None, replaceable=False,
fallback_replaceable: "Optional[bool]" = None, **kwargs) -> T:
def add_option(
self,
name: str,
shortname=None,
*,
type: "Union[type[T], Callable[[str], T]]" = str,
default: "Union[ComputedDefaultValue[T], Optional[T], Callable[[ConfigBase, typing.Any], T]]" = None,
_owning_class: "Optional[type]" = None,
_fallback_names: "Optional[list[str]]" = None,
option_cls: "Optional[type[ConfigOptionBase[T]]]" = None,
replaceable=False,
fallback_replaceable: "Optional[bool]" = None,
**kwargs,
) -> T:
if option_cls is None:
option_cls = self.__option_cls
if fallback_replaceable is None:
Expand All @@ -124,10 +141,16 @@ def add_option(self, name: str, shortname=None, *, type: "Union[type[T], Callabl
fallback_handle = self.option_handles.get(fallback_name)
if fallback_handle is None or fallback_handle.replaceable:
# Do not assign an owning class or a default value to this implicitly added fallback option.
self.add_option(fallback_name, type=type, option_cls=option_cls, replaceable=fallback_replaceable,
is_fallback=True)
option = option_cls(name, shortname, default, type, _owning_class, _loader=self,
_fallback_names=_fallback_names, **kwargs)
self.add_option(
fallback_name,
type=type,
option_cls=option_cls,
replaceable=fallback_replaceable,
is_fallback=True,
)
option = option_cls(
name, shortname, default, type, _owning_class, _loader=self, _fallback_names=_fallback_names, **kwargs
)
if name in self.option_handles:
self.option_handles[name]._replace_option(option, replaceable)
else:
Expand All @@ -138,14 +161,20 @@ def add_bool_option(self, name: str, shortname=None, default=False, **kwargs) ->
# noinspection PyTypeChecker
return self.add_option(name, shortname, default=default, type=bool, **kwargs)

def add_path_option(self, name: str, *,
default: "Union[ComputedDefaultValue[Path], Path, Callable[[ConfigBase, typing.Any], Path]]",
shortname=None, **kwargs) -> Path:
def add_path_option(
self,
name: str,
*,
default: "Union[ComputedDefaultValue[Path], Path, Callable[[ConfigBase, typing.Any], Path]]",
shortname=None,
**kwargs,
) -> Path:
# we have to make sure we resolve this to an absolute path because otherwise steps where CWD is different fail!
return typing.cast(Path, self.add_option(name, shortname, type=Path, default=default, **kwargs))

def add_optional_path_option(self, name: str, *, default: "Optional[Path]" = None, shortname=None,
**kwargs) -> Path:
def add_optional_path_option(
self, name: str, *, default: "Optional[Path]" = None, shortname=None, **kwargs
) -> Path:
# we have to make sure we resolve this to an absolute path because otherwise steps where CWD is different fail!
return self.add_option(name, shortname, type=Path, default=default, **kwargs)

Expand Down Expand Up @@ -193,8 +222,9 @@ def targets(self) -> "list[str]":

class AbstractConfigOption(typing.Generic[T], metaclass=ABCMeta):
@abstractmethod
def load_option(self, config: "ConfigBase", instance: "Optional[object]", _: type,
return_none_if_default=False) -> T:
def load_option(
self, config: "ConfigBase", instance: "Optional[object]", _: type, return_none_if_default=False
) -> T:
...

@abstractmethod
Expand Down Expand Up @@ -229,10 +259,19 @@ def _convert_type(self, loaded_result: _LoadedConfigValue) -> "Optional[T]":


class ConfigOptionBase(AbstractConfigOption[T]):
def __init__(self, name: str, shortname: Optional[str], default,
value_type: "Union[type[T], Callable[[typing.Any], T]]", _owning_class=None, *,
_loader: "Optional[ConfigLoaderBase]" = None, _fallback_names: "Optional[list[str]]" = None,
_legacy_alias_names: "Optional[list[str]]" = None, is_fallback: bool = False):
def __init__(
self,
name: str,
shortname: Optional[str],
default,
value_type: "Union[type[T], Callable[[typing.Any], T]]",
_owning_class=None,
*,
_loader: "Optional[ConfigLoaderBase]" = None,
_fallback_names: "Optional[list[str]]" = None,
_legacy_alias_names: "Optional[list[str]]" = None,
is_fallback: bool = False,
):
self.name = name
self.shortname = shortname
self.default = default
Expand All @@ -250,8 +289,9 @@ def __init__(self, name: str, shortname: Optional[str], default,
self._is_default_value = False
self.is_fallback_only = is_fallback

def load_option(self, config: "ConfigBase", instance: "Optional[object]", _: type,
return_none_if_default=False) -> T:
def load_option(
self, config: "ConfigBase", instance: "Optional[object]", _: type, return_none_if_default=False
) -> T:
result = self._load_option_impl(config, self.full_option_name)
# fall back from --qtbase-mips/foo to --qtbase/foo
# Try aliases first:
Expand Down Expand Up @@ -283,8 +323,16 @@ def load_option(self, config: "ConfigBase", instance: "Optional[object]", _: typ
try:
result = self._convert_type(result)
except ValueError as e:
fatal_error("Invalid value for option '", self.full_option_name,
"': could not convert '", result, "': ", str(e), sep="", pretend=config.pretend)
fatal_error(
"Invalid value for option '",
self.full_option_name,
"': could not convert '",
result,
"': ",
str(e),
sep="",
pretend=config.pretend,
)
sys.exit()
return result

Expand All @@ -305,9 +353,10 @@ def is_default_value(self) -> bool:
return self._is_default_value

def __get__(self, instance, owner) -> T:
assert instance is not None or not callable(self.default), \
f"Tried to access read config option {self.full_option_name} without an object instance. " \
assert instance is not None or not callable(self.default), (
f"Tried to access read config option {self.full_option_name} without an object instance. "
f"Config options using computed defaults can only be used with an object instance. Owner = {owner}"
)

# TODO: would be nice if this was possible (but too much depends on accessing values without instances)
# if instance is None:
Expand Down Expand Up @@ -335,8 +384,16 @@ def _convert_type(self, loaded_result: _LoadedConfigValue) -> "Optional[T]":
if isinstance(self.value_type, type) and issubclass(self.value_type, collections.abc.Sequence):
string_value = result
result = shlex.split(string_value)
warning_message("Config option ", self.full_option_name, " (", string_value, ") should be a list, ",
"got a string instead -> assuming the correct value is ", result, sep="")
warning_message(
"Config option ",
self.full_option_name,
" (",
string_value,
") should be a list, ",
"got a string instead -> assuming the correct value is ",
result,
sep="",
)
if isinstance(self.value_type, type) and issubclass(self.value_type, Path):
expanded = os.path.expanduser(os.path.expandvars(str(result)))
while expanded.startswith("//"):
Expand Down Expand Up @@ -391,8 +448,9 @@ def _replace_option(self, option: "ConfigOptionBase[T]", replaceable: bool):
def replaceable(self) -> bool:
return self.__replaceable

def load_option(self, config: "ConfigBase", instance: "Optional[object]", _: type,
return_none_if_default=False) -> T:
def load_option(
self, config: "ConfigBase", instance: "Optional[object]", _: type, return_none_if_default=False
) -> T:
return self._get_option().load_option(config, instance, _, return_none_if_default)

def _load_option_impl(self, config: "ConfigBase", target_option_name) -> "Optional[_LoadedConfigValue]":
Expand Down
Loading

0 comments on commit b92aece

Please sign in to comment.