From ff2ab7db9a6414ea8047daef110cc8cd39e5ea61 Mon Sep 17 00:00:00 2001 From: DaanDeMeyer Date: Tue, 1 Jul 2025 22:03:00 +0200 Subject: [PATCH] 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. --- mkosi/resources/__init__.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) 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 -- 2.47.3