From 442059f6c835f7961d272bb1ac6d4cb745c7fcfd Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Fri, 24 Oct 2025 10:14:41 +0200 Subject: implement coccinelle processor --- patchtree/cli.py | 1 - patchtree/patch.py | 7 +++++-- patchtree/process.py | 36 +++++++++++++++++++++++++++++++----- 3 files changed, 36 insertions(+), 8 deletions(-) diff --git a/patchtree/cli.py b/patchtree/cli.py index 104564d..f4c0dd1 100644 --- a/patchtree/cli.py +++ b/patchtree/cli.py @@ -36,7 +36,6 @@ def parse_arguments(config: Config) -> Context: def main(): config = load_config() - print(config) context = parse_arguments(config) diff --git a/patchtree/patch.py b/patchtree/patch.py index 8e07f59..86df566 100644 --- a/patchtree/patch.py +++ b/patchtree/patch.py @@ -31,6 +31,7 @@ class Patch: self.processors = self.file_name[idx:].split(config.process_delimiter) self.processors = [template.strip() for template in self.processors] self.processors = [template for template in self.processors if len(template) > 0] + self.processors.reverse() self.file_name = self.file_name[:idx] # save the path to the target file @@ -60,13 +61,15 @@ class Patch: diff = diff_class(self.file) # read file A contents - diff.content_a = context.get_content(self.file) + content_a = context.get_content(self.file) # read file B contents content_b = self.patch.read_text() for processor_class in processor_classes: processor = processor_class(context) - content_b = processor.transform(content_b) + content_b = processor.transform(content_a, content_b) + + diff.content_a = content_a diff.content_b = content_b delta = diff.diff() diff --git a/patchtree/process.py b/patchtree/process.py index 2846d28..7170c18 100644 --- a/patchtree/process.py +++ b/patchtree/process.py @@ -1,7 +1,10 @@ from __future__ import annotations from typing import TYPE_CHECKING, Any +from tempfile import mkstemp from jinja2 import Environment +from subprocess import Popen +from pathlib import Path if TYPE_CHECKING: from .context import Context @@ -12,8 +15,8 @@ class Process: def __init__(self, context: Context): self.context = context - def transform(self, input: str) -> str: - return input + def transform(self, content_a: str | None, content_b: str) -> str: + return content_b class ProcessJinja2(Process): environment: Environment = Environment( @@ -21,13 +24,36 @@ class ProcessJinja2(Process): lstrip_blocks=True, ) - def transform(self, input: str) -> str: + def transform(self, content_a, content_b) -> str: template_vars = self.get_template_vars() - return self.environment.from_string(input).render(**template_vars) + return self.environment.from_string(content_b).render(**template_vars) def get_template_vars(self) -> dict[str, Any]: return {} class ProcessCoccinelle(Process): - pass + def transform(self, content_a, content_b) -> str: + content_a = content_a or "" + + temp_a = Path(mkstemp()[1]) + temp_b = Path(mkstemp()[1]) + temp_sp = Path(mkstemp()[1]) + + temp_a.write_text(content_a) + temp_sp.write_text(content_b) + cmd = ( + "spatch", "--very-quiet", "--no-show-diff", + "--sp-file", str(temp_sp), + str(temp_a), "-o", str(temp_b), + ) + coccinelle = Popen(cmd) + coccinelle.wait() + + content_b = temp_b.read_text() + + temp_a.unlink() + temp_b.unlink() + temp_sp.unlink() + + return content_b -- cgit v1.2.3