diff --git a/dangerzone/container_utils.py b/dangerzone/container_utils.py index d651f0a..5794764 100644 --- a/dangerzone/container_utils.py +++ b/dangerzone/container_utils.py @@ -121,7 +121,7 @@ def delete_image_tag(tag: str) -> None: def get_expected_tag() -> str: """Get the tag of the Dangerzone image tarball from the image-id.txt file.""" - with open(get_resource_path("image-id.txt")) as f: + with get_resource_path("image-id.txt").open() as f: return f.read().strip() @@ -130,7 +130,7 @@ def load_image_tarball() -> None: tarball_path = get_resource_path("container.tar") try: res = subprocess.run( - [get_runtime(), "load", "-i", tarball_path], + [get_runtime(), "load", "-i", str(tarball_path)], startupinfo=get_subprocess_startupinfo(), capture_output=True, check=True, diff --git a/dangerzone/gui/__init__.py b/dangerzone/gui/__init__.py index 0f51759..5a4c26c 100644 --- a/dangerzone/gui/__init__.py +++ b/dangerzone/gui/__init__.py @@ -51,7 +51,7 @@ class Application(QtWidgets.QApplication): def __init__(self, *args: typing.Any, **kwargs: typing.Any) -> None: super(Application, self).__init__(*args, **kwargs) self.setQuitOnLastWindowClosed(False) - with open(get_resource_path("dangerzone.css"), "r") as f: + with get_resource_path("dangerzone.css").open("r") as f: style = f.read() self.setStyleSheet(style) diff --git a/dangerzone/gui/logic.py b/dangerzone/gui/logic.py index 9721043..b8403b1 100644 --- a/dangerzone/gui/logic.py +++ b/dangerzone/gui/logic.py @@ -63,7 +63,7 @@ class DangerzoneGui(DangerzoneCore): path = get_resource_path("dangerzone.ico") else: path = get_resource_path("icon.png") - return QtGui.QIcon(path) + return QtGui.QIcon(str(path)) def open_pdf_viewer(self, filename: str) -> None: if platform.system() == "Darwin": @@ -252,7 +252,7 @@ class Alert(Dialog): def create_layout(self) -> QtWidgets.QBoxLayout: logo = QtWidgets.QLabel() logo.setPixmap( - QtGui.QPixmap.fromImage(QtGui.QImage(get_resource_path("icon.png"))) + QtGui.QPixmap.fromImage(QtGui.QImage(str(get_resource_path("icon.png")))) ) label = QtWidgets.QLabel() diff --git a/dangerzone/gui/main_window.py b/dangerzone/gui/main_window.py index 0728961..0adc9c2 100644 --- a/dangerzone/gui/main_window.py +++ b/dangerzone/gui/main_window.py @@ -62,7 +62,7 @@ def load_svg_image(filename: str, width: int, height: int) -> QtGui.QPixmap: This answer is basically taken from: https://stackoverflow.com/a/25689790 """ path = get_resource_path(filename) - svg_renderer = QtSvg.QSvgRenderer(path) + svg_renderer = QtSvg.QSvgRenderer(str(path)) image = QtGui.QImage(width, height, QtGui.QImage.Format_ARGB32) # Set the ARGB to 0 to prevent rendering artifacts image.fill(0x00000000) @@ -130,9 +130,8 @@ class MainWindow(QtWidgets.QMainWindow): # Header logo = QtWidgets.QLabel() - logo.setPixmap( - QtGui.QPixmap.fromImage(QtGui.QImage(get_resource_path("icon.png"))) - ) + icon_path = str(get_resource_path("icon.png")) + logo.setPixmap(QtGui.QPixmap.fromImage(QtGui.QImage(icon_path))) header_label = QtWidgets.QLabel("Dangerzone") header_label.setFont(self.dangerzone.fixed_font) header_label.setStyleSheet("QLabel { font-weight: bold; font-size: 50px; }") @@ -1222,7 +1221,7 @@ class DocumentsListWidget(QtWidgets.QListWidget): if not self.thread_pool_initized: max_jobs = self.dangerzone.isolation_provider.get_max_parallel_conversions() # Call freeze_support() to avoid passing unknown options to the subprocess. - # See https://github.com/freedomofpress/dangerzone/issues/873 + # See https://github.com/freedomofpress/Adangerzone/issues/873 freeze_support() self.thread_pool = ThreadPool(max_jobs) @@ -1306,7 +1305,7 @@ class DocumentWidget(QtWidgets.QWidget): def load_status_image(self, filename: str) -> QtGui.QPixmap: path = get_resource_path(filename) - img = QtGui.QImage(path) + img = QtGui.QImage(str(path)) image = QtGui.QPixmap.fromImage(img) return image.scaled(QtCore.QSize(15, 15)) diff --git a/dangerzone/isolation_provider/container.py b/dangerzone/isolation_provider/container.py index 1cd80cc..c133442 100644 --- a/dangerzone/isolation_provider/container.py +++ b/dangerzone/isolation_provider/container.py @@ -64,7 +64,7 @@ class Container(IsolationProvider): # # [1] https://github.com/freedomofpress/dangerzone/issues/846 # [2] https://github.com/containers/common/blob/d3283f8401eeeb21f3c59a425b5461f069e199a7/pkg/seccomp/seccomp.json - seccomp_json_path = get_resource_path("seccomp.gvisor.json") + seccomp_json_path = str(get_resource_path("seccomp.gvisor.json")) security_args += ["--security-opt", f"seccomp={seccomp_json_path}"] security_args += ["--cap-drop", "all"] diff --git a/dangerzone/isolation_provider/qubes.py b/dangerzone/isolation_provider/qubes.py index 4d2d5ee..d46e6cc 100644 --- a/dangerzone/isolation_provider/qubes.py +++ b/dangerzone/isolation_provider/qubes.py @@ -130,7 +130,6 @@ def is_qubes_native_conversion() -> bool: # This disambiguates if it is running a Qubes targetted build or not # (Qubes-specific builds don't ship the container image) - container_image_path = get_resource_path("container.tar") - return not os.path.exists(container_image_path) + return not get_resource_path("container.tar").exists() else: return False diff --git a/dangerzone/logic.py b/dangerzone/logic.py index eb588b9..1ec94d3 100644 --- a/dangerzone/logic.py +++ b/dangerzone/logic.py @@ -27,7 +27,7 @@ class DangerzoneCore(object): self.appdata_path = util.get_config_dir() # Languages supported by tesseract - with open(get_resource_path("ocr-languages.json"), "r") as f: + with get_resource_path("ocr-languages.json").open("r") as f: unsorted_ocr_languages = json.load(f) self.ocr_languages = dict(sorted(unsorted_ocr_languages.items())) diff --git a/dangerzone/util.py b/dangerzone/util.py index 1fa3025..6cae643 100644 --- a/dangerzone/util.py +++ b/dangerzone/util.py @@ -1,9 +1,9 @@ -import pathlib import platform import subprocess import sys import traceback import unicodedata +from pathlib import Path try: import platformdirs @@ -11,40 +11,39 @@ except ImportError: import appdirs as platformdirs -def get_config_dir() -> str: - return platformdirs.user_config_dir("dangerzone") +def get_config_dir() -> Path: + return Path(platformdirs.user_config_dir("dangerzone")) -def get_resource_path(filename: str) -> str: +def get_resource_path(filename: str) -> Path: if getattr(sys, "dangerzone_dev", False): # Look for resources directory relative to python file - project_root = pathlib.Path(__file__).parent.parent + project_root = Path(__file__).parent.parent prefix = project_root / "share" else: if platform.system() == "Darwin": - bin_path = pathlib.Path(sys.executable) + bin_path = Path(sys.executable) app_path = bin_path.parent.parent prefix = app_path / "Resources" / "share" elif platform.system() == "Linux": - prefix = pathlib.Path(sys.prefix) / "share" / "dangerzone" + prefix = Path(sys.prefix) / "share" / "dangerzone" elif platform.system() == "Windows": - exe_path = pathlib.Path(sys.executable) + exe_path = Path(sys.executable) dz_install_path = exe_path.parent prefix = dz_install_path / "share" else: raise NotImplementedError(f"Unsupported system {platform.system()}") - resource_path = prefix / filename - return str(resource_path) + return prefix / filename -def get_tessdata_dir() -> pathlib.Path: +def get_tessdata_dir() -> Path: if getattr(sys, "dangerzone_dev", False) or platform.system() in ( "Windows", "Darwin", ): # Always use the tessdata path from the Dangerzone ./share directory, for # development builds, or in Windows/macOS platforms. - return pathlib.Path(get_resource_path("tessdata")) + return get_resource_path("tessdata") # In case of Linux systems, grab the Tesseract data from any of the following # locations. We have found some of the locations through trial and error, whereas @@ -55,11 +54,11 @@ def get_tessdata_dir() -> pathlib.Path: # # [1] https://tesseract-ocr.github.io/tessdoc/Installation.html tessdata_dirs = [ - pathlib.Path("/usr/share/tessdata/"), # on some Debian - pathlib.Path("/usr/share/tesseract/tessdata/"), # on Fedora - pathlib.Path("/usr/share/tesseract-ocr/tessdata/"), # ? (documented) - pathlib.Path("/usr/share/tesseract-ocr/4.00/tessdata/"), # on Debian Bullseye - pathlib.Path("/usr/share/tesseract-ocr/5/tessdata/"), # on Debian Trixie + Path("/usr/share/tessdata/"), # on some Debian + Path("/usr/share/tesseract/tessdata/"), # on Fedora + Path("/usr/share/tesseract-ocr/tessdata/"), # ? (documented) + Path("/usr/share/tesseract-ocr/4.00/tessdata/"), # on Debian Bullseye + Path("/usr/share/tesseract-ocr/5/tessdata/"), # on Debian Trixie ] for dir in tessdata_dirs: @@ -71,7 +70,7 @@ def get_tessdata_dir() -> pathlib.Path: def get_version() -> str: try: - with open(get_resource_path("version.txt")) as f: + with get_resource_path("version.txt").open() as f: version = f.read().strip() except FileNotFoundError: # In dev mode, in Windows, get_resource_path doesn't work properly for the container, but luckily diff --git a/tests/isolation_provider/test_container.py b/tests/isolation_provider/test_container.py index c8c9655..7f39664 100644 --- a/tests/isolation_provider/test_container.py +++ b/tests/isolation_provider/test_container.py @@ -74,7 +74,7 @@ class TestContainer(IsolationProviderTest): container_utils.get_runtime(), "load", "-i", - get_resource_path("container.tar"), + get_resource_path("container.tar").absolute(), ], returncode=-1, ) @@ -113,7 +113,7 @@ class TestContainer(IsolationProviderTest): container_utils.get_runtime(), "load", "-i", - get_resource_path("container.tar"), + get_resource_path("container.tar").absolute(), ], ) with pytest.raises(errors.ImageNotPresentException): diff --git a/tests/test_util.py b/tests/test_util.py index 13d36ee..a8726bd 100644 --- a/tests/test_util.py +++ b/tests/test_util.py @@ -11,7 +11,7 @@ VERSION_FILE_NAME = "version.txt" def test_get_resource_path() -> None: share_dir = Path("share").resolve() - resource_path = Path(util.get_resource_path(VERSION_FILE_NAME)).parent + resource_path = util.get_resource_path(VERSION_FILE_NAME).parent assert share_dir.samefile(resource_path), ( f"{share_dir} is not the same file as {resource_path}" )