Drop unecessary temp files

This commit is contained in:
Alex Pyrgiotis 2024-10-02 18:51:16 +03:00
parent 76b0de4169
commit c810ad5642
No known key found for this signature in database
GPG key ID: B6C15EBA0357C9AA
4 changed files with 14 additions and 42 deletions

View file

@ -5,7 +5,6 @@ import platform
import signal import signal
import subprocess import subprocess
import sys import sys
import tempfile
from abc import ABC, abstractmethod from abc import ABC, abstractmethod
from pathlib import Path from pathlib import Path
from typing import IO, Callable, Iterator, Optional from typing import IO, Callable, Iterator, Optional
@ -107,10 +106,8 @@ class IsolationProvider(ABC):
self.progress_callback = progress_callback self.progress_callback = progress_callback
document.mark_as_converting() document.mark_as_converting()
try: try:
with tempfile.TemporaryDirectory() as t: with self.doc_to_pixels_proc(document) as conversion_proc:
Path(f"{t}/pixels").mkdir() self._convert(document, ocr_lang, conversion_proc)
with self.doc_to_pixels_proc(document) as conversion_proc:
self._convert(document, t, ocr_lang, conversion_proc)
document.mark_as_safe() document.mark_as_safe()
if document.archive_after_conversion: if document.archive_after_conversion:
document.archive() document.archive()
@ -161,7 +158,6 @@ class IsolationProvider(ABC):
def _convert( def _convert(
self, self,
document: Document, document: Document,
tempdir: str,
ocr_lang: Optional[str], ocr_lang: Optional[str],
p: subprocess.Popen, p: subprocess.Popen,
) -> None: ) -> None:

View file

@ -4,12 +4,10 @@ import os
import platform import platform
import shlex import shlex
import shutil import shutil
import signal
import subprocess import subprocess
from typing import List, Tuple from typing import List, Tuple
from ..document import Document 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 ..util import get_resource_path, get_subprocess_startupinfo
from .base import IsolationProvider, terminate_process_group from .base import IsolationProvider, terminate_process_group

View file

@ -4,7 +4,6 @@ import platform
import subprocess import subprocess
import sys import sys
import unicodedata import unicodedata
from typing import Optional
import appdirs import appdirs
@ -13,17 +12,6 @@ def get_config_dir() -> str:
return appdirs.user_config_dir("dangerzone") 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: def get_resource_path(filename: str) -> str:
if getattr(sys, "dangerzone_dev", False): if getattr(sys, "dangerzone_dev", False):
# Look for resources directory relative to python file # Look for resources directory relative to python file

View file

@ -134,29 +134,19 @@ class TestCli:
if os.environ.get("DUMMY_CONVERSION", False): if os.environ.get("DUMMY_CONVERSION", False):
args = ("--unsafe-dummy-conversion", *args) args = ("--unsafe-dummy-conversion", *args)
with tempfile.TemporaryDirectory() as t: # TODO: Replace this with `contextlib.chdir()` [1], which was added in
tmp_dir = Path(t) # Python 3.11.
# 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:
# [1]: https://docs.python.org/3/library/contextlib.html#contextlib.chdir if tmp_path is not None:
try: cwd = os.getcwd()
if tmp_path is not None: os.chdir(tmp_path)
cwd = os.getcwd()
os.chdir(tmp_path)
with mock.patch( result = CliRunner().invoke(cli_main, args)
"dangerzone.isolation_provider.container.get_tmp_dir", finally:
return_value=t, if tmp_path is not None:
): os.chdir(cwd)
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
# XXX Print stdout so that junitXML exports with output capturing # XXX Print stdout so that junitXML exports with output capturing
# actually include the stdout + stderr (they are combined into stdout) # actually include the stdout + stderr (they are combined into stdout)