mirror of
https://github.com/freedomofpress/dangerzone.git
synced 2025-05-04 12:41:50 +02:00
Move is_container_installed and install_container to container module
This commit is contained in:
parent
c655d3a2ca
commit
1042c51974
4 changed files with 82 additions and 86 deletions
|
@ -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")
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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
|
||||
+ "│"
|
||||
|
|
Loading…
Reference in a new issue