From 1042c5197433a4b4be3fb082176e383a306cd984 Mon Sep 17 00:00:00 2001 From: Guthrie McAfee Armstrong Date: Mon, 6 Jun 2022 22:33:43 -0400 Subject: [PATCH] Move is_container_installed and install_container to container module --- dangerzone/cli.py | 2 +- dangerzone/container.py | 77 ++++++++++++++++++++++++++++++++ dangerzone/gui/main_window.py | 7 ++- dangerzone/util.py | 82 +---------------------------------- 4 files changed, 82 insertions(+), 86 deletions(-) diff --git a/dangerzone/cli.py b/dangerzone/cli.py index bceb6f8..af42ab7 100644 --- a/dangerzone/cli.py +++ b/dangerzone/cli.py @@ -84,7 +84,7 @@ def cli_main(output_filename, ocr_lang, filename): return # Ensure container is installed - dzutil.install_container() + container.install_container() # Convert the document print_header("Converting document to safe PDF") diff --git a/dangerzone/container.py b/dangerzone/container.py index 8e323d7..89be1fa 100644 --- a/dangerzone/container.py +++ b/dangerzone/container.py @@ -1,5 +1,7 @@ from __future__ import annotations +import gzip +import platform import subprocess import pipes import shutil @@ -188,3 +190,78 @@ def convert( # return False, f"Page {i} has an invalid RGB file size" # return True, True +def is_container_installed(): + """ + See if the podman container is installed. Linux only. + """ + # Get the image id + with open(dzutil.get_resource_path("image-id.txt")) as f: + expected_image_id = f.read().strip() + + # See if this image is already installed + installed = False + found_image_id = subprocess.check_output( + [ + dzutil.CONTAINER_RUNTIME, + "image", + "list", + "--format", + "{{.ID}}", + dzutil.CONTAINER_NAME, + ], + text=True, + startupinfo=dzutil.get_subprocess_startupinfo(), + ) + found_image_id = found_image_id.strip() + + if found_image_id == expected_image_id: + installed = True + elif found_image_id == "": + pass + else: + print("Deleting old dangerzone container image") + + try: + subprocess.check_output( + [dzutil.CONTAINER_RUNTIME, "rmi", "--force", found_image_id], + startupinfo=dzutil.get_subprocess_startupinfo(), + ) + except: + print("Couldn't delete old container image, so leaving it there") + + return installed + + +def install_container(): + """ + Make sure the podman container is installed. Linux only. + """ + if is_container_installed(): + return + + # Load the container into podman + print("Installing Dangerzone container image...") + + p = subprocess.Popen( + [dzutil.CONTAINER_RUNTIME, "load"], + stdin=subprocess.PIPE, + startupinfo=dzutil.get_subprocess_startupinfo(), + ) + + chunk_size = 10240 + compressed_container_path = dzutil.get_resource_path("container.tar.gz") + with gzip.open(compressed_container_path) as f: + while True: + chunk = f.read(chunk_size) + if len(chunk) > 0: + p.stdin.write(chunk) + else: + break + p.communicate() + + if not is_container_installed(): + print("Failed to install the container image") + return False + + print("Container image installed") + return True diff --git a/dangerzone/gui/main_window.py b/dangerzone/gui/main_window.py index 7fe25ee..3edc101 100644 --- a/dangerzone/gui/main_window.py +++ b/dangerzone/gui/main_window.py @@ -10,8 +10,7 @@ from colorama import Style, Fore import dangerzone.util as dzutil from dangerzone.gui import GuiCommon -from dangerzone.container import convert -from dangerzone.util import install_container +from dangerzone import container class MainWindow(QtWidgets.QMainWindow): @@ -91,7 +90,7 @@ class InstallContainerThread(QtCore.QThread): super(InstallContainerThread, self).__init__() def run(self): - install_container() + container.install_container() self.finished.emit() @@ -483,7 +482,7 @@ class ConvertThread(QtCore.QThread): else: ocr_lang = None - if convert( + if container.convert( self.common.input_filename, self.common.output_filename, ocr_lang, diff --git a/dangerzone/util.py b/dangerzone/util.py index 8cb7f67..fabd3c3 100644 --- a/dangerzone/util.py +++ b/dangerzone/util.py @@ -1,6 +1,5 @@ from __future__ import annotations -import gzip import inspect import os import pathlib @@ -14,8 +13,6 @@ import appdirs # then it belongs here. from colorama import Back, Fore, Style -import dangerzone - SYSTEM = platform.system() @@ -251,83 +248,6 @@ OCR_LANGUAGES = { } -def is_container_installed(): - """ - See if the podman container is installed. Linux only. - """ - # Get the image id - with open(get_resource_path("image-id.txt")) as f: - expected_image_id = f.read().strip() - - # See if this image is already installed - installed = False - found_image_id = subprocess.check_output( - [ - CONTAINER_RUNTIME, - "image", - "list", - "--format", - "{{.ID}}", - dangerzone.util.CONTAINER_NAME, - ], - text=True, - startupinfo=get_subprocess_startupinfo(), - ) - found_image_id = found_image_id.strip() - - if found_image_id == expected_image_id: - installed = True - elif found_image_id == "": - pass - else: - print("Deleting old dangerzone container image") - - try: - subprocess.check_output( - [CONTAINER_RUNTIME, "rmi", "--force", found_image_id], - startupinfo=get_subprocess_startupinfo(), - ) - except: - print("Couldn't delete old container image, so leaving it there") - - return installed - - -def install_container(): - """ - Make sure the podman container is installed. Linux only. - """ - if is_container_installed(): - return - - # Load the container into podman - print("Installing Dangerzone container image...") - - p = subprocess.Popen( - [CONTAINER_RUNTIME, "load"], - stdin=subprocess.PIPE, - startupinfo=get_subprocess_startupinfo(), - ) - - chunk_size = 10240 - compressed_container_path = get_resource_path("container.tar.gz") - with gzip.open(compressed_container_path) as f: - while True: - chunk = f.read(chunk_size) - if len(chunk) > 0: - p.stdin.write(chunk) - else: - break - p.communicate() - - if not is_container_installed(): - print("Failed to install the container image") - return False - - print("Container image installed") - return True - - def display_banner(): """ Raw ASCII art example: @@ -483,7 +403,7 @@ def display_banner(): + Back.BLACK + Fore.LIGHTWHITE_EX + Style.BRIGHT - + f"{' '*left_spaces}Dangerzone v{VERSION}{' '*right_spaces}" + + f"{' ' * left_spaces}Dangerzone v{VERSION}{' ' * right_spaces}" + Fore.YELLOW + Style.DIM + "│"