Skip to content
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

Add env variable and pre kernel launch logic #40

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 36 additions & 8 deletions condacolab.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ def install_from_url(
installer_url: AnyStr,
prefix: os.PathLike = PREFIX,
env: Dict[AnyStr, AnyStr] = None,
pre_conda: str = None,
run_checks: bool = True,
restart_kernel: bool = True,
):
Expand All @@ -114,6 +115,9 @@ def install_from_url(
For example, a value with spaces should be passed as::

env={"VAR": '"a value with spaces"'}
pre_conda
Shell script to run before activating the conda base environment.
Accepts a file path or a string with the contents.
run_checks
Run checks to see if installation was run previously.
Change to False to ignore checks and always attempt
Expand Down Expand Up @@ -198,13 +202,25 @@ def install_from_url(

env = env or {}
bin_path = f"{prefix}/bin"
pre_conda_contents = ""

if env:
pre_conda_contents = "".join(f'export {key}={value}\n' for key, value in env.items())
ssurbhi560 marked this conversation as resolved.
Show resolved Hide resolved

if pre_conda:
if os.path.isfile(pre_conda):
with open(pre_conda, "r") as f:
pre_conda_contents += f.read()
else:
pre_conda_contents += str(pre_conda)

os.rename(sys.executable, f"{sys.executable}.renamed_by_condacolab.bak")
with open(sys.executable, "w") as f:
f.write(
dedent(
f"""
#!/bin/bash
{pre_conda_contents}
source {prefix}/etc/profile.d/conda.sh
ssurbhi560 marked this conversation as resolved.
Show resolved Hide resolved
conda activate
unset PYTHONPATH
Expand All @@ -231,7 +247,7 @@ def install_from_url(
print("🔁 Please restart kernel by clicking on Runtime > Restart runtime.")

def install_mambaforge(
prefix: os.PathLike = PREFIX, env: Dict[AnyStr, AnyStr] = None, run_checks: bool = True, restart_kernel: bool = True,
prefix: os.PathLike = PREFIX, env: Dict[AnyStr, AnyStr] = None, pre_conda: str = None, run_checks: bool = True, restart_kernel: bool = True,
):
"""
Install Mambaforge, built for Python 3.7.
Expand All @@ -256,21 +272,24 @@ def install_mambaforge(
For example, a value with spaces should be passed as::

env={"VAR": '"a value with spaces"'}
pre_conda
Shell script to run before activating the conda base environment.
Accepts a file path or a string with the contents.
run_checks
Run checks to see if installation was run previously.
Change to False to ignore checks and always attempt
to run the installation.
"""
installer_url = r"https://github.com/jaimergp/miniforge/releases/latest/download/Mambaforge-colab-Linux-x86_64.sh"
install_from_url(installer_url, prefix=prefix, env=env, run_checks=run_checks, restart_kernel=restart_kernel)
install_from_url(installer_url, prefix=prefix, env=env, pre_conda=pre_conda, run_checks=run_checks, restart_kernel=restart_kernel)


# Make mambaforge the default
install = install_mambaforge


def install_miniforge(
prefix: os.PathLike = PREFIX, env: Dict[AnyStr, AnyStr] = None, run_checks: bool = True, restart_kernel: bool = True,
prefix: os.PathLike = PREFIX, env: Dict[AnyStr, AnyStr] = None, pre_conda: str = None, run_checks: bool = True, restart_kernel: bool = True,
):
"""
Install Mambaforge, built for Python 3.7.
Expand All @@ -294,17 +313,20 @@ def install_miniforge(
For example, a value with spaces should be passed as::

env={"VAR": '"a value with spaces"'}
pre_conda
Shell script to run before activating the conda base environment.
Accepts a file path or a string with the contents.
run_checks
Run checks to see if installation was run previously.
Change to False to ignore checks and always attempt
to run the installation.
"""
installer_url = r"https://github.com/jaimergp/miniforge/releases/latest/download/Miniforge-colab-Linux-x86_64.sh"
install_from_url(installer_url, prefix=prefix, env=env, run_checks=run_checks, restart_kernel=restart_kernel)
install_from_url(installer_url, prefix=prefix, env=env, pre_conda=pre_conda, run_checks=run_checks, restart_kernel=restart_kernel)


def install_miniconda(
prefix: os.PathLike = PREFIX, env: Dict[AnyStr, AnyStr] = None, run_checks: bool = True, restart_kernel: bool = True,
prefix: os.PathLike = PREFIX, env: Dict[AnyStr, AnyStr] = None, pre_conda: str = None, run_checks: bool = True, restart_kernel: bool = True,
):
"""
Install Miniconda 4.12.0 for Python 3.7.
Expand All @@ -323,17 +345,20 @@ def install_miniconda(
For example, a value with spaces should be passed as::

env={"VAR": '"a value with spaces"'}
pre_conda
Shell script to run before activating the conda base environment.
Accepts a file path or a string with the contents.
run_checks
Run checks to see if installation was run previously.
Change to False to ignore checks and always attempt
to run the installation.
"""
installer_url = r"https://repo.anaconda.com/miniconda/Miniconda3-py37_4.12.0-Linux-x86_64.sh"
install_from_url(installer_url, prefix=prefix, env=env, run_checks=run_checks, restart_kernel=restart_kernel)
install_from_url(installer_url, prefix=prefix, env=env, pre_conda=pre_conda, run_checks=run_checks, restart_kernel=restart_kernel)


def install_anaconda(
prefix: os.PathLike = PREFIX, env: Dict[AnyStr, AnyStr] = None, run_checks: bool = True, restart_kernel: bool = True,
prefix: os.PathLike = PREFIX, env: Dict[AnyStr, AnyStr] = None, pre_conda: str = None, run_checks: bool = True, restart_kernel: bool = True,
):
"""
Install Anaconda 2022.05, the latest version built
Expand All @@ -353,13 +378,16 @@ def install_anaconda(
For example, a value with spaces should be passed as::

env={"VAR": '"a value with spaces"'}
pre_conda
Shell script to run before activating the conda base environment.
Accepts a file path or a string with the contents.
run_checks
Run checks to see if installation was run previously.
Change to False to ignore checks and always attempt
to run the installation.
"""
installer_url = r"https://repo.anaconda.com/archive/Anaconda3-2022.05-Linux-x86_64.sh"
install_from_url(installer_url, prefix=prefix, env=env, run_checks=run_checks, restart_kernel=restart_kernel)
install_from_url(installer_url, prefix=prefix, env=env, pre_conda=pre_conda, run_checks=run_checks, restart_kernel=restart_kernel)


def check(prefix: os.PathLike = PREFIX, verbose: bool = True):
Expand Down