mirror of
https://github.com/freedomofpress/dangerzone.git
synced 2025-04-29 10:12:38 +02:00
Drop unecessary temp files
This commit is contained in:
parent
76b0de4169
commit
c810ad5642
4 changed files with 14 additions and 42 deletions
|
@ -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:
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in a new issue