diff options
| -rw-r--r-- | patchtree/__init__.py | 2 | ||||
| -rw-r--r-- | patchtree/cli.py | 4 | ||||
| -rw-r--r-- | patchtree/config.py | 26 | ||||
| -rw-r--r-- | patchtree/context.py | 6 | ||||
| -rw-r--r-- | patchtree/patch.py | 2 | ||||
| -rw-r--r-- | patchtree/process.py | 3 |
6 files changed, 40 insertions, 3 deletions
diff --git a/patchtree/__init__.py b/patchtree/__init__.py index e7f3568..f2cc78a 100644 --- a/patchtree/__init__.py +++ b/patchtree/__init__.py @@ -1,4 +1,4 @@ -from .config import Config +from .config import Config, Header from .diff import Diff, IgnoreDiff from .context import Context from .process import ProcessCoccinelle, ProcessJinja2 diff --git a/patchtree/cli.py b/patchtree/cli.py index f4c0dd1..8f3a48b 100644 --- a/patchtree/cli.py +++ b/patchtree/cli.py @@ -51,9 +51,11 @@ def main(): print("no files to patch!", file=stderr) return 0 + config.header(context) + for file in files: patch = config.patch(config, file) - patch.write_diff(context) + patch.write(context) context.output.flush() context.output.close() diff --git a/patchtree/config.py b/patchtree/config.py index 11f92f9..23849a9 100644 --- a/patchtree/config.py +++ b/patchtree/config.py @@ -1,5 +1,6 @@ from dataclasses import dataclass, field from argparse import ArgumentParser +from importlib import metadata from .context import Context from .patch import Patch @@ -16,6 +17,30 @@ DEFAULT_DIFFS: dict[str, type[Diff]] = { ".gitignore": IgnoreDiff, } +class Header: + context: Context + name = "patchtree" + license = None + + def __init__(self, context: Context): + self.context = context + + self.write_version() + self.write_version_extra() + self.write_license() + + def write_version(self): + version = metadata.version("patchtree") + self.context.output.write(f"{self.name} output (version {version})\n") + + def write_version_extra(self): + pass + + def write_license(self): + if self.license is None: + return + self.context.output.write(f"{self.license}\n") + @dataclass class Config: context: type[Context] = Context @@ -24,6 +49,7 @@ class Config: process_delimiter: str = "#" processors: dict[str, type[Process]] = field(default_factory=lambda: DEFAULT_PROCESSORS) diff_strategies: dict[str, type[Diff]] = field(default_factory=lambda: DEFAULT_DIFFS) + header: type[Header] = Header def __post_init__(self): self.processors = {**DEFAULT_PROCESSORS, **self.processors} diff --git a/patchtree/context.py b/patchtree/context.py index 914b3f1..207b8c2 100644 --- a/patchtree/context.py +++ b/patchtree/context.py @@ -22,6 +22,12 @@ class Context: if not path.exists(target): raise Exception(f"cannot open `{target}'") + if options.out is not None: + if options.out == "-": + self.output = stdout + else: + self.output = open(options.out, "w+") + if path.isdir(target): self.fs = DiskPath(target) elif is_zipfile(target): diff --git a/patchtree/patch.py b/patchtree/patch.py index 86df566..cddf313 100644 --- a/patchtree/patch.py +++ b/patchtree/patch.py @@ -54,7 +54,7 @@ class Patch: processors.append(self.config.processors[processor]) return processors - def write_diff(self, context: Context) -> None: + def write(self, context: Context) -> None: diff_class = self.get_diff() processor_classes = self.get_processors() diff --git a/patchtree/process.py b/patchtree/process.py index 7170c18..44082cd 100644 --- a/patchtree/process.py +++ b/patchtree/process.py @@ -35,6 +35,9 @@ class ProcessCoccinelle(Process): def transform(self, content_a, content_b) -> str: content_a = content_a or "" + if len(content_b.strip()) == 0: + return content_a + temp_a = Path(mkstemp()[1]) temp_b = Path(mkstemp()[1]) temp_sp = Path(mkstemp()[1]) |