From 0fba8b1d767472fdfce96e76e56dd929d8c57b6a Mon Sep 17 00:00:00 2001 From: Alex Pyrgiotis Date: Mon, 30 Sep 2024 12:45:51 +0300 Subject: [PATCH] WIP: Make the dummy provider less... dummy --- dangerzone/isolation_provider/dummy.py | 69 +++++++++++--------------- 1 file changed, 30 insertions(+), 39 deletions(-) diff --git a/dangerzone/isolation_provider/dummy.py b/dangerzone/isolation_provider/dummy.py index 1db99e4df..81f1a817f 100644 --- a/dangerzone/isolation_provider/dummy.py +++ b/dangerzone/isolation_provider/dummy.py @@ -6,6 +6,8 @@ import time from typing import Callable, Optional +from ..document import Document +from ..conversion.common import DangerzoneConverter from ..document import Document from ..util import get_resource_path from .base import IsolationProvider @@ -13,6 +15,18 @@ log = logging.getLogger(__name__) +def dummy_script() -> None: + sys.stdin.read() + + pages = 2 + width = height = 9 + DangerzoneConverter._write_int(pages) + for page in range(pages): + DangerzoneConverter._write_int(width) + DangerzoneConverter._write_int(height) + DangerzoneConverter._write_bytes(width * height * 3 * b"A") + + class Dummy(IsolationProvider): """Dummy Isolation Provider (FOR TESTING ONLY) @@ -28,53 +42,30 @@ def __init__(self) -> None: + "called in a non-testing system." ) + super().__init__() + def install(self) -> bool: return True - def convert( - self, - document: Document, - ocr_lang: Optional[str], - progress_callback: Optional[Callable] = None, - ) -> None: - self.progress_callback = None - log.debug("Dummy converter started:") - log.debug( - f" - document: {os.path.basename(document.input_filename)} ({document.id})" - ) - log.debug(f" - ocr : {ocr_lang}") - log.debug("\n(simulating conversion)") - success = True - progress = [ - [False, "Converting to PDF using GraphicsMagick", 0.0], - [False, "Separating document into pages", 3.0], - [False, "Converting page 1/1 to pixels", 5.0], - [False, "Converted document to pixels", 50.0], - [False, "Converting page 1/1 from pixels to PDF", 50.0], - [False, "Merging 1 pages into a single PDF", 95.0], - [False, "Compressing PDF", 97.0], - [False, "Safe PDF created", 100.0], - ] - for error, text, percentage in progress: - self.print_progress(document, error, text, percentage) # type: ignore [arg-type] - if error: - success = False - # time.sleep(0.2) - if success: - shutil.copy( - get_resource_path("dummy_document.pdf"), document.output_filename - ) - document.mark_as_safe() - if document.archive_after_conversion: - document.archive() - def start_doc_to_pixels_proc(self, document: Document) -> subprocess.Popen: - return subprocess.Popen("True", start_new_session=True) + cmd = [ + "python3", + "-c", + "from dangerzone.isolation_provider.dummy import dummy_script;" + " dummy_script()" + ] + return subprocess.Popen( + cmd, + stdin=subprocess.PIPE, + stdout=subprocess.PIPE, + stderr=self.proc_stderr, + start_new_session=True + ) def terminate_doc_to_pixels_proc( self, document: Document, p: subprocess.Popen ) -> None: - pass + self.terminate_process_group(p) def get_max_parallel_conversions(self) -> int: return 1