From f1d4b4fce1969f1fdbd77d89e0fda4a48f2ba0ca Mon Sep 17 00:00:00 2001 From: Andy Shapiro Date: Wed, 6 Nov 2024 17:54:19 -0500 Subject: [PATCH] add environment activation command to diagnostics (#71) * try to detect enviro * fix comment wrap --- bmds_ui/desktop/actions.py | 31 +++++++++++++++++++++-- bmds_ui/desktop/templates/manager-bat.txt | 9 +++++++ bmds_ui/desktop/templates/manager-sh.txt | 9 +++++++ 3 files changed, 47 insertions(+), 2 deletions(-) diff --git a/bmds_ui/desktop/actions.py b/bmds_ui/desktop/actions.py index 0eb4657..5533606 100644 --- a/bmds_ui/desktop/actions.py +++ b/bmds_ui/desktop/actions.py @@ -31,8 +31,11 @@ def sync_persistent_data(): - """Sync persistent data to database and static file path. We do this every time a database - is created or an application starts, to make sure application state is consistent with files.""" + """Sync persistent data to database and static file path. + + We do this every time a database is created or an application starts, to make sure application + state is consistent with files. + """ call_command("collectstatic", interactive=False, verbosity=3, stdout=stream, stderr=stream) call_command("migrate", interactive=False, verbosity=3, stdout=stream, stderr=stream) @@ -172,8 +175,30 @@ def render_template(template_text: str, context: dict) -> str: return template.render(Context(context)) +def get_activate_script() -> tuple[str, str]: + """Try to determine how to activate the environment. + + First check if we're in a python virtual environment with an activate script, next try to + determine if we're in a conda environment. If neither, return unknown. + + Returns: + tuple[str, str]: (environment_type {venv, conda, unknown}, path/name) + """ + python_path = Path(sys.executable) + bin_path = python_path.parent + if (bin_path / "activate").exists(): + return "venv", str(bin_path / "activate") + elif (bin_path / "activate.bat").exists(): + return "venv", str(bin_path / "activate.bat") + elif "CONDA_PREFIX" in os.environ and Path(os.environ["CONDA_PREFIX"]).exists(): + return "conda", Path(os.environ["CONDA_PREFIX"]).name + else: + return "unknown", "" + + def write_startup_script(template: str) -> str: python_path = Path(sys.executable) + env_type, env = get_activate_script() show_prerelease = get_installed_version().is_prerelease return render_template( template, @@ -181,6 +206,8 @@ def write_startup_script(template: str) -> str: "prerelease_url": PRERELEASE_URL, "show_prerelease": show_prerelease, "python_path": python_path, + "env_type": env_type, + "env": env, }, ) diff --git a/bmds_ui/desktop/templates/manager-bat.txt b/bmds_ui/desktop/templates/manager-bat.txt index f2f1225..1988942 100644 --- a/bmds_ui/desktop/templates/manager-bat.txt +++ b/bmds_ui/desktop/templates/manager-bat.txt @@ -75,6 +75,15 @@ echo: echo Python Path: echo {{ python_path }} echo: +{% if env_type == "venv" %} +echo To activate your environment, open a new terminal and run: +echo {{env}} +echo: +{% elif env_type == "conda" %} +echo To activate your environment, open a new terminal and run: +echo conda activate {{env}} +echo: +{% endif %} echo BMDS UI + pybmds Version: "{{ python_path }}" -m pip show bmds-ui pybmds echo: diff --git a/bmds_ui/desktop/templates/manager-sh.txt b/bmds_ui/desktop/templates/manager-sh.txt index daeb01c..f5935bf 100644 --- a/bmds_ui/desktop/templates/manager-sh.txt +++ b/bmds_ui/desktop/templates/manager-sh.txt @@ -74,6 +74,15 @@ echo echo "Python Path:" echo "{{ python_path }}" echo +{% if env_type == "venv" %} +echo To activate your environment, open a new terminal and run: +echo "source \"{{env}}\"" +echo +{% elif env_type == "conda" %} +echo To activate your environment, open a new terminal and run: +echo "conda activate {{env}}" +echo +{% endif %} echo "BMDS UI + pybmds Version:" "{{ python_path }}" -m pip show bmds-ui pybmds echo