-
Notifications
You must be signed in to change notification settings - Fork 48
/
condacolab.py
412 lines (342 loc) Β· 14.3 KB
/
condacolab.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
"""
condacolab
Install Conda and friends on Google Colab, easily
Usage:
>>> import condacolab
>>> condacolab.install()
For more details, check the docstrings for ``install_from_url()``.
"""
import json
import os
import sys
import shutil
from datetime import datetime, timedelta
from pathlib import Path
from subprocess import check_output, run, PIPE, STDOUT
from textwrap import dedent
from typing import Dict, AnyStr
from urllib.request import urlopen
from distutils.spawn import find_executable
from IPython.display import display
from IPython import get_ipython
try:
import ipywidgets as widgets
HAS_IPYWIDGETS = True
except ImportError:
HAS_IPYWIDGETS = False
try:
import google.colab
except ImportError:
raise RuntimeError("This module must ONLY run as part of a Colab notebook!")
__version__ = "0.1.4"
__author__ = (
"Jaime RodrΓguez-Guerra <jaimergp@users.noreply.github.com>, "
"Surbhi Sharma <ssurbhi560@users.noreply.github.com>"
)
PREFIX = "/opt/conda"
if HAS_IPYWIDGETS:
restart_kernel_button = widgets.Button(description="Restart kernel now...")
restart_button_output = widgets.Output(layout={'border': '1px solid black'})
else:
restart_kernel_button = restart_button_output = None
def _on_button_clicked(b):
with restart_button_output:
get_ipython().kernel.do_shutdown(True)
print("Kernel restarted!")
restart_kernel_button.close()
def _run_subprocess(command, logs_filename):
"""
Run subprocess then write the logs for that process and raise errors if it fails.
Parameters
----------
command
Command to run while installing the packages.
logs_filename
Name of the file to be used for writing the logs after running the task.
"""
task = run(
command,
check=False,
stdout=PIPE,
stderr=STDOUT,
text=True,
)
logs_file_path = "/var/condacolab"
os.makedirs(logs_file_path, exist_ok=True)
with open(f"{logs_file_path}/{logs_filename}", "w") as f:
f.write(task.stdout)
assert (task.returncode == 0), f"π₯ππ₯ The installation failed! Logs are available at `{logs_file_path}/{logs_filename}`."
def install_from_url(
installer_url: AnyStr,
prefix: os.PathLike = PREFIX,
env: Dict[AnyStr, AnyStr] = None,
run_checks: bool = True,
restart_kernel: bool = True,
):
"""
Download and run a constructor-like installer, patching
the necessary bits so it works on Colab right away.
This will restart your kernel as a result!
Parameters
----------
installer_url
URL pointing to a ``constructor``-like installer, such
as Miniconda or Mambaforge
prefix
Target location for the installation
env
Environment variables to inject in the kernel restart.
We *need* to inject ``LD_LIBRARY_PATH`` so ``{PREFIX}/lib``
is first, but you can also add more if you need it. Take
into account that no quote handling is done, so you need
to add those yourself in the raw string. They will
end up added to a line like ``exec env VAR=VALUE python3...``.
For example, a value with spaces should be passed as::
env={"VAR": '"a value with spaces"'}
run_checks
Run checks to see if installation was run previously.
Change to False to ignore checks and always attempt
to run the installation.
restart_kernel
Variable to manage the kernel restart during the installation
of condacolab. Set it `False` to stop the kernel from restarting
automatically and get a button instead to do it.
"""
if run_checks:
try: # run checks to see if it this was run already
return check(prefix)
except AssertionError:
pass # just install
t0 = datetime.now()
print(f"β¬ Downloading {installer_url}...")
installer_fn = "__installer__.sh"
with urlopen(installer_url) as response, open(installer_fn, "wb") as out:
shutil.copyfileobj(response, out)
condacolab_task = _run_subprocess(
["bash", installer_fn, "-bfp", str(prefix)],
"condacolab_install.log",
)
print("π Adjusting configuration...")
cuda_version = ".".join(os.environ.get("CUDA_VERSION", "*.*.*").split(".")[:2])
prefix = Path(prefix)
condameta = prefix / "conda-meta"
condameta.mkdir(parents=True, exist_ok=True)
with open(condameta / "pinned", "a") as f:
f.write(f"cudatoolkit {cuda_version}.*\n")
with open(prefix / ".condarc", "a") as f:
f.write("always_yes: true\n")
print("π¦ Installing...")
# Installing the following packages because Colab server expects these packages to be installed in order to launch a Python kernel:
# - matplotlib-base
# - psutil
# - google-colab
# - colabtools
conda_exe = "mamba" if os.path.isfile(f"{prefix}/bin/mamba") else "conda"
# check if any of those packages are already installed. If it is installed, remove it from the list of required packages.
output = check_output([f"{prefix}/bin/conda", "list", "--json"])
payload = json.loads(output)
installed_names = [pkg["name"] for pkg in payload]
required_packages = ["matplotlib-base", "psutil", "google-colab"]
for pkg in required_packages.copy():
if pkg in installed_names:
required_packages.remove(pkg)
if required_packages:
_run_subprocess(
[f"{prefix}/bin/{conda_exe}", "install", "-yq", *required_packages],
"conda_task.log",
)
pip_task = _run_subprocess(
[f"{prefix}/bin/python", "-m", "pip", "-q", "install", "-U", "https://github.com/googlecolab/colabtools/archive/refs/heads/main.zip", "condacolab"],
"pip_task.log"
)
env = env or {}
bin_path = f"{prefix}/bin"
os.rename(sys.executable, f"{sys.executable}.renamed_by_condacolab.bak")
with open(sys.executable, "w") as f:
f.write(
dedent(
f"""
#!/bin/bash
source {prefix}/etc/profile.d/conda.sh
conda activate
unset PYTHONPATH
mv /usr/bin/lsb_release /usr/bin/lsb_release.renamed_by_condacolab.bak
exec {bin_path}/python $@
"""
).lstrip()
)
run(["chmod", "+x", sys.executable])
taken = timedelta(seconds=round((datetime.now() - t0).total_seconds(), 0))
print(f"β² Done in {taken}")
if restart_kernel:
print("π Restarting kernel...")
get_ipython().kernel.do_shutdown(True)
elif HAS_IPYWIDGETS:
print("π Please restart kernel...")
restart_kernel_button.on_click(_on_button_clicked)
display(restart_kernel_button, restart_button_output)
else:
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,
):
"""
Install Mambaforge, built for Python 3.7.
Mambaforge consists of a Miniconda-like distribution optimized
and preconfigured for conda-forge packages, and includes ``mamba``,
a faster ``conda`` implementation.
Unlike the official Miniconda, this is built with the latest ``conda``.
Parameters
----------
prefix
Target location for the installation
env
Environment variables to inject in the kernel restart.
We *need* to inject ``LD_LIBRARY_PATH`` so ``{PREFIX}/lib``
is first, but you can also add more if you need it. Take
into account that no quote handling is done, so you need
to add those yourself in the raw string. They will
end up added to a line like ``exec env VAR=VALUE python3...``.
For example, a value with spaces should be passed as::
env={"VAR": '"a value with spaces"'}
run_checks
Run checks to see if installation was run previously.
Change to False to ignore checks and always attempt
to run the installation.
restart_kernel
Variable to manage the kernel restart during the installation
of condacolab. Set it `False` to stop the kernel from restarting
automatically and get a button instead to do it.
"""
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)
# 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,
):
"""
Install Mambaforge, built for Python 3.7.
Mambaforge consists of a Miniconda-like distribution optimized
and preconfigured for conda-forge packages.
Unlike the official Miniconda, this is built with the latest ``conda``.
Parameters
----------
prefix
Target location for the installation
env
Environment variables to inject in the kernel restart.
We *need* to inject ``LD_LIBRARY_PATH`` so ``{PREFIX}/lib``
is first, but you can also add more if you need it. Take
into account that no quote handling is done, so you need
to add those yourself in the raw string. They will
end up added to a line like ``exec env VAR=VALUE python3...``.
For example, a value with spaces should be passed as::
env={"VAR": '"a value with spaces"'}
run_checks
Run checks to see if installation was run previously.
Change to False to ignore checks and always attempt
to run the installation.
restart_kernel
Variable to manage the kernel restart during the installation
of condacolab. Set it `False` to stop the kernel from restarting
automatically and get a button instead to do it.
"""
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)
def install_miniconda(
prefix: os.PathLike = PREFIX, env: Dict[AnyStr, AnyStr] = None, run_checks: bool = True, restart_kernel: bool = True,
):
"""
Install Miniconda 4.12.0 for Python 3.7.
Parameters
----------
prefix
Target location for the installation
env
Environment variables to inject in the kernel restart.
We *need* to inject ``LD_LIBRARY_PATH`` so ``{PREFIX}/lib``
is first, but you can also add more if you need it. Take
into account that no quote handling is done, so you need
to add those yourself in the raw string. They will
end up added to a line like ``exec env VAR=VALUE python3...``.
For example, a value with spaces should be passed as::
env={"VAR": '"a value with spaces"'}
run_checks
Run checks to see if installation was run previously.
Change to False to ignore checks and always attempt
to run the installation.
restart_kernel
Variable to manage the kernel restart during the installation
of condacolab. Set it `False` to stop the kernel from restarting
automatically and get a button instead to do it.
"""
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)
def install_anaconda(
prefix: os.PathLike = PREFIX, env: Dict[AnyStr, AnyStr] = None, run_checks: bool = True, restart_kernel: bool = True,
):
"""
Install Anaconda 2022.05, the latest version built
for Python 3.7 at the time of update.
Parameters
----------
prefix
Target location for the installation
env
Environment variables to inject in the kernel restart.
We *need* to inject ``LD_LIBRARY_PATH`` so ``{PREFIX}/lib``
is first, but you can also add more if you need it. Take
into account that no quote handling is done, so you need
to add those yourself in the raw string. They will
end up added to a line like ``exec env VAR=VALUE python3...``.
For example, a value with spaces should be passed as::
env={"VAR": '"a value with spaces"'}
run_checks
Run checks to see if installation was run previously.
Change to False to ignore checks and always attempt
to run the installation.
restart_kernel
Variable to manage the kernel restart during the installation
of condacolab. Set it `False` to stop the kernel from restarting
automatically and get a button instead to do it.
"""
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)
def check(prefix: os.PathLike = PREFIX, verbose: bool = True):
"""
Run some basic checks to ensure that ``conda`` has been installed
correctly
Parameters
----------
prefix
Location where ``conda`` was installed (should match the one
provided for ``install()``.
verbose
Print success message if True
"""
assert find_executable("conda"), "π₯ππ₯ Conda not found!"
pymaj, pymin = sys.version_info[:2]
sitepackages = f"{prefix}/lib/python{pymaj}.{pymin}/site-packages"
assert sitepackages in sys.path, f"π₯ππ₯ PYTHONPATH was not patched! Value: {sys.path}"
assert all(
not path.startswith("/usr/local/") for path in sys.path
), f"π₯ππ₯ PYTHONPATH include system locations: {[path for path in sys.path if path.startswith('/usr/local')]}!"
assert (
f"{prefix}/bin" in os.environ["PATH"]
), f"π₯ππ₯ PATH was not patched! Value: {os.environ['PATH']}"
assert (
prefix == os.environ.get("CONDA_PREFIX")
), f"π₯ππ₯ CONDA_PREFIX value: {os.environ.get('CONDA_PREFIX', '<not set>')} does not match conda installation location {prefix}!"
if verbose:
print("β¨π°β¨ Everything looks OK!")
__all__ = [
"install",
"install_from_url",
"install_mambaforge",
"install_miniforge",
"install_miniconda",
"install_anaconda",
"check",
"PREFIX",
]