aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLoek Le Blansch <loek@pipeframe.xyz>2025-10-25 17:02:25 +0200
committerLoek Le Blansch <loek@pipeframe.xyz>2025-10-25 17:02:25 +0200
commita90ca9caf3b3a5f20f405512cd2d0875b1d723ae (patch)
treedab79ba80b1fca7c5c92e0783df6a1bd767d4ab4
parent26cb18a5caba723e75d2728cf731a6066a4d1c87 (diff)
add #exec processor
-rw-r--r--patchtree/config.py1
-rw-r--r--patchtree/process.py24
2 files changed, 24 insertions, 1 deletions
diff --git a/patchtree/config.py b/patchtree/config.py
index 79e82dc..555494e 100644
--- a/patchtree/config.py
+++ b/patchtree/config.py
@@ -12,6 +12,7 @@ DEFAULT_PROCESSORS: dict[str, type[Process]] = {
"cocci": ProcessCoccinelle,
"smpl": ProcessCoccinelle,
"touch": ProcessTouch,
+ "exec": ProcessExec,
}
DEFAULT_DIFFS: dict[str, type[Diff]] = {
diff --git a/patchtree/process.py b/patchtree/process.py
index 3ac47b1..e4d0162 100644
--- a/patchtree/process.py
+++ b/patchtree/process.py
@@ -3,8 +3,9 @@ from typing import TYPE_CHECKING, Any
from tempfile import mkstemp
from jinja2 import Environment
-from subprocess import Popen
+from subprocess import Popen, run
from pathlib import Path
+from shlex import split as shell_split
from .diff import DiffFile
@@ -77,3 +78,24 @@ class ProcessCoccinelle(Process):
class ProcessTouch(Process):
def transform(self, a, b):
return DiffFile(content=a.content, mode=b.mode)
+
+
+class ProcessExec(Process):
+ def transform(self, a, b):
+ assert b.content is not None
+
+ exec = Path(mkstemp()[1])
+ exec.write_text(b.content)
+
+ cmd = [str(exec)]
+
+ if b.content.startswith("#!"):
+ shebang = b.content.split("\n", 1)[0][2:]
+ cmd = [*shell_split(shebang), *cmd]
+
+ proc = run(cmd, text=True, input=a.content, capture_output=True)
+ b.content = proc.stdout
+
+ exec.unlink()
+
+ return b