From a4ff3d4eb5a2618c9a6e5d56435536ea4ebe8855 Mon Sep 17 00:00:00 2001 From: Anja Rabich Date: Tue, 5 Aug 2025 17:57:07 +0200 Subject: [PATCH] feat: WIP clean-up TODOs Co-authored-by: Benjamin Lipp --- marzipan/TODO.md | 2 +- marzipan/src/__init__.py | 29 +++++++++++++---------------- 2 files changed, 14 insertions(+), 17 deletions(-) diff --git a/marzipan/TODO.md b/marzipan/TODO.md index 691454f..6705ede 100644 --- a/marzipan/TODO.md +++ b/marzipan/TODO.md @@ -23,7 +23,7 @@ ## Next Steps -* rebase from main +* ~~rebase from main~~ * see if we still need the `extra_args is None` check in `_run_proverif` * set colors differently to prevent injection attack * by calling a function diff --git a/marzipan/src/__init__.py b/marzipan/src/__init__.py index 679df20..1b69056 100644 --- a/marzipan/src/__init__.py +++ b/marzipan/src/__init__.py @@ -1,5 +1,5 @@ from .util import pkgs, setup_exports, export, rename -from rich import print as rich_print +#from rich.console import Console import click @@ -7,6 +7,10 @@ import click export(setup_exports) +console = pkgs.rich.console.Console() +logger = pkgs.logging.getLogger(__name__) + + def eprint(*args, **kwargs): print(*args, **{"file": pkgs.sys.stderr, **kwargs}) @@ -31,13 +35,8 @@ def run_proverif(file, extra_args=[]): _run_proverif(file, extra_args) def _run_proverif(file, extra_args=[]): - # Do we need this extra check? -- karo - if extra_args is None: - extra_args = [] params = ["proverif", "-test", *extra_args, file] - # This appears to be superfluous printing? - print(params) - eprint(params) + logger.debug(params) process = exc_piped(params, stderr=pkgs.subprocess.PIPE, stdout=pkgs.subprocess.PIPE, text=True, bufsize=1) try: @@ -61,7 +60,7 @@ def _run_proverif(file, extra_args=[]): #print(p) except Exception as e: # When does this happen? Should the error even be ignored? Metaverif should probably just abort here, right? --karo - print(f"An error occurred: {e}") + logger.error(f"An error occurred: {e}") finally: process.stdout.close() process.wait() @@ -75,7 +74,7 @@ def cpp(file, cpp_prep): def _cpp(file, cpp_prep): - print(f"_cpp: {file}, {cpp_prep}") + logger.debug(f"_cpp: {file}, {cpp_prep}") file_path = pkgs.pathlib.Path(file) dirname = file_path.parent @@ -106,18 +105,15 @@ def awk(cpp_prep, awk_prep): def _awk(cpp_prep, awk_prep): params = ["awk", "-f", "marzipan/marzipan.awk", cpp_prep] - with open(awk_prep, 'w') as file: + with open(awk_prep, 'a+') as file: exc(params, stderr=pkgs.sys.stderr, stdout=file) file.write("\nprocess main") def pretty_output_line(prefix, mark, color, text): - # Vulnerable to injection attacks. Use better API. -- karo - prefix = f"[grey42]{prefix}[/grey42]" - content = f"[{color}]{mark} {text}[/{color}]" - - output = prefix + content - rich_print(output) + content = f"{mark} {text}" + console.print(prefix, style="grey42", end="", no_wrap=True) + console.print(content, style=color) def pretty_output_init(file_path): @@ -306,6 +302,7 @@ def _metaverif(tmpdir, file): @click.group() def main(): #pkgs.IPython.embed() + pkgs.logging.basicConfig(level=pkgs.logging.DEBUG) pass