From 28612c899a02d4e95e40302d7fc71f94dfd85486 Mon Sep 17 00:00:00 2001 From: Lincoln Stein Date: Thu, 23 Feb 2023 10:15:01 -0500 Subject: [PATCH] add a sanity check to root directory finding algorithm Root directory finding algorithm is: 2) use --root argument 2) use INVOKEAI_ROOT environment variable 3) use VIRTUAL_ENV environment variable 4) use ~/invokeai Since developer's are liable to put virtual environments in their favorite places, not necessarily in the invokeai root directory, this PR adds a sanity check that looks for the existence of VIRTUAL_ENV/invokeai.init, and moves to (4) if not found. --- ldm/invoke/globals.py | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/ldm/invoke/globals.py b/ldm/invoke/globals.py index eeb69d266b..e47b5c059e 100644 --- a/ldm/invoke/globals.py +++ b/ldm/invoke/globals.py @@ -19,15 +19,7 @@ from typing import Union Globals = Namespace() -# This is usually overwritten by the command line and/or environment variables -if os.environ.get('INVOKEAI_ROOT'): - Globals.root = osp.abspath(os.environ.get('INVOKEAI_ROOT')) -elif os.environ.get('VIRTUAL_ENV'): - Globals.root = osp.abspath(osp.join(os.environ.get('VIRTUAL_ENV'), '..')) -else: - Globals.root = osp.abspath(osp.expanduser('~/invokeai')) - -# Where to look for the initialization file +# Where to look for the initialization file and other key components Globals.initfile = 'invokeai.init' Globals.models_file = 'models.yaml' Globals.models_dir = 'models' @@ -35,6 +27,20 @@ Globals.config_dir = 'configs' Globals.autoscan_dir = 'weights' Globals.converted_ckpts_dir = 'converted_ckpts' +# Set the default root directory. This can be overwritten by explicitly +# passing the `--root ` argument on the command line. +# logic is: +# 1) use INVOKEAI_ROOT environment variable (no check for this being a valid directory) +# 2) use VIRTUAL_ENV environment variable, with a check for initfile being there +# 3) use ~/invokeai + +if os.environ.get('INVOKEAI_ROOT'): + Globals.root = osp.abspath(os.environ.get('INVOKEAI_ROOT')) +elif os.environ.get('VIRTUAL_ENV') and Path(os.environ.get('VIRTUAL_ENV'),'..',Globals.initfile).exists(): + Globals.root = osp.abspath(osp.join(os.environ.get('VIRTUAL_ENV'), '..')) +else: + Globals.root = osp.abspath(osp.expanduser('~/invokeai')) + # Try loading patchmatch Globals.try_patchmatch = True