From c810ad5642cd7a36b37c130c4e54e20e008e4e7a Mon Sep 17 00:00:00 2001 From: Alex Pyrgiotis Date: Wed, 2 Oct 2024 18:51:16 +0300 Subject: [PATCH] Drop unecessary temp files --- dangerzone/isolation_provider/base.py | 8 ++--- dangerzone/isolation_provider/container.py | 2 -- dangerzone/util.py | 12 -------- tests/test_cli.py | 34 ++++++++-------------- 4 files changed, 14 insertions(+), 42 deletions(-) diff --git a/dangerzone/isolation_provider/base.py b/dangerzone/isolation_provider/base.py index e0c0f66..d585872 100644 --- a/dangerzone/isolation_provider/base.py +++ b/dangerzone/isolation_provider/base.py @@ -5,7 +5,6 @@ import platform import signal import subprocess import sys -import tempfile from abc import ABC, abstractmethod from pathlib import Path from typing import IO, Callable, Iterator, Optional @@ -107,10 +106,8 @@ class IsolationProvider(ABC): self.progress_callback = progress_callback document.mark_as_converting() try: - with tempfile.TemporaryDirectory() as t: - Path(f"{t}/pixels").mkdir() - with self.doc_to_pixels_proc(document) as conversion_proc: - self._convert(document, t, ocr_lang, conversion_proc) + with self.doc_to_pixels_proc(document) as conversion_proc: + self._convert(document, ocr_lang, conversion_proc) document.mark_as_safe() if document.archive_after_conversion: document.archive() @@ -161,7 +158,6 @@ class IsolationProvider(ABC): def _convert( self, document: Document, - tempdir: str, ocr_lang: Optional[str], p: subprocess.Popen, ) -> None: diff --git a/dangerzone/isolation_provider/container.py b/dangerzone/isolation_provider/container.py index 8259361..b0f4880 100644 --- a/dangerzone/isolation_provider/container.py +++ b/dangerzone/isolation_provider/container.py @@ -4,12 +4,10 @@ import os import platform import shlex import shutil -import signal import subprocess from typing import List, Tuple from ..document import Document -from ..util import get_tmp_dir # NOQA : required for mocking in our tests. from ..util import get_resource_path, get_subprocess_startupinfo from .base import IsolationProvider, terminate_process_group diff --git a/dangerzone/util.py b/dangerzone/util.py index bbedfdb..f53ac4b 100644 --- a/dangerzone/util.py +++ b/dangerzone/util.py @@ -4,7 +4,6 @@ import platform import subprocess import sys import unicodedata -from typing import Optional import appdirs @@ -13,17 +12,6 @@ def get_config_dir() -> str: return appdirs.user_config_dir("dangerzone") -def get_tmp_dir() -> Optional[str]: - """Get the parent dir for the Dangerzone temporary dirs. - - This function returns the parent directory where Dangerzone will store its temporary - directories. The default behavior is to let Python choose for us (e.g., in `/tmp` - for Linux), which is why we return None. However, we still need to define this - function in order to be able to set this dir via mocking in our tests. - """ - return None - - def get_resource_path(filename: str) -> str: if getattr(sys, "dangerzone_dev", False): # Look for resources directory relative to python file diff --git a/tests/test_cli.py b/tests/test_cli.py index f79a8cf..21f934f 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -134,29 +134,19 @@ class TestCli: if os.environ.get("DUMMY_CONVERSION", False): args = ("--unsafe-dummy-conversion", *args) - with tempfile.TemporaryDirectory() as t: - tmp_dir = Path(t) - # TODO: Replace this with `contextlib.chdir()` [1], which was added in - # Python 3.11. - # - # [1]: https://docs.python.org/3/library/contextlib.html#contextlib.chdir - try: - if tmp_path is not None: - cwd = os.getcwd() - os.chdir(tmp_path) + # TODO: Replace this with `contextlib.chdir()` [1], which was added in + # Python 3.11. + # + # [1]: https://docs.python.org/3/library/contextlib.html#contextlib.chdir + try: + if tmp_path is not None: + cwd = os.getcwd() + os.chdir(tmp_path) - with mock.patch( - "dangerzone.isolation_provider.container.get_tmp_dir", - return_value=t, - ): - result = CliRunner().invoke(cli_main, args) - finally: - if tmp_path is not None: - os.chdir(cwd) - - if tmp_dir.exists(): - stale_files = list(tmp_dir.iterdir()) - assert not stale_files + result = CliRunner().invoke(cli_main, args) + finally: + if tmp_path is not None: + os.chdir(cwd) # XXX Print stdout so that junitXML exports with output capturing # actually include the stdout + stderr (they are combined into stdout)