Skip to content

Commit

Permalink
add environment activation command to diagnostics (#71)
Browse files Browse the repository at this point in the history
* try to detect enviro

* fix comment wrap
  • Loading branch information
shapiromatron authored Nov 6, 2024
1 parent a19f69c commit f1d4b4f
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 2 deletions.
31 changes: 29 additions & 2 deletions bmds_ui/desktop/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -172,15 +175,39 @@ 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,
{
"prerelease_url": PRERELEASE_URL,
"show_prerelease": show_prerelease,
"python_path": python_path,
"env_type": env_type,
"env": env,
},
)

Expand Down
9 changes: 9 additions & 0 deletions bmds_ui/desktop/templates/manager-bat.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
9 changes: 9 additions & 0 deletions bmds_ui/desktop/templates/manager-sh.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit f1d4b4f

Please sign in to comment.