From ba516476dedf2d229f393e8a378bb1b1f5115831 Mon Sep 17 00:00:00 2001 From: Guthrie McAfee Armstrong Date: Mon, 6 Jun 2022 21:40:02 -0400 Subject: [PATCH] Delete global_common.py, move its functions to dzutil --- dangerzone/cli.py | 2 +- dangerzone/global_common.py | 259 ---------------------------------- dangerzone/gui/__init__.py | 2 +- dangerzone/gui/main_window.py | 2 +- dangerzone/util.py | 257 +++++++++++++++++++++++++++++++++ 5 files changed, 260 insertions(+), 262 deletions(-) delete mode 100644 dangerzone/global_common.py diff --git a/dangerzone/cli.py b/dangerzone/cli.py index 2f20e1c..7f2f85f 100644 --- a/dangerzone/cli.py +++ b/dangerzone/cli.py @@ -5,7 +5,7 @@ import click import colorama from colorama import Fore, Style # type: ignore -from .global_common import display_banner, install_container +from .util import install_container, display_banner from .common import Common from .container import convert import dangerzone.util as dzutil diff --git a/dangerzone/global_common.py b/dangerzone/global_common.py deleted file mode 100644 index f190c2d..0000000 --- a/dangerzone/global_common.py +++ /dev/null @@ -1,259 +0,0 @@ -import subprocess -import gzip -from colorama import Fore, Back, Style - -import dangerzone -import dangerzone.util as dzutil - - -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}}", - dangerzone.util.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 - - -def display_banner(): - """ - Raw ASCII art example: - ╭──────────────────────────╮ - │ ▄██▄ │ - │ ██████ │ - │ ███▀▀▀██ │ - │ ███ ████ │ - │ ███ ██████ │ - │ ███ ▀▀▀▀████ │ - │ ███████ ▄██████ │ - │ ███████ ▄█████████ │ - │ ████████████████████ │ - │ ▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀ │ - │ │ - │ Dangerzone v0.1.5 │ - │ https://dangerzone.rocks │ - ╰──────────────────────────╯ - """ - - print(Back.BLACK + Fore.YELLOW + Style.DIM + "╭──────────────────────────╮") - print( - Back.BLACK - + Fore.YELLOW - + Style.DIM - + "│" - + Fore.LIGHTYELLOW_EX - + Style.NORMAL - + " ▄██▄ " - + Fore.YELLOW - + Style.DIM - + "│" - ) - print( - Back.BLACK - + Fore.YELLOW - + Style.DIM - + "│" - + Fore.LIGHTYELLOW_EX - + Style.NORMAL - + " ██████ " - + Fore.YELLOW - + Style.DIM - + "│" - ) - print( - Back.BLACK - + Fore.YELLOW - + Style.DIM - + "│" - + Fore.LIGHTYELLOW_EX - + Style.NORMAL - + " ███▀▀▀██ " - + Fore.YELLOW - + Style.DIM - + "│" - ) - print( - Back.BLACK - + Fore.YELLOW - + Style.DIM - + "│" - + Fore.LIGHTYELLOW_EX - + Style.NORMAL - + " ███ ████ " - + Fore.YELLOW - + Style.DIM - + "│" - ) - print( - Back.BLACK - + Fore.YELLOW - + Style.DIM - + "│" - + Fore.LIGHTYELLOW_EX - + Style.NORMAL - + " ███ ██████ " - + Fore.YELLOW - + Style.DIM - + "│" - ) - print( - Back.BLACK - + Fore.YELLOW - + Style.DIM - + "│" - + Fore.LIGHTYELLOW_EX - + Style.NORMAL - + " ███ ▀▀▀▀████ " - + Fore.YELLOW - + Style.DIM - + "│" - ) - print( - Back.BLACK - + Fore.YELLOW - + Style.DIM - + "│" - + Fore.LIGHTYELLOW_EX - + Style.NORMAL - + " ███████ ▄██████ " - + Fore.YELLOW - + Style.DIM - + "│" - ) - print( - Back.BLACK - + Fore.YELLOW - + Style.DIM - + "│" - + Fore.LIGHTYELLOW_EX - + Style.NORMAL - + " ███████ ▄█████████ " - + Fore.YELLOW - + Style.DIM - + "│" - ) - print( - Back.BLACK - + Fore.YELLOW - + Style.DIM - + "│" - + Fore.LIGHTYELLOW_EX - + Style.NORMAL - + " ████████████████████ " - + Fore.YELLOW - + Style.DIM - + "│" - ) - print( - Back.BLACK - + Fore.YELLOW - + Style.DIM - + "│" - + Fore.LIGHTYELLOW_EX - + Style.NORMAL - + " ▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀ " - + Fore.YELLOW - + Style.DIM - + "│" - ) - print(Back.BLACK + Fore.YELLOW + Style.DIM + "│ │") - left_spaces = (15 - len(dzutil.VERSION) - 1) // 2 - right_spaces = left_spaces - if left_spaces + len(dzutil.VERSION) + 1 + right_spaces < 15: - right_spaces += 1 - print( - Back.BLACK - + Fore.YELLOW - + Style.DIM - + "│" - + Style.RESET_ALL - + Back.BLACK - + Fore.LIGHTWHITE_EX - + Style.BRIGHT - + f"{' '*left_spaces}Dangerzone v{dzutil.VERSION}{' '*right_spaces}" - + Fore.YELLOW - + Style.DIM - + "│" - ) - print( - Back.BLACK - + Fore.YELLOW - + Style.DIM - + "│" - + Style.RESET_ALL - + Back.BLACK - + Fore.LIGHTWHITE_EX - + " https://dangerzone.rocks " - + Fore.YELLOW - + Style.DIM - + "│" - ) - print(Back.BLACK + Fore.YELLOW + Style.DIM + "╰──────────────────────────╯") diff --git a/dangerzone/gui/__init__.py b/dangerzone/gui/__init__.py index 0a4e9cb..f491a7e 100644 --- a/dangerzone/gui/__init__.py +++ b/dangerzone/gui/__init__.py @@ -10,7 +10,7 @@ import uuid import colorama from .application import Application -from .guicommon import GuiCommon +from .gui_common import GuiCommon from .main_window import MainWindow from .systray import SysTray diff --git a/dangerzone/gui/main_window.py b/dangerzone/gui/main_window.py index ce71e64..0715086 100644 --- a/dangerzone/gui/main_window.py +++ b/dangerzone/gui/main_window.py @@ -12,7 +12,7 @@ import dangerzone.util as dzutil from . import GuiCommon from ..common import Common from ..container import convert -from ..global_common import install_container +from ..util import install_container class MainWindow(QtWidgets.QMainWindow): diff --git a/dangerzone/util.py b/dangerzone/util.py index f19d114..24586db 100644 --- a/dangerzone/util.py +++ b/dangerzone/util.py @@ -1,5 +1,6 @@ from __future__ import annotations +import gzip import inspect import os import pathlib @@ -11,6 +12,9 @@ import appdirs # If a general-purpose function or constant doesn't depend on anything else in the dangerzone package, # then it belongs here. +from colorama import Back, Fore, Style + +import dangerzone SYSTEM = platform.system() @@ -241,3 +245,256 @@ OCR_LANGUAGES = { "Yiddish": "yid", "Yoruba": "yor" } + + +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: + ╭──────────────────────────╮ + │ ▄██▄ │ + │ ██████ │ + │ ███▀▀▀██ │ + │ ███ ████ │ + │ ███ ██████ │ + │ ███ ▀▀▀▀████ │ + │ ███████ ▄██████ │ + │ ███████ ▄█████████ │ + │ ████████████████████ │ + │ ▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀ │ + │ │ + │ Dangerzone v0.1.5 │ + │ https://dangerzone.rocks │ + ╰──────────────────────────╯ + """ + + print(Back.BLACK + Fore.YELLOW + Style.DIM + "╭──────────────────────────╮") + print( + Back.BLACK + + Fore.YELLOW + + Style.DIM + + "│" + + Fore.LIGHTYELLOW_EX + + Style.NORMAL + + " ▄██▄ " + + Fore.YELLOW + + Style.DIM + + "│" + ) + print( + Back.BLACK + + Fore.YELLOW + + Style.DIM + + "│" + + Fore.LIGHTYELLOW_EX + + Style.NORMAL + + " ██████ " + + Fore.YELLOW + + Style.DIM + + "│" + ) + print( + Back.BLACK + + Fore.YELLOW + + Style.DIM + + "│" + + Fore.LIGHTYELLOW_EX + + Style.NORMAL + + " ███▀▀▀██ " + + Fore.YELLOW + + Style.DIM + + "│" + ) + print( + Back.BLACK + + Fore.YELLOW + + Style.DIM + + "│" + + Fore.LIGHTYELLOW_EX + + Style.NORMAL + + " ███ ████ " + + Fore.YELLOW + + Style.DIM + + "│" + ) + print( + Back.BLACK + + Fore.YELLOW + + Style.DIM + + "│" + + Fore.LIGHTYELLOW_EX + + Style.NORMAL + + " ███ ██████ " + + Fore.YELLOW + + Style.DIM + + "│" + ) + print( + Back.BLACK + + Fore.YELLOW + + Style.DIM + + "│" + + Fore.LIGHTYELLOW_EX + + Style.NORMAL + + " ███ ▀▀▀▀████ " + + Fore.YELLOW + + Style.DIM + + "│" + ) + print( + Back.BLACK + + Fore.YELLOW + + Style.DIM + + "│" + + Fore.LIGHTYELLOW_EX + + Style.NORMAL + + " ███████ ▄██████ " + + Fore.YELLOW + + Style.DIM + + "│" + ) + print( + Back.BLACK + + Fore.YELLOW + + Style.DIM + + "│" + + Fore.LIGHTYELLOW_EX + + Style.NORMAL + + " ███████ ▄█████████ " + + Fore.YELLOW + + Style.DIM + + "│" + ) + print( + Back.BLACK + + Fore.YELLOW + + Style.DIM + + "│" + + Fore.LIGHTYELLOW_EX + + Style.NORMAL + + " ████████████████████ " + + Fore.YELLOW + + Style.DIM + + "│" + ) + print( + Back.BLACK + + Fore.YELLOW + + Style.DIM + + "│" + + Fore.LIGHTYELLOW_EX + + Style.NORMAL + + " ▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀ " + + Fore.YELLOW + + Style.DIM + + "│" + ) + print(Back.BLACK + Fore.YELLOW + Style.DIM + "│ │") + left_spaces = (15 - len(VERSION) - 1) // 2 + right_spaces = left_spaces + if left_spaces + len(VERSION) + 1 + right_spaces < 15: + right_spaces += 1 + print( + Back.BLACK + + Fore.YELLOW + + Style.DIM + + "│" + + Style.RESET_ALL + + Back.BLACK + + Fore.LIGHTWHITE_EX + + Style.BRIGHT + + f"{' '*left_spaces}Dangerzone v{VERSION}{' '*right_spaces}" + + Fore.YELLOW + + Style.DIM + + "│" + ) + print( + Back.BLACK + + Fore.YELLOW + + Style.DIM + + "│" + + Style.RESET_ALL + + Back.BLACK + + Fore.LIGHTWHITE_EX + + " https://dangerzone.rocks " + + Fore.YELLOW + + Style.DIM + + "│" + ) + print(Back.BLACK + Fore.YELLOW + Style.DIM + "╰──────────────────────────╯")