refactor root directory detection to be cleaner

This commit is contained in:
Lincoln Stein 2023-07-31 22:27:07 -04:00
parent 7cd8b2f207
commit 72ebe2ce68
2 changed files with 16 additions and 2 deletions

View File

@ -356,7 +356,6 @@ def _find_root() -> Path:
venv = Path(os.environ.get("VIRTUAL_ENV") or ".")
if os.environ.get("INVOKEAI_ROOT"):
root = Path(os.environ.get("INVOKEAI_ROOT")).resolve()
os.environ["INVOKEAI_ROOT"] = str(root) # absolutize it to protect against code doing a cwd()
elif any([(venv.parent / x).exists() for x in [INIT_FILE, LEGACY_INIT_FILE]]):
root = (venv.parent).resolve()
else:
@ -403,7 +402,7 @@ class InvokeAIAppConfig(InvokeAISettings):
xformers_enabled : bool = Field(default=True, description="Enable/disable memory-efficient attention", category='Memory/Performance')
tiled_decode : bool = Field(default=False, description="Whether to enable tiled VAE decode (reduces memory consumption with some performance penalty)", category='Memory/Performance')
root : Path = Field(default=_find_root(), description='InvokeAI runtime root directory', category='Paths')
root : Path = Field(default=None, description='InvokeAI runtime root directory', category='Paths')
autoimport_dir : Path = Field(default='autoimport', description='Path to a directory of models files to be imported on startup.', category='Paths')
lora_dir : Path = Field(default=None, description='Path to a directory of LoRA/LyCORIS models to be imported on startup.', category='Paths')
embedding_dir : Path = Field(default=None, description='Path to a directory of Textual Inversion embeddings to be imported on startup.', category='Paths')
@ -475,6 +474,7 @@ class InvokeAIAppConfig(InvokeAISettings):
root = Path(self.root).expanduser().absolute()
else:
root = self.find_root()
self.root = root # insulate ourselves from relative paths that may change
return root
@property

View File

@ -84,6 +84,20 @@ def test_env_override():
assert conf.max_cache_size == 20
def test_root_resists_cwd():
previous = os.environ["INVOKEAI_ROOT"]
cwd = Path(os.getcwd()).resolve()
os.environ["INVOKEAI_ROOT"] = "."
conf = InvokeAIAppConfig.get_config()
conf.parse_args([])
assert conf.root_path == cwd
os.chdir(previous)
assert conf.root_path == cwd
os.environ["INVOKEAI_ROOT"] = previous
def test_type_coercion():
conf = InvokeAIAppConfig().get_config()
conf.parse_args(argv=["--root=/tmp/foobar"])