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,260 +6,254 @@ 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()
|
||||||
|
|
||||||
|
# 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 │
|
||||||
|
╰──────────────────────────╯
|
||||||
"""
|
"""
|
||||||
|
|
||||||
@staticmethod
|
print(Back.BLACK + Fore.YELLOW + Style.DIM + "╭──────────────────────────╮")
|
||||||
def display_banner():
|
print(
|
||||||
"""
|
Back.BLACK
|
||||||
Raw ASCII art example:
|
+ Fore.YELLOW
|
||||||
╭──────────────────────────╮
|
+ Style.DIM
|
||||||
│ ▄██▄ │
|
+ "│"
|
||||||
│ ██████ │
|
+ Fore.LIGHTYELLOW_EX
|
||||||
│ ███▀▀▀██ │
|
+ Style.NORMAL
|
||||||
│ ███ ████ │
|
+ " ▄██▄ "
|
||||||
│ ███ ██████ │
|
+ Fore.YELLOW
|
||||||
│ ███ ▀▀▀▀████ │
|
+ Style.DIM
|
||||||
│ ███████ ▄██████ │
|
+ "│"
|
||||||
│ ███████ ▄█████████ │
|
)
|
||||||
│ ████████████████████ │
|
print(
|
||||||
│ ▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀ │
|
Back.BLACK
|
||||||
│ │
|
+ Fore.YELLOW
|
||||||
│ Dangerzone v0.1.5 │
|
+ Style.DIM
|
||||||
│ https://dangerzone.rocks │
|
+ "│"
|
||||||
╰──────────────────────────╯
|
+ Fore.LIGHTYELLOW_EX
|
||||||
"""
|
+ Style.NORMAL
|
||||||
|
+ " ██████ "
|
||||||
print(Back.BLACK + Fore.YELLOW + Style.DIM + "╭──────────────────────────╮")
|
+ Fore.YELLOW
|
||||||
print(
|
+ Style.DIM
|
||||||
Back.BLACK
|
+ "│"
|
||||||
+ Fore.YELLOW
|
)
|
||||||
+ Style.DIM
|
print(
|
||||||
+ "│"
|
Back.BLACK
|
||||||
+ Fore.LIGHTYELLOW_EX
|
+ Fore.YELLOW
|
||||||
+ Style.NORMAL
|
+ Style.DIM
|
||||||
+ " ▄██▄ "
|
+ "│"
|
||||||
+ Fore.YELLOW
|
+ Fore.LIGHTYELLOW_EX
|
||||||
+ Style.DIM
|
+ Style.NORMAL
|
||||||
+ "│"
|
+ " ███▀▀▀██ "
|
||||||
)
|
+ Fore.YELLOW
|
||||||
print(
|
+ Style.DIM
|
||||||
Back.BLACK
|
+ "│"
|
||||||
+ Fore.YELLOW
|
)
|
||||||
+ Style.DIM
|
print(
|
||||||
+ "│"
|
Back.BLACK
|
||||||
+ Fore.LIGHTYELLOW_EX
|
+ Fore.YELLOW
|
||||||
+ Style.NORMAL
|
+ Style.DIM
|
||||||
+ " ██████ "
|
+ "│"
|
||||||
+ Fore.YELLOW
|
+ Fore.LIGHTYELLOW_EX
|
||||||
+ Style.DIM
|
+ Style.NORMAL
|
||||||
+ "│"
|
+ " ███ ████ "
|
||||||
)
|
+ Fore.YELLOW
|
||||||
print(
|
+ Style.DIM
|
||||||
Back.BLACK
|
+ "│"
|
||||||
+ Fore.YELLOW
|
)
|
||||||
+ Style.DIM
|
print(
|
||||||
+ "│"
|
Back.BLACK
|
||||||
+ Fore.LIGHTYELLOW_EX
|
+ Fore.YELLOW
|
||||||
+ Style.NORMAL
|
+ Style.DIM
|
||||||
+ " ███▀▀▀██ "
|
+ "│"
|
||||||
+ Fore.YELLOW
|
+ Fore.LIGHTYELLOW_EX
|
||||||
+ Style.DIM
|
+ Style.NORMAL
|
||||||
+ "│"
|
+ " ███ ██████ "
|
||||||
)
|
+ Fore.YELLOW
|
||||||
print(
|
+ Style.DIM
|
||||||
Back.BLACK
|
+ "│"
|
||||||
+ Fore.YELLOW
|
)
|
||||||
+ Style.DIM
|
print(
|
||||||
+ "│"
|
Back.BLACK
|
||||||
+ Fore.LIGHTYELLOW_EX
|
+ Fore.YELLOW
|
||||||
+ Style.NORMAL
|
+ Style.DIM
|
||||||
+ " ███ ████ "
|
+ "│"
|
||||||
+ Fore.YELLOW
|
+ Fore.LIGHTYELLOW_EX
|
||||||
+ Style.DIM
|
+ Style.NORMAL
|
||||||
+ "│"
|
+ " ███ ▀▀▀▀████ "
|
||||||
)
|
+ Fore.YELLOW
|
||||||
print(
|
+ Style.DIM
|
||||||
Back.BLACK
|
+ "│"
|
||||||
+ Fore.YELLOW
|
)
|
||||||
+ Style.DIM
|
print(
|
||||||
+ "│"
|
Back.BLACK
|
||||||
+ Fore.LIGHTYELLOW_EX
|
+ Fore.YELLOW
|
||||||
+ Style.NORMAL
|
+ Style.DIM
|
||||||
+ " ███ ██████ "
|
+ "│"
|
||||||
+ Fore.YELLOW
|
+ Fore.LIGHTYELLOW_EX
|
||||||
+ Style.DIM
|
+ Style.NORMAL
|
||||||
+ "│"
|
+ " ███████ ▄██████ "
|
||||||
)
|
+ Fore.YELLOW
|
||||||
print(
|
+ Style.DIM
|
||||||
Back.BLACK
|
+ "│"
|
||||||
+ Fore.YELLOW
|
)
|
||||||
+ Style.DIM
|
print(
|
||||||
+ "│"
|
Back.BLACK
|
||||||
+ Fore.LIGHTYELLOW_EX
|
+ Fore.YELLOW
|
||||||
+ Style.NORMAL
|
+ Style.DIM
|
||||||
+ " ███ ▀▀▀▀████ "
|
+ "│"
|
||||||
+ Fore.YELLOW
|
+ Fore.LIGHTYELLOW_EX
|
||||||
+ Style.DIM
|
+ Style.NORMAL
|
||||||
+ "│"
|
+ " ███████ ▄█████████ "
|
||||||
)
|
+ Fore.YELLOW
|
||||||
print(
|
+ Style.DIM
|
||||||
Back.BLACK
|
+ "│"
|
||||||
+ Fore.YELLOW
|
)
|
||||||
+ Style.DIM
|
print(
|
||||||
+ "│"
|
Back.BLACK
|
||||||
+ Fore.LIGHTYELLOW_EX
|
+ Fore.YELLOW
|
||||||
+ Style.NORMAL
|
+ Style.DIM
|
||||||
+ " ███████ ▄██████ "
|
+ "│"
|
||||||
+ Fore.YELLOW
|
+ Fore.LIGHTYELLOW_EX
|
||||||
+ Style.DIM
|
+ Style.NORMAL
|
||||||
+ "│"
|
+ " ████████████████████ "
|
||||||
)
|
+ Fore.YELLOW
|
||||||
print(
|
+ Style.DIM
|
||||||
Back.BLACK
|
+ "│"
|
||||||
+ Fore.YELLOW
|
)
|
||||||
+ Style.DIM
|
print(
|
||||||
+ "│"
|
Back.BLACK
|
||||||
+ Fore.LIGHTYELLOW_EX
|
+ Fore.YELLOW
|
||||||
+ Style.NORMAL
|
+ Style.DIM
|
||||||
+ " ███████ ▄█████████ "
|
+ "│"
|
||||||
+ Fore.YELLOW
|
+ Fore.LIGHTYELLOW_EX
|
||||||
+ Style.DIM
|
+ Style.NORMAL
|
||||||
+ "│"
|
+ " ▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀ "
|
||||||
)
|
+ Fore.YELLOW
|
||||||
print(
|
+ Style.DIM
|
||||||
Back.BLACK
|
+ "│"
|
||||||
+ Fore.YELLOW
|
)
|
||||||
+ Style.DIM
|
print(Back.BLACK + Fore.YELLOW + Style.DIM + "│ │")
|
||||||
+ "│"
|
left_spaces = (15 - len(dzutil.VERSION) - 1) // 2
|
||||||
+ Fore.LIGHTYELLOW_EX
|
right_spaces = left_spaces
|
||||||
+ Style.NORMAL
|
if left_spaces + len(dzutil.VERSION) + 1 + right_spaces < 15:
|
||||||
+ " ████████████████████ "
|
right_spaces += 1
|
||||||
+ Fore.YELLOW
|
print(
|
||||||
+ Style.DIM
|
Back.BLACK
|
||||||
+ "│"
|
+ Fore.YELLOW
|
||||||
)
|
+ Style.DIM
|
||||||
print(
|
+ "│"
|
||||||
Back.BLACK
|
+ Style.RESET_ALL
|
||||||
+ Fore.YELLOW
|
+ Back.BLACK
|
||||||
+ Style.DIM
|
+ Fore.LIGHTWHITE_EX
|
||||||
+ "│"
|
+ Style.BRIGHT
|
||||||
+ Fore.LIGHTYELLOW_EX
|
+ f"{' '*left_spaces}Dangerzone v{dzutil.VERSION}{' '*right_spaces}"
|
||||||
+ Style.NORMAL
|
+ Fore.YELLOW
|
||||||
+ " ▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀ "
|
+ Style.DIM
|
||||||
+ Fore.YELLOW
|
+ "│"
|
||||||
+ Style.DIM
|
)
|
||||||
+ "│"
|
print(
|
||||||
)
|
Back.BLACK
|
||||||
print(Back.BLACK + Fore.YELLOW + Style.DIM + "│ │")
|
+ Fore.YELLOW
|
||||||
left_spaces = (15 - len(dzutil.VERSION) - 1) // 2
|
+ Style.DIM
|
||||||
right_spaces = left_spaces
|
+ "│"
|
||||||
if left_spaces + len(dzutil.VERSION) + 1 + right_spaces < 15:
|
+ Style.RESET_ALL
|
||||||
right_spaces += 1
|
+ Back.BLACK
|
||||||
print(
|
+ Fore.LIGHTWHITE_EX
|
||||||
Back.BLACK
|
+ " https://dangerzone.rocks "
|
||||||
+ Fore.YELLOW
|
+ Fore.YELLOW
|
||||||
+ Style.DIM
|
+ Style.DIM
|
||||||
+ "│"
|
+ "│"
|
||||||
+ Style.RESET_ALL
|
)
|
||||||
+ Back.BLACK
|
print(Back.BLACK + Fore.YELLOW + Style.DIM + "╰──────────────────────────╯")
|
||||||
+ 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 + "╰──────────────────────────╯")
|
|
||||||
|
|
||||||
@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