diff --git a/dangerzone/global_common.py b/dangerzone/global_common.py index 7f47d83..03f9893 100644 --- a/dangerzone/global_common.py +++ b/dangerzone/global_common.py @@ -37,10 +37,6 @@ class GlobalCommon(object): # Container self.container_name = "dangerzone.rocks/dangerzone" - if platform.system() == "Linux": - self.container_runtime = shutil.which("podman") - else: - self.container_runtime = shutil.which("docker") # Languages supported by tesseract self.ocr_languages = { @@ -384,6 +380,12 @@ class GlobalCommon(object): ) print(Back.BLACK + Fore.YELLOW + Style.DIM + "╰──────────────────────────╯") + def get_container_runtime(self): + if platform.system() == "Linux": + return shutil.which("podman") + else: + return shutil.which("docker") + def get_resource_path(self, filename): if getattr(sys, "dangerzone_dev", False): # Look for resources directory relative to python file @@ -465,16 +467,19 @@ class GlobalCommon(object): Make sure the podman container is installed. Linux only. """ if self.is_container_installed(): - print("Dangerzone container is already installed") return # Load the container into podman print("Installing Dangerzone container...") - p = subprocess.Popen([self.container_runtime, "load"], stdin=subprocess.PIPE) + p = subprocess.Popen( + [self.get_container_runtime(), "load"], stdin=subprocess.PIPE + ) chunk_size = 1024 - compressed_container_path = self.get_resource_path("dangerzone-converter.tar.gz") + compressed_container_path = self.get_resource_path( + "dangerzone-converter.tar.gz" + ) with gzip.open(compressed_container_path) as f: while True: chunk = f.read(chunk_size) @@ -495,8 +500,6 @@ class GlobalCommon(object): """ See if the podman container is installed. Linux only. """ - print("Checking if container is already installed") - # Get the image id with open(self.get_resource_path("image-id.txt")) as f: expected_image_id = f.read().strip() @@ -507,7 +510,9 @@ class GlobalCommon(object): if platform.system() == "Linux": # Podman images = json.loads( - subprocess.check_output([self.container_runtime, "image", "list", "--format", "json"]) + subprocess.check_output( + [self.get_container_runtime(), "image", "list", "--format", "json"] + ) ) for image in images: if image["Id"] == expected_image_id: @@ -515,10 +520,28 @@ class GlobalCommon(object): break else: # Docker - found_image_id = subprocess.check_output([self.container_runtime, "image", "list", "--format", "{{.ID}}", self.container_name], text=True) - if found_image_id.strip() == expected_image_id: + found_image_id = subprocess.check_output( + [ + self.get_container_runtime(), + "image", + "list", + "--format", + "{{.ID}}", + self.container_name, + ], + text=True, + ) + found_image_id = found_image_id.strip() + if found_image_id == expected_image_id: installed = True + elif found_image_id == "": + print("Dangerzone container image is not installed") else: print(f"Image {found_image_id} is installed, not {expected_image_id}") + # Delete the image that exists + subprocess.check_output( + [self.get_container_runtime(), "rmi", found_image_id] + ) + return installed diff --git a/dangerzone/gui/main_window.py b/dangerzone/gui/main_window.py index 06f7603..d6fb6f4 100644 --- a/dangerzone/gui/main_window.py +++ b/dangerzone/gui/main_window.py @@ -135,6 +135,7 @@ class WaitingWidget(QtWidgets.QWidget): layout = QtWidgets.QVBoxLayout() layout.addStretch() layout.addWidget(self.label) + layout.addStretch() layout.addWidget(self.buttons) layout.addStretch() self.setLayout(layout) @@ -150,41 +151,43 @@ class WaitingWidget(QtWidgets.QWidget): container_runtime = shutil.which("podman") else: container_runtime = shutil.which("docker") - + if container_runtime is None: print("Docker is not installed") state = "not_installed" else: # Can we run `docker image ls` without an error - with subprocess.Popen([container_runtime, "image", "ls"]) as p: + with subprocess.Popen( + [container_runtime, "image", "ls"], + stdout=subprocess.DEVNULL, + stderr=subprocess.DEVNULL, + ) as p: p.communicate() if p.returncode != 0: print("Docker is not running") state = "not_running" else: # Always try installing the container - print("Ensuring the container is installed") state = "install_container" - + # Update the state - print(f"Dangerzone state: {state}") self.state_change(state) def state_change(self, state): if state == "not_installed": self.label.setText( - "Dangerzone requires Docker

Download Docker Desktop and install it." + "Dangerzone Requires Docker Desktop

Download Docker Desktop, install it, and open it." ) self.buttons.show() elif state == "not_running": self.label.setText( - "Docker Desktop is installed, but you must launch it first. Open Docker and make sure it's running in the background." + "Dangerzone Requires Docker Desktop

Docker is installed but isn't running.

Open Docker and make sure it's running in the background." ) self.buttons.show() else: self.label.setText( - "Installing the Dangerzone container..." + "Installing the Dangerzone container image.

This might take a few minutes..." ) self.buttons.hide() self.install_container_t = InstallContainerThread(self.global_common)