import typing from lib import arch from . import pkgcomposer, sources _key_global = "@global" _key_version = "version" _key_description = "description" _key_on_pack = "on_pack" _key_arch_any = "arch_any" _key_links = "links" _key_dependencies = "dependencies" _key_provides = "provides" _current_pkgname: str = _key_global _pkginfo: dict[str, dict[str, typing.Any]] = { _key_global: { _key_links: [], _key_dependencies: [], _key_provides: [], }, } _supported_arch: typing.Callable[[str], bool] = lambda x: True _on_build: typing.Callable[[], None] | None = None _blocked_hooks: list[str] = [] def package(s: str): global _current_pkgname _current_pkgname = s _pkginfo[s] = _pkginfo[_key_global].copy() _pkginfo[s][_key_links] = _pkginfo[s][_key_links].copy() _pkginfo[s][_key_dependencies] = _pkginfo[s][_key_dependencies].copy() _pkginfo[s][_key_provides] = _pkginfo[s][_key_provides].copy() def version(s: str): _pkginfo[_current_pkgname][_key_version] = s def description(s: str): _pkginfo[_current_pkgname][_key_description] = s def arch_any(): _pkginfo[_current_pkgname][_key_arch_any] = True def provide(s: str): _pkginfo[_current_pkgname][_key_provides] += [ s.replace("@same-arch", f"@{arch.get_target()}") ] def dep(s: str): _pkginfo[_current_pkgname][_key_dependencies] += [ s.replace("(same)", "(same-version) @same-arch") .replace("(same-version)", f"(={_pkginfo[_current_pkgname][_key_version]})") .replace("@same-arch", f"@{arch.get_target()}") ] def link(src: str, dst: str, default: bool = True): _pkginfo[_current_pkgname][_key_links] += [ { "from": src, "to": dst, "default": default, } ] def get_prefix(pkgname: str) -> str: pkginfo = _pkginfo[pkgname] if pkginfo.get(_key_arch_any): pkgarch = "any" else: pkgarch = arch.get_target() return f"/pkg/{pkgname}={pkginfo[_key_version]}@{pkgarch}" def supported_arch(s: list[str] | typing.Callable[[str], bool]): global _supported_arch if isinstance(s, list): _supported_arch = lambda arch: arch in s else: _supported_arch = s def on_build(func: typing.Callable[[], None]): global _on_build _on_build = func def on_pack(func: typing.Callable[[pkgcomposer.PkgComposer], None]): _pkginfo[_current_pkgname][_key_on_pack] = func def block_hook(hook: str): _blocked_hooks.append(hook) def source_url(url: str): sources.source_url(url) def source_git(url: str, branch: str | None = None): sources.source_git(url, branch)