Skip to content

Commit

Permalink
try to detect enviro
Browse files Browse the repository at this point in the history
  • Loading branch information
shapiromatron committed Nov 5, 2024
1 parent a19f69c commit 58e21db
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
23 changes: 23 additions & 0 deletions bmds_ui/desktop/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,15 +172,38 @@ 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 58e21db

Please sign in to comment.