-
Notifications
You must be signed in to change notification settings - Fork 29
[feat] New posydon-setup-env command
#714
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
sgossage
wants to merge
3
commits into
v2.2
Choose a base branch
from
sg_install_script
base: v2.2
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| from posydon.utils.install import _run_install | ||
|
|
||
| if __name__ == '__main__': | ||
| _run_install() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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}") | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is specific to Linux. We should run a test for other OS's |
||
| 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/" | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here - check for different OS's |
||
| 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}") | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we come up with a user input here?