]> git.ipfire.org Git - thirdparty/mkosi.git/commitdiff
resources: Make sure scripts are made executable in as_file()
authorDaanDeMeyer <daan.j.demeyer@gmail.com>
Tue, 1 Jul 2025 20:03:00 +0000 (22:03 +0200)
committerDaanDeMeyer <daan.j.demeyer@gmail.com>
Tue, 1 Jul 2025 20:47:13 +0000 (22:47 +0200)
We don't have access to permissions from the Traversables so we check
for a shebang in each file instead.

mkosi/resources/__init__.py

index d4b9f980294957aca0c6f0b49f94a74127174ee3..6d8d7238a765a901e69dccf569ef622241e11e71 100644 (file)
@@ -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