From b506e0222b83fadc8708fbb8adc47e96cb1ae2fc Mon Sep 17 00:00:00 2001 From: Pat Gunn Date: Fri, 31 May 2024 12:29:46 -0400 Subject: [PATCH 1/4] caimanmanager: Detect and adjust behaviour for editable installs --- caiman/caimanmanager.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/caiman/caimanmanager.py b/caiman/caimanmanager.py index c2d57b8c0..912ad9db0 100755 --- a/caiman/caimanmanager.py +++ b/caiman/caimanmanager.py @@ -3,6 +3,7 @@ import argparse import filecmp import glob +import json import os import platform import psutil @@ -52,6 +53,15 @@ def do_install_to(targdir: str, inplace: bool = False, force: bool = False) -> None: global sourcedir_base + + try: + import importlib_metadata + # A lot can change upstream with this code; I hope the APIs are stable, but just in case, make this best-effort + if json.loads(importlib_metadata.Distribution.from_name('caiman').read_text('direct_url.json'))['dir_info']['editable']: + inplace = True + except: + pass + ignore_pycache=shutil.ignore_patterns('__pycache__') if os.path.isdir(targdir) and not force: raise Exception(targdir + " already exists. You may move it out of the way, remove it, or use --force") From be70e72c7cb6b5bccd9af7c4605b4ed17e27c25c Mon Sep 17 00:00:00 2001 From: Pat Gunn Date: Fri, 31 May 2024 12:31:40 -0400 Subject: [PATCH 2/4] Improve messaging around editable installs --- caiman/caimanmanager.py | 1 + 1 file changed, 1 insertion(+) diff --git a/caiman/caimanmanager.py b/caiman/caimanmanager.py index 912ad9db0..067398ce7 100755 --- a/caiman/caimanmanager.py +++ b/caiman/caimanmanager.py @@ -55,6 +55,7 @@ def do_install_to(targdir: str, inplace: bool = False, force: bool = False) -> N global sourcedir_base try: + print("If you did an editable install (with -e), the install must happen within the source tree; otherwise, it must not") import importlib_metadata # A lot can change upstream with this code; I hope the APIs are stable, but just in case, make this best-effort if json.loads(importlib_metadata.Distribution.from_name('caiman').read_text('direct_url.json'))['dir_info']['editable']: From d0a5eb3a37094d1ab805247480220984925cf47f Mon Sep 17 00:00:00 2001 From: Pat Gunn Date: Fri, 31 May 2024 14:59:29 -0400 Subject: [PATCH 3/4] chdir for the user if needed --- caiman/caimanmanager.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/caiman/caimanmanager.py b/caiman/caimanmanager.py index 067398ce7..67e7af242 100755 --- a/caiman/caimanmanager.py +++ b/caiman/caimanmanager.py @@ -55,13 +55,16 @@ def do_install_to(targdir: str, inplace: bool = False, force: bool = False) -> N global sourcedir_base try: - print("If you did an editable install (with -e), the install must happen within the source tree; otherwise, it must not") + import importlib import importlib_metadata # A lot can change upstream with this code; I hope the APIs are stable, but just in case, make this best-effort if json.loads(importlib_metadata.Distribution.from_name('caiman').read_text('direct_url.json'))['dir_info']['editable']: inplace = True + cwd = os.getcwd() + os.chdir(str(importlib.resources.files('caiman').joinpath('..'))) + print(f"Used editable fallback, entered {os.getcwd()} directory") except: - pass + print("Did not use editable fallback") ignore_pycache=shutil.ignore_patterns('__pycache__') if os.path.isdir(targdir) and not force: @@ -88,6 +91,8 @@ def do_install_to(targdir: str, inplace: bool = False, force: bool = False) -> N with open(os.path.join(targdir, 'RELEASE'), 'w') as verfile_fh: print(f"Version:{caiman.__version__}", file=verfile_fh) print("Installed " + targdir) + if cwd is not None: + os.chdir(cwd) def do_check_install(targdir: str, inplace: bool = False) -> None: From ddca9828f3c9f52ee3911a3afb70bf757c3ad684 Mon Sep 17 00:00:00 2001 From: Pat Gunn Date: Fri, 31 May 2024 15:05:59 -0400 Subject: [PATCH 4/4] caimanmanager: "is None" still can't be used on a variable if it was created in a branch, so assign =None --- caiman/caimanmanager.py | 1 + 1 file changed, 1 insertion(+) diff --git a/caiman/caimanmanager.py b/caiman/caimanmanager.py index 67e7af242..62580930c 100755 --- a/caiman/caimanmanager.py +++ b/caiman/caimanmanager.py @@ -53,6 +53,7 @@ def do_install_to(targdir: str, inplace: bool = False, force: bool = False) -> None: global sourcedir_base + cwd = None # Assigning so it exists to avoid UnboundLocalError try: import importlib