Separate config dirs from temp dirs

Do not store temporary directories in the Dangerzone's config directory.
There are two reasons for that:

1. They are ephemeral, and they need a temporary place to be stored,
   preferably RAM-backed.
2. We need to set them while running our CI tests.
This commit is contained in:
Alex Pyrgiotis 2023-02-15 15:22:53 +02:00
parent 9b3d98b20b
commit 44c324f9ac
No known key found for this signature in database
GPG key ID: B6C15EBA0357C9AA
3 changed files with 22 additions and 9 deletions

View file

@ -9,10 +9,8 @@ import subprocess
import tempfile import tempfile
from typing import Callable, List, Optional, Tuple from typing import Callable, List, Optional, Tuple
import appdirs
from ..document import Document from ..document import Document
from ..util import get_resource_path, get_subprocess_startupinfo from ..util import get_resource_path, get_subprocess_startupinfo, get_tmp_dir
from .base import IsolationProvider from .base import IsolationProvider
# Define startupinfo for subprocesses # Define startupinfo for subprocesses
@ -225,9 +223,7 @@ class Container(IsolationProvider):
else: else:
ocr = "0" ocr = "0"
dz_tmp = os.path.join(appdirs.user_config_dir("dangerzone"), "tmp") dz_tmp = get_tmp_dir()
os.makedirs(dz_tmp, exist_ok=True)
tmpdir = tempfile.TemporaryDirectory(dir=dz_tmp) tmpdir = tempfile.TemporaryDirectory(dir=dz_tmp)
pixel_dir = os.path.join(tmpdir.name, "pixels") pixel_dir = os.path.join(tmpdir.name, "pixels")
safe_dir = os.path.join(tmpdir.name, "safe") safe_dir = os.path.join(tmpdir.name, "safe")

View file

@ -9,10 +9,9 @@ import subprocess
import sys import sys
from typing import Callable, List, Optional from typing import Callable, List, Optional
import appdirs
import colorama import colorama
from . import errors from . import errors, util
from .document import Document from .document import Document
from .isolation_provider.base import IsolationProvider from .isolation_provider.base import IsolationProvider
from .settings import Settings from .settings import Settings
@ -31,7 +30,7 @@ class DangerzoneCore(object):
colorama.init(autoreset=True) colorama.init(autoreset=True)
# App data folder # App data folder
self.appdata_path = appdirs.user_config_dir("dangerzone") self.appdata_path = util.get_config_dir()
# Languages supported by tesseract # Languages supported by tesseract
with open(get_resource_path("ocr-languages.json"), "r") as f: with open(get_resource_path("ocr-languages.json"), "r") as f:

View file

@ -2,6 +2,24 @@ import pathlib
import platform import platform
import subprocess import subprocess
import sys import sys
from typing import Optional
import appdirs
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: def get_resource_path(filename: str) -> str: