diff --git a/hooks/__init__.py b/hooks/__init__.py index df10de0..ef7a4d4 100644 --- a/hooks/__init__.py +++ b/hooks/__init__.py @@ -1,9 +1,14 @@ from typing import Callable, TypeAlias -from . import autostrip +from . import autopatch, autostrip +PreBuild: TypeAlias = Callable[[], None] PostBuild: TypeAlias = Callable[[], None] +pre_build: dict[str, PreBuild] = { + "autopatch": autopatch.autopatch, +} + post_build: dict[str, PostBuild] = { "autostrip": autostrip.autostrip, } diff --git a/hooks/autopatch.py b/hooks/autopatch.py new file mode 100644 index 0000000..672985a --- /dev/null +++ b/hooks/autopatch.py @@ -0,0 +1,29 @@ +import os +import subprocess + +import lib.sources as sources + + +def autopatch(): + if not os.path.exists("patches"): + return + + target_dir = os.getcwd() + "/target" + source_dir = sources.sources_dir_name() + patches_tag_file = f"{target_dir}/patches.tag" + + patches_dir = os.getcwd() + "/patches" + patches = os.listdir(patches_dir) + patches.sort() + + os.chdir(source_dir) + if not os.path.exists(patches_tag_file): + open(patches_tag_file, "+w").close() + for i in patches: + with open(patches_tag_file, "r") as file: + if i + "\n" in file.readlines(): + continue + with open(f"{patches_dir}/{i}", "r") as file: + subprocess.run(["patch", "-p1"], stdin=file) + with open(patches_tag_file, "+a") as file: + file.write(i + "\n") diff --git a/lib/cpp.py b/lib/cpp.py index 73f9c0f..d1ecd63 100644 --- a/lib/cpp.py +++ b/lib/cpp.py @@ -21,7 +21,12 @@ def make_install(rem: list[str] | None = None, chdir: bool = True): def _do_auto_pack(pc: pkgcomposer.PkgComposer, srcdir: str, dstdir: str, filter): - pc.makedir(dstdir) + if not os.path.exists(srcdir): + return + try: + pc.makedir(dstdir) + except Exception: + pass with os.scandir(srcdir) as entries: for ent in entries: if filter(ent): diff --git a/x.py b/x.py index 1013ade..bda3e26 100644 --- a/x.py +++ b/x.py @@ -46,6 +46,13 @@ def run_build_package(package: str): print(f"Architecture {lib.arch.get_target()} is not supported for this package") exit(1) + # Run `pre_build` hooks + for n in hooks.pre_build.keys(): + if n in lib.make._blocked_hooks: + continue + hooks.pre_build[n]() + os.chdir(package_dir) + # Build the package if lib.make._on_build: os.chdir(lib.sources.sources_dir_name()) @@ -54,10 +61,12 @@ def run_build_package(package: str): print("on_build(): Not defined") # Run `post_build` hooks + post_build_dir = os.getcwd() for n in hooks.post_build.keys(): if n in lib.make._blocked_hooks: continue hooks.post_build[n]() + os.chdir(post_build_dir) # Pack packages for pkgname in lib.make._pkginfo.keys():