mirror of
https://github.com/freedomofpress/dangerzone.git
synced 2025-05-04 12:41:50 +02:00
Remove GlobalCommon, convert its methods (all static) to functions
This commit is contained in:
parent
519b8fd1b8
commit
ca5d17571b
5 changed files with 254 additions and 260 deletions
|
@ -5,7 +5,7 @@ import click
|
||||||
import colorama
|
import colorama
|
||||||
from colorama import Fore, Style # type: ignore
|
from colorama import Fore, Style # type: ignore
|
||||||
|
|
||||||
from .global_common import GlobalCommon
|
from .global_common import display_banner, install_container
|
||||||
from .common import Common
|
from .common import Common
|
||||||
from .container import convert
|
from .container import convert
|
||||||
import dangerzone.util as dzutil
|
import dangerzone.util as dzutil
|
||||||
|
@ -23,7 +23,7 @@ def print_header(s):
|
||||||
def cli_main(output_filename, ocr_lang, filename):
|
def cli_main(output_filename, ocr_lang, filename):
|
||||||
colorama.init(autoreset=True)
|
colorama.init(autoreset=True)
|
||||||
common = Common()
|
common = Common()
|
||||||
GlobalCommon.display_banner()
|
display_banner()
|
||||||
|
|
||||||
# Validate filename
|
# Validate filename
|
||||||
valid = True
|
valid = True
|
||||||
|
@ -85,7 +85,7 @@ def cli_main(output_filename, ocr_lang, filename):
|
||||||
return
|
return
|
||||||
|
|
||||||
# Ensure container is installed
|
# Ensure container is installed
|
||||||
GlobalCommon.install_container()
|
install_container()
|
||||||
|
|
||||||
# Convert the document
|
# Convert the document
|
||||||
print_header("Converting document to safe PDF")
|
print_header("Converting document to safe PDF")
|
||||||
|
|
|
@ -6,13 +6,84 @@ import dangerzone
|
||||||
import dangerzone.util as dzutil
|
import dangerzone.util as dzutil
|
||||||
|
|
||||||
|
|
||||||
class GlobalCommon(object):
|
def is_container_installed():
|
||||||
"""
|
"""
|
||||||
The GlobalCommon class contains functionality shared throughout the app
|
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()
|
||||||
|
|
||||||
@staticmethod
|
# See if this image is already installed
|
||||||
def display_banner():
|
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:
|
Raw ASCII art example:
|
||||||
╭──────────────────────────╮
|
╭──────────────────────────╮
|
||||||
|
@ -186,80 +257,3 @@ class GlobalCommon(object):
|
||||||
+ "│"
|
+ "│"
|
||||||
)
|
)
|
||||||
print(Back.BLACK + Fore.YELLOW + Style.DIM + "╰──────────────────────────╯")
|
print(Back.BLACK + Fore.YELLOW + Style.DIM + "╰──────────────────────────╯")
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
def install_container():
|
|
||||||
"""
|
|
||||||
Make sure the podman container is installed. Linux only.
|
|
||||||
"""
|
|
||||||
if GlobalCommon.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 GlobalCommon.is_container_installed():
|
|
||||||
print("Failed to install the container image")
|
|
||||||
return False
|
|
||||||
|
|
||||||
print("Container image installed")
|
|
||||||
return True
|
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
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
|
|
||||||
|
|
|
@ -10,7 +10,7 @@ import uuid
|
||||||
import colorama
|
import colorama
|
||||||
|
|
||||||
from .application import Application
|
from .application import Application
|
||||||
from .common import GuiCommon
|
from .guicommon import GuiCommon
|
||||||
from .main_window import MainWindow
|
from .main_window import MainWindow
|
||||||
from .systray import SysTray
|
from .systray import SysTray
|
||||||
|
|
||||||
|
|
|
@ -12,7 +12,7 @@ import dangerzone.util as dzutil
|
||||||
from . import GuiCommon
|
from . import GuiCommon
|
||||||
from ..common import Common
|
from ..common import Common
|
||||||
from ..container import convert
|
from ..container import convert
|
||||||
from ..global_common import GlobalCommon
|
from ..global_common import install_container
|
||||||
|
|
||||||
|
|
||||||
class MainWindow(QtWidgets.QMainWindow):
|
class MainWindow(QtWidgets.QMainWindow):
|
||||||
|
@ -97,7 +97,7 @@ class InstallContainerThread(QtCore.QThread):
|
||||||
super(InstallContainerThread, self).__init__()
|
super(InstallContainerThread, self).__init__()
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
GlobalCommon.install_container()
|
install_container()
|
||||||
self.finished.emit()
|
self.finished.emit()
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue