diff options
| -rw-r--r-- | patchtree/config.py | 1 | ||||
| -rw-r--r-- | patchtree/process.py | 24 |
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 |