From 2775b367103bc273b6b0036f3db8978c705ea4a9 Mon Sep 17 00:00:00 2001 From: Seth Gossage Date: Fri, 3 Oct 2025 14:12:25 -0500 Subject: [PATCH 1/2] adding conda env setup command --- bin/posydon-setup-env | 4 ++ posydon/utils/install.py | 102 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 106 insertions(+) create mode 100644 bin/posydon-setup-env create mode 100644 posydon/utils/install.py diff --git a/bin/posydon-setup-env b/bin/posydon-setup-env new file mode 100644 index 0000000000..646543270c --- /dev/null +++ b/bin/posydon-setup-env @@ -0,0 +1,4 @@ +from posydon.utils.install import _run_install + +if __name__ == '__main__': + _run_install() \ No newline at end of file diff --git a/posydon/utils/install.py b/posydon/utils/install.py new file mode 100644 index 0000000000..0d17fb594b --- /dev/null +++ b/posydon/utils/install.py @@ -0,0 +1,102 @@ +import os +import subprocess +from pathlib import Path + +HOME = Path.home() +MINICONDA_DIR = HOME / "miniconda" +MINICONDA_SCRIPT = HOME / "miniconda.sh" +ENV_NAME = "posydon_env" +DATA_DIR = "/projects/e33022/POSYDON-shared/data" +ENV_VARS_DIR = HOME / "env_vars" +DOTENV_FILE = ENV_VARS_DIR / f"{ENV_NAME}.env" +IPYCONFIG_PATH = HOME / ".ipython" / "profile_default" / "ipython_kernel_config.py" + +def run(cmd, check=True, shell=True, **kwargs): + """Run a shell command.""" + print(f"Running: {cmd}") + subprocess.run(cmd, shell=shell, check=check, **kwargs) + +def install_miniconda(): + if not MINICONDA_DIR.exists(): + print("šŸ“¦ Installing Miniconda...") + run(f"curl https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh --output {MINICONDA_SCRIPT}") + run(f"bash {MINICONDA_SCRIPT} -b -p {MINICONDA_DIR}") + MINICONDA_SCRIPT.unlink() + +def activate_conda(): + print("⚔ Activating all shells for conda usage...") + os.environ["PATH"] = f"{MINICONDA_DIR / 'bin'}:{os.environ['PATH']}" + run("conda init --all") + bashrc = HOME / ".bashrc" + if bashrc.exists(): + run(f"source {bashrc}", shell=True, executable="/bin/bash") + +def create_env_and_kernel(): + print("šŸ Creating conda environment and IPython kernel with POSYDON installed...") + run("conda --version") + run("conda tos accept --override-channels --channel https://repo.anaconda.com/pkgs/main") + run("conda tos accept --override-channels --channel https://repo.anaconda.com/pkgs/r") + run("conda config --add channels posydon") + + run(f"conda create --name {ENV_NAME} -c conda-forge -c posydon python=3.11 git posydon ipykernel --yes") + + activate_cmd = f"conda activate {ENV_NAME} && python -c \"import posydon; print(posydon.__version__)\"" + result = subprocess.run(activate_cmd, shell=True, capture_output=True, text=True, executable="/bin/bash") + version = result.stdout.strip() + + run(f"conda activate {ENV_NAME} && python -m ipykernel install --user --name {ENV_NAME} --display-name \"POSYDON ({version})\"", executable="/bin/bash") + + return version + +def set_conda_env_vars(posydon_dir): + run(f"conda env config vars set PATH_TO_POSYDON={posydon_dir} --name {ENV_NAME}") + run(f"conda env config vars set PATH_TO_POSYDON_DATA={DATA_DIR} --name {ENV_NAME}") + +def setup_dotenv(posydon_dir): + ENV_VARS_DIR.mkdir(parents=True, exist_ok=True) + if DOTENV_FILE.exists(): + DOTENV_FILE.unlink() + with open(DOTENV_FILE, "w") as f: + f.write(f"PATH_TO_POSYDON={posydon_dir}\n") + f.write(f"PATH_TO_POSYDON_DATA={DATA_DIR}\n") + +def update_bashrc(): + bashrc = HOME / ".bashrc" + conda_cmd = f"conda activate {ENV_NAME}" + if bashrc.exists(): + with open(bashrc, "r") as f: + lines = f.read().splitlines() + if conda_cmd not in lines: + with open(bashrc, "a") as f: + f.write(f"\n{conda_cmd}\n") + +def setup_ipython_config(posydon_dir): + if IPYCONFIG_PATH.exists(): + IPYCONFIG_PATH.unlink() + run("ipython profile create") + tex_path = "/software/texlive/2020/bin/x86_64-linux/" + ipy_config_line = ( + 'c.InteractiveShellApp.exec_lines = [' + f'"import os; os.environ[\'PATH\'] += \':{tex_path}\'; ' + f'os.environ[\'PATH_TO_POSYDON\'] = \'{posydon_dir}\'; ' + f'os.environ[\'PATH_TO_POSYDON_DATA\'] = \'{DATA_DIR}\'"]' + ) + with open(IPYCONFIG_PATH, "a") as f: + f.write(ipy_config_line + "\n") + +def get_posydon_dir(): + cmd = f"conda activate {ENV_NAME} && python -c \"import posydon; print(posydon.__file__.split('/posydon/')[0])\"" + result = subprocess.run(cmd, shell=True, capture_output=True, text=True, executable="/bin/bash") + return result.stdout.strip() + +def _run_install(): + install_miniconda() + activate_conda() + version = create_env_and_kernel() + posydon_dir = get_posydon_dir() + set_conda_env_vars(posydon_dir) + setup_dotenv(posydon_dir) + update_bashrc() + setup_ipython_config(posydon_dir) + + print(f"\nāœ… Successfully installed POSYDON version {version}") From d90c722ed545f089123b77e4a2436e86aa98069d Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 9 Oct 2025 05:06:28 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- bin/posydon-setup-env | 2 +- posydon/utils/install.py | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/bin/posydon-setup-env b/bin/posydon-setup-env index 646543270c..c20c9b1d6f 100644 --- a/bin/posydon-setup-env +++ b/bin/posydon-setup-env @@ -1,4 +1,4 @@ from posydon.utils.install import _run_install if __name__ == '__main__': - _run_install() \ No newline at end of file + _run_install() diff --git a/posydon/utils/install.py b/posydon/utils/install.py index 0d17fb594b..5ab3ea96d1 100644 --- a/posydon/utils/install.py +++ b/posydon/utils/install.py @@ -37,15 +37,15 @@ def create_env_and_kernel(): run("conda tos accept --override-channels --channel https://repo.anaconda.com/pkgs/main") run("conda tos accept --override-channels --channel https://repo.anaconda.com/pkgs/r") run("conda config --add channels posydon") - + run(f"conda create --name {ENV_NAME} -c conda-forge -c posydon python=3.11 git posydon ipykernel --yes") - + activate_cmd = f"conda activate {ENV_NAME} && python -c \"import posydon; print(posydon.__version__)\"" result = subprocess.run(activate_cmd, shell=True, capture_output=True, text=True, executable="/bin/bash") version = result.stdout.strip() - + run(f"conda activate {ENV_NAME} && python -m ipykernel install --user --name {ENV_NAME} --display-name \"POSYDON ({version})\"", executable="/bin/bash") - + return version def set_conda_env_vars(posydon_dir): @@ -98,5 +98,5 @@ def _run_install(): setup_dotenv(posydon_dir) update_bashrc() setup_ipython_config(posydon_dir) - + print(f"\nāœ… Successfully installed POSYDON version {version}")