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 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:
|
|
||||||
Path(f"{t}/pixels").mkdir()
|
|
||||||
with self.doc_to_pixels_proc(document) as conversion_proc:
|
with self.doc_to_pixels_proc(document) as conversion_proc:
|
||||||
self._convert(document, t, ocr_lang, conversion_proc)
|
self._convert(document, 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:
|
||||||
|
|
|
@ -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
|
||||||
|
|
||||||
|
|
|
@ -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
|
||||||
|
|
|
@ -134,8 +134,6 @@ 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:
|
|
||||||
tmp_dir = Path(t)
|
|
||||||
# TODO: Replace this with `contextlib.chdir()` [1], which was added in
|
# TODO: Replace this with `contextlib.chdir()` [1], which was added in
|
||||||
# Python 3.11.
|
# Python 3.11.
|
||||||
#
|
#
|
||||||
|
@ -145,19 +143,11 @@ class TestCli:
|
||||||
cwd = os.getcwd()
|
cwd = os.getcwd()
|
||||||
os.chdir(tmp_path)
|
os.chdir(tmp_path)
|
||||||
|
|
||||||
with mock.patch(
|
|
||||||
"dangerzone.isolation_provider.container.get_tmp_dir",
|
|
||||||
return_value=t,
|
|
||||||
):
|
|
||||||
result = CliRunner().invoke(cli_main, args)
|
result = CliRunner().invoke(cli_main, args)
|
||||||
finally:
|
finally:
|
||||||
if tmp_path is not None:
|
if tmp_path is not None:
|
||||||
os.chdir(cwd)
|
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)
|
||||||
print(result.stdout)
|
print(result.stdout)
|
||||||
|
|
Loading…
Reference in a new issue