mirror of
https://github.com/freedomofpress/dangerzone.git
synced 2025-05-04 20:51:49 +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
|
return
|
||||||
|
|
||||||
# Ensure container is installed
|
# Ensure container is installed
|
||||||
dzutil.install_container()
|
container.install_container()
|
||||||
|
|
||||||
# Convert the document
|
# Convert the document
|
||||||
print_header("Converting document to safe PDF")
|
print_header("Converting document to safe PDF")
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import gzip
|
||||||
|
import platform
|
||||||
import subprocess
|
import subprocess
|
||||||
import pipes
|
import pipes
|
||||||
import shutil
|
import shutil
|
||||||
|
@ -188,3 +190,78 @@ def convert(
|
||||||
# return False, f"Page {i} has an invalid RGB file size"
|
# return False, f"Page {i} has an invalid RGB file size"
|
||||||
|
|
||||||
# return True, True
|
# 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
|
import dangerzone.util as dzutil
|
||||||
from dangerzone.gui import GuiCommon
|
from dangerzone.gui import GuiCommon
|
||||||
from dangerzone.container import convert
|
from dangerzone import container
|
||||||
from dangerzone.util import install_container
|
|
||||||
|
|
||||||
|
|
||||||
class MainWindow(QtWidgets.QMainWindow):
|
class MainWindow(QtWidgets.QMainWindow):
|
||||||
|
@ -91,7 +90,7 @@ class InstallContainerThread(QtCore.QThread):
|
||||||
super(InstallContainerThread, self).__init__()
|
super(InstallContainerThread, self).__init__()
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
install_container()
|
container.install_container()
|
||||||
self.finished.emit()
|
self.finished.emit()
|
||||||
|
|
||||||
|
|
||||||
|
@ -483,7 +482,7 @@ class ConvertThread(QtCore.QThread):
|
||||||
else:
|
else:
|
||||||
ocr_lang = None
|
ocr_lang = None
|
||||||
|
|
||||||
if convert(
|
if container.convert(
|
||||||
self.common.input_filename,
|
self.common.input_filename,
|
||||||
self.common.output_filename,
|
self.common.output_filename,
|
||||||
ocr_lang,
|
ocr_lang,
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
import gzip
|
|
||||||
import inspect
|
import inspect
|
||||||
import os
|
import os
|
||||||
import pathlib
|
import pathlib
|
||||||
|
@ -14,8 +13,6 @@ import appdirs
|
||||||
# then it belongs here.
|
# then it belongs here.
|
||||||
from colorama import Back, Fore, Style
|
from colorama import Back, Fore, Style
|
||||||
|
|
||||||
import dangerzone
|
|
||||||
|
|
||||||
SYSTEM = platform.system()
|
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():
|
def display_banner():
|
||||||
"""
|
"""
|
||||||
Raw ASCII art example:
|
Raw ASCII art example:
|
||||||
|
|
Loading…
Reference in a new issue