Start implementing CLI args, and start with validating custom containers and pulling the latest container

This commit is contained in:
Micah Lee 2021-06-09 16:15:19 -07:00
parent 6ff68f88ea
commit 8aaf7ebcf1
No known key found for this signature in database
GPG key ID: 403C2657CD994F73
5 changed files with 87 additions and 24 deletions

View file

@ -1,8 +1,63 @@
import click 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.command()
@click.option("--custom-container") # Use this container instead of flmcode/dangerzone @click.option("--custom-container", help="Use a custom container")
@click.argument("filename", required=False) @click.option("--safe-pdf-filename", help="Default is filename ending with -safe.pdf")
def cli_main(custom_container, filename): @click.option("--ocr-lang", help="Language to OCR, defaults to none")
pass @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

View file

@ -274,3 +274,26 @@ class GlobalCommon(object):
return startupinfo return startupinfo
else: else:
return None 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

View file

@ -15,7 +15,6 @@ from .docker_installer import (
AuthorizationFailed, AuthorizationFailed,
) )
from ..global_common import GlobalCommon from ..global_common import GlobalCommon
from ..container import container_runtime
# For some reason, Dangerzone segfaults if I inherit from QApplication directly, so instead # For some reason, Dangerzone segfaults if I inherit from QApplication directly, so instead
@ -62,23 +61,9 @@ def gui_main(custom_container, filename):
gui_common = GuiCommon(app, global_common) gui_common = GuiCommon(app, global_common)
if custom_container: if custom_container:
# Do we have this container? success, error_message = global_common.container_exists(custom_container)
with global_common.exec_dangerzone_container( if not success:
["ls", "--container-name", custom_container] click.echo(error_message)
) 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 return
global_common.custom_container = custom_container global_common.custom_container = custom_container

View file

@ -5,7 +5,7 @@ import platform
import subprocess import subprocess
from PySide2 import QtCore, QtGui, QtWidgets from PySide2 import QtCore, QtGui, QtWidgets
from ..tasks import PullImageTask, ConvertToPixels, ConvertToPDF from .tasks import PullImageTask, ConvertToPixels, ConvertToPDF
class TasksWidget(QtWidgets.QWidget): class TasksWidget(QtWidgets.QWidget):