From: DaanDeMeyer Date: Tue, 1 Jul 2025 20:03:00 +0000 (+0200) Subject: resources: Make sure scripts are made executable in as_file() X-Git-Tag: v26~192^2~3 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=ff2ab7db9a6414ea8047daef110cc8cd39e5ea61;p=thirdparty%2Fmkosi.git resources: Make sure scripts are made executable in as_file() We don't have access to permissions from the Traversables so we check for a shebang in each file instead. --- diff --git a/mkosi/resources/__init__.py b/mkosi/resources/__init__.py index d4b9f9802..6d8d7238a 100644 --- a/mkosi/resources/__init__.py +++ b/mkosi/resources/__init__.py @@ -4,6 +4,7 @@ import contextlib import functools import os +import stat import sys import tempfile from collections.abc import Iterator @@ -16,6 +17,15 @@ else: from importlib.abc import Traversable +def make_executable_if_script(path: Path) -> Path: + with path.open("rb") as f: + buf = f.read(3) + if buf == b"#!/": + os.chmod(path, path.stat().st_mode | stat.S_IEXEC) + + return path + + @contextlib.contextmanager def temporary_file(path: Traversable, suffix: str = "") -> Iterator[Path]: fd, raw_path = tempfile.mkstemp(suffix=suffix) @@ -24,7 +34,7 @@ def temporary_file(path: Traversable, suffix: str = "") -> Iterator[Path]: os.write(fd, path.read_bytes()) finally: os.close(fd) - yield Path(raw_path) + yield make_executable_if_script(Path(raw_path)) finally: try: os.remove(raw_path) @@ -73,4 +83,5 @@ def write_contents(target: Path, source: Traversable) -> Path: write_contents(child, item) else: child.write_bytes(source.read_bytes()) + make_executable_if_script(child) return child