Skip to content

Commit

Permalink
Refuse to upgrade or install into existing directories which exist an…
Browse files Browse the repository at this point in the history
…d are not empty #152

This includes directories with existing Comfy that was not previously installed by the plugin
  • Loading branch information
Acly committed Nov 30, 2023
1 parent 3a0ea6b commit 770d8ce
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 28 deletions.
2 changes: 1 addition & 1 deletion ai_diffusion/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Generative AI plugin for Krita using Stable Diffusion"""

__version__ = "1.8.1"
__version__ = "1.8.2"

import importlib.util

Expand Down
33 changes: 14 additions & 19 deletions ai_diffusion/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,22 +74,10 @@ def check_install(self):
self.version = None

comfy_pkg = ["main.py", "nodes.py", "custom_nodes"]
self.comfy_dir = _find_component(comfy_pkg, [self.path, self.path / "ComfyUI"])
self.comfy_dir = _find_component(comfy_pkg, [self.path / "ComfyUI"])

python_pkg = ["python3.dll", "python.exe"] if is_windows else ["python3", "pip3"]
python_search_paths = [
self.path / "python",
self.path / "venv" / "bin",
self.path / ".venv" / "bin",
]
if self.comfy_dir:
python_search_paths += [
self.comfy_dir / "python",
self.comfy_dir / "venv" / "bin",
self.comfy_dir / ".venv" / "bin",
self.comfy_dir.parent / "python",
self.comfy_dir.parent / "python_embeded",
]
python_search_paths = [self.path / "python", self.path / "venv" / "bin"]
python_path = _find_component(python_pkg, python_search_paths)
if python_path is None:
self._python_cmd = _find_program("python3", "python")
Expand Down Expand Up @@ -341,9 +329,10 @@ def info(message: str):
self.comfy_dir = None
await self.install(callback)
except Exception as e:
log.warning(f"Error during upgrade: {str(e)} - Restoring {upgrade_comfy_dir}")
shutil.rmtree(comfy_dir, ignore_errors=True)
shutil.move(upgrade_comfy_dir, comfy_dir)
if upgrade_comfy_dir.exists():
log.warning(f"Error during upgrade: {str(e)} - Restoring {upgrade_comfy_dir}")
safe_remove_dir(comfy_dir)
shutil.move(upgrade_comfy_dir, comfy_dir)
raise e

try:
Expand Down Expand Up @@ -472,10 +461,16 @@ def is_installed(self, package: str | ModelResource | CustomNode):
def all_installed(self, packages: list[str] | list[ModelResource] | list[CustomNode]):
return all(self.is_installed(p) for p in packages)

@property
def can_install(self):
return not self.path.exists() or (self.path.is_dir() and not any(self.path.iterdir()))

@property
def upgrade_required(self):
return self.state is not ServerState.not_installed and (
self.version is None or self.version != resources.version
return (
self.state is not ServerState.not_installed
and self.version is not None
and self.version != resources.version
)


Expand Down
12 changes: 5 additions & 7 deletions ai_diffusion/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

from . import util


class ServerMode(Enum):
undefined = -1
managed = 0
Expand All @@ -31,6 +32,7 @@ def default():
else:
return ServerBackend.cuda


class PerformancePreset(Enum):
auto = "Automatic"
cpu = "CPU"
Expand Down Expand Up @@ -71,10 +73,8 @@ class Settings(QObject):
_server_path = Setting(
"Server Path",
str(Path(__file__).parent / ".server"),
(
"Directory where ComfyUI is installed. At least 10GB of free disk space is required"
" for a full installation."
),
"Directory where ComfyUI will be installed. At least 10GB of free disk space is required"
" for a full installation.",
)

server_url: str
Expand Down Expand Up @@ -126,9 +126,7 @@ class Settings(QObject):
)

show_control_end: bool
_show_control_end = Setting(
"Control ending step", False, "Show control ending step ratio"
)
_show_control_end = Setting("Control ending step", False, "Show control ending step ratio")

history_size: int
_history_size = Setting(
Expand Down
10 changes: 9 additions & 1 deletion ai_diffusion/ui/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,15 @@ def update(self):

if self.requires_install:
self._launch_button.setText("Install")
self._launch_button.setEnabled(True)
if not self._server.version and not self._server.can_install:
self._status_label.setText(
"Invalid location: directory is not empty, but no previous installation was"
" found"
)
self._status_label.setStyleSheet(f"color:{red};font-weight:bold")
self._launch_button.setEnabled(False)
else:
self._launch_button.setEnabled(True)

if self._error:
self._status_label.setText(f"<b>Error:</b> {self._error}")
Expand Down

0 comments on commit 770d8ce

Please sign in to comment.