diff --git a/dangerzone/cli.py b/dangerzone/cli.py index 7265bc9..2b6b385 100644 --- a/dangerzone/cli.py +++ b/dangerzone/cli.py @@ -1,8 +1,63 @@ import click +from .global_common import GlobalCommon + + +def exec_container(global_common, args): + output = "" + + with global_common.exec_dangerzone_container(args) as p: + for line in p.stdout: + output += line.decode() + print(line.decode(), end="") + + stderr = p.stderr.read().decode() + print(stderr) + + if p.returncode == 126 or p.returncode == 127: + click.echo(f"Authorization failed") + elif p.returncode == 0: + click.echo(f"Return code: {p.returncode}") + + print("") + return p.returncode, output, stderr + @click.command() -@click.option("--custom-container") # Use this container instead of flmcode/dangerzone -@click.argument("filename", required=False) -def cli_main(custom_container, filename): - pass +@click.option("--custom-container", help="Use a custom container") +@click.option("--safe-pdf-filename", help="Default is filename ending with -safe.pdf") +@click.option("--ocr-lang", help="Language to OCR, defaults to none") +@click.option( + "--skip-update", + is_flag=True, + help="Don't update flmcode/dangerzone container", +) +@click.argument("filename", required=True) +def cli_main(custom_container, safe_pdf_filename, ocr_lang, skip_update, filename): + global_common = GlobalCommon() + + # Make sure custom container exists + if custom_container: + success, error_message = global_common.container_exists(custom_container) + if not success: + click.echo(error_message) + return + + global_common.custom_container = custom_container + else: + if skip_update: + # Make sure flmcode/dangerzone exists + success, error_message = global_common.container_exists( + "flmcode/dangerzone" + ) + if not success: + click.echo( + "You don't have the flmcode/dangerzone container so you can't use --skip-update" + ) + return + + if not skip_update: + click.echo("Pulling container image (this might take a few minutes)") + returncode, _, _ = exec_container(global_common, ["pull"]) + if returncode != 0: + return diff --git a/dangerzone/global_common.py b/dangerzone/global_common.py index 084e79f..ef0440c 100644 --- a/dangerzone/global_common.py +++ b/dangerzone/global_common.py @@ -274,3 +274,26 @@ class GlobalCommon(object): return startupinfo else: return None + + def container_exists(self, container_name): + # Do we have this container? + with self.exec_dangerzone_container( + ["ls", "--container-name", container_name] + ) as p: + stdout_data, _ = p.communicate() + if stdout_data.startswith(b"Executing: "): + stdout_data = b"\n".join(stdout_data.split(b"\n")[1:]) + + # The user canceled, or permission denied + if p.returncode == 126 or p.returncode == 127: + return False, "Authorization failed" + return + elif p.returncode != 0: + return False, "Container error" + return + + # Check the output + if container_name.encode() not in stdout_data: + return False, f"Container '{container_name}' not found" + + return True, True diff --git a/dangerzone/gui/__init__.py b/dangerzone/gui/__init__.py index a2c59a8..65ccddd 100644 --- a/dangerzone/gui/__init__.py +++ b/dangerzone/gui/__init__.py @@ -15,7 +15,6 @@ from .docker_installer import ( AuthorizationFailed, ) from ..global_common import GlobalCommon -from ..container import container_runtime # For some reason, Dangerzone segfaults if I inherit from QApplication directly, so instead @@ -62,24 +61,10 @@ def gui_main(custom_container, filename): gui_common = GuiCommon(app, global_common) if custom_container: - # Do we have this container? - with global_common.exec_dangerzone_container( - ["ls", "--container-name", custom_container] - ) as p: - stdout_data, stderr_data = p.communicate() - - # The user canceled, or permission denied - if p.returncode == 126 or p.returncode == 127: - click.echo("Authorization failed") - return - elif p.returncode != 0: - click.echo("Container error") - return - - # Check the output - if custom_container.encode() not in stdout_data: - click.echo(f"Container '{custom_container}' not found") - return + success, error_message = global_common.container_exists(custom_container) + if not success: + click.echo(error_message) + return global_common.custom_container = custom_container diff --git a/dangerzone/tasks.py b/dangerzone/gui/tasks.py similarity index 100% rename from dangerzone/tasks.py rename to dangerzone/gui/tasks.py diff --git a/dangerzone/gui/tasks_widget.py b/dangerzone/gui/tasks_widget.py index ab77f47..94a00a4 100644 --- a/dangerzone/gui/tasks_widget.py +++ b/dangerzone/gui/tasks_widget.py @@ -5,7 +5,7 @@ import platform import subprocess from PySide2 import QtCore, QtGui, QtWidgets -from ..tasks import PullImageTask, ConvertToPixels, ConvertToPDF +from .tasks import PullImageTask, ConvertToPixels, ConvertToPDF class TasksWidget(QtWidgets.QWidget):