2018-06-26 04:14:29 -07:00
|
|
|
"""
|
|
|
|
|
Wrap conda commandline program
|
|
|
|
|
"""
|
|
|
|
|
import os
|
|
|
|
|
import subprocess
|
|
|
|
|
import json
|
2018-07-19 17:30:09 -07:00
|
|
|
import hashlib
|
|
|
|
|
import contextlib
|
|
|
|
|
import tempfile
|
2019-05-24 14:37:42 +03:00
|
|
|
import requests
|
2018-07-20 11:12:03 -07:00
|
|
|
from distutils.version import LooseVersion as V
|
2019-05-19 13:45:57 -07:00
|
|
|
from tljh import utils
|
2018-06-26 18:35:58 -07:00
|
|
|
|
2018-07-19 17:30:09 -07:00
|
|
|
|
2020-05-08 16:48:52 +05:30
|
|
|
def sha256_file(fname):
|
2018-07-19 17:30:09 -07:00
|
|
|
"""
|
2020-05-08 16:48:52 +05:30
|
|
|
Return sha256 of a given filename
|
2018-07-19 17:30:09 -07:00
|
|
|
|
|
|
|
|
Copied from https://stackoverflow.com/a/3431838
|
|
|
|
|
"""
|
2020-05-08 16:48:52 +05:30
|
|
|
hash_sha256 = hashlib.sha256()
|
2018-07-19 17:30:09 -07:00
|
|
|
with open(fname, "rb") as f:
|
|
|
|
|
for chunk in iter(lambda: f.read(4096), b""):
|
2020-05-08 16:48:52 +05:30
|
|
|
hash_sha256.update(chunk)
|
|
|
|
|
return hash_sha256.hexdigest()
|
2018-07-19 17:30:09 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def check_miniconda_version(prefix, version):
|
|
|
|
|
"""
|
|
|
|
|
Return true if a miniconda install with version exists at prefix
|
|
|
|
|
"""
|
|
|
|
|
try:
|
2021-11-01 09:42:45 +01:00
|
|
|
installed_version = (
|
|
|
|
|
subprocess.check_output(
|
|
|
|
|
[os.path.join(prefix, 'bin', 'conda'), '-V'], stderr=subprocess.STDOUT
|
|
|
|
|
)
|
|
|
|
|
.decode()
|
|
|
|
|
.strip()
|
|
|
|
|
.split()[1]
|
|
|
|
|
)
|
2018-07-20 11:12:03 -07:00
|
|
|
return V(installed_version) >= V(version)
|
2018-07-19 17:30:09 -07:00
|
|
|
except (subprocess.CalledProcessError, FileNotFoundError):
|
2018-07-20 11:12:03 -07:00
|
|
|
# Conda doesn't exist
|
2018-07-19 17:30:09 -07:00
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@contextlib.contextmanager
|
2020-05-08 16:48:52 +05:30
|
|
|
def download_miniconda_installer(installer_url, sha256sum):
|
2018-07-19 17:30:09 -07:00
|
|
|
"""
|
2020-05-08 16:48:52 +05:30
|
|
|
Context manager to download miniconda installer from a given URL
|
2018-07-19 17:30:09 -07:00
|
|
|
|
|
|
|
|
This should be used as a contextmanager. It downloads miniconda installer
|
2020-05-08 16:48:52 +05:30
|
|
|
of given version, verifies the sha256sum & provides path to it to the `with`
|
2018-07-19 17:30:09 -07:00
|
|
|
block to run.
|
|
|
|
|
"""
|
2021-10-19 14:05:36 +05:30
|
|
|
with tempfile.NamedTemporaryFile('wb') as f:
|
|
|
|
|
f.write(requests.get(installer_url).content)
|
2021-10-19 13:47:11 +02:00
|
|
|
# Remain in the NamedTemporaryFile context, but flush changes, see:
|
|
|
|
|
# https://docs.python.org/3/library/os.html#os.fsync
|
2021-10-19 15:07:44 +05:30
|
|
|
f.flush()
|
2021-10-19 13:47:11 +02:00
|
|
|
os.fsync(f.fileno())
|
2018-07-19 17:30:09 -07:00
|
|
|
|
2020-05-08 16:48:52 +05:30
|
|
|
if sha256_file(f.name) != sha256sum:
|
|
|
|
|
raise Exception('sha256sum hash mismatch! Downloaded file corrupted')
|
2018-07-19 17:30:09 -07:00
|
|
|
|
|
|
|
|
yield f.name
|
|
|
|
|
|
|
|
|
|
|
2019-01-30 16:42:35 +01:00
|
|
|
def fix_permissions(prefix):
|
|
|
|
|
"""Fix permissions in the install prefix
|
|
|
|
|
|
|
|
|
|
For all files in the prefix, ensure that:
|
|
|
|
|
- everything is owned by current user:group
|
|
|
|
|
- nothing is world-writeable
|
|
|
|
|
|
|
|
|
|
Run after each install command.
|
|
|
|
|
"""
|
2021-11-01 09:42:45 +01:00
|
|
|
utils.run_subprocess(["chown", "-R", f"{os.getuid()}:{os.getgid()}", prefix])
|
2019-05-19 13:45:57 -07:00
|
|
|
utils.run_subprocess(["chmod", "-R", "o-w", prefix])
|
2019-01-30 16:42:35 +01:00
|
|
|
|
|
|
|
|
|
2018-07-19 17:30:09 -07:00
|
|
|
def install_miniconda(installer_path, prefix):
|
|
|
|
|
"""
|
|
|
|
|
Install miniconda with installer at installer_path under prefix
|
|
|
|
|
"""
|
2021-11-01 09:42:45 +01:00
|
|
|
utils.run_subprocess(['/bin/bash', installer_path, '-u', '-b', '-p', prefix])
|
2018-07-19 17:30:09 -07:00
|
|
|
# fix permissions on initial install
|
|
|
|
|
# a few files have the wrong ownership and permissions initially
|
|
|
|
|
# when the installer is run as root
|
2019-01-30 16:42:35 +01:00
|
|
|
fix_permissions(prefix)
|
2018-06-26 04:14:29 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def ensure_conda_packages(prefix, packages):
|
|
|
|
|
"""
|
2018-06-27 03:05:24 -07:00
|
|
|
Ensure packages (from conda-forge) are installed in the conda prefix.
|
2021-10-27 02:29:04 +02:00
|
|
|
|
|
|
|
|
Note that conda seem to update dependencies by default, so there is probably
|
|
|
|
|
no need to have a update parameter exposed for this function.
|
2018-06-26 04:14:29 -07:00
|
|
|
"""
|
2021-07-31 20:44:23 +01:00
|
|
|
conda_executable = [os.path.join(prefix, 'bin', 'mamba')]
|
2018-06-26 04:14:29 -07:00
|
|
|
abspath = os.path.abspath(prefix)
|
|
|
|
|
# Let subprocess errors propagate
|
2018-08-20 14:38:45 -07:00
|
|
|
# Explicitly do *not* capture stderr, since that's not always JSON!
|
|
|
|
|
# Scripting conda is a PITA!
|
2018-06-26 04:14:29 -07:00
|
|
|
# FIXME: raise different exception when using
|
2021-11-01 09:42:45 +01:00
|
|
|
raw_output = subprocess.check_output(
|
|
|
|
|
conda_executable
|
|
|
|
|
+ [
|
|
|
|
|
'install',
|
|
|
|
|
'-c',
|
|
|
|
|
'conda-forge', # Make customizable if we ever need to
|
|
|
|
|
'--json',
|
|
|
|
|
'--prefix',
|
|
|
|
|
abspath,
|
|
|
|
|
]
|
|
|
|
|
+ packages
|
|
|
|
|
).decode()
|
2018-06-26 04:14:29 -07:00
|
|
|
# `conda install` outputs JSON lines for fetch updates,
|
|
|
|
|
# and a undelimited output at the end. There is no reasonable way to
|
|
|
|
|
# parse this outside of this kludge.
|
2021-11-01 09:42:45 +01:00
|
|
|
filtered_output = '\n'.join(
|
|
|
|
|
[
|
|
|
|
|
l
|
|
|
|
|
for l in raw_output.split('\n')
|
|
|
|
|
# Sometimes the JSON messages start with a \x00. The lstrip removes these.
|
|
|
|
|
# conda messages seem to randomly throw \x00 in places for no reason
|
|
|
|
|
if not l.lstrip('\x00').startswith('{"fetch"')
|
|
|
|
|
]
|
|
|
|
|
)
|
2018-06-26 18:36:20 -07:00
|
|
|
output = json.loads(filtered_output.lstrip('\x00'))
|
2018-06-26 04:14:29 -07:00
|
|
|
if 'success' in output and output['success'] == True:
|
|
|
|
|
return
|
2019-01-30 16:42:35 +01:00
|
|
|
fix_permissions(prefix)
|
2018-06-26 17:38:56 -07:00
|
|
|
|
|
|
|
|
|
2021-10-27 02:29:04 +02:00
|
|
|
def ensure_pip_packages(prefix, packages, upgrade=False):
|
2018-06-26 17:38:56 -07:00
|
|
|
"""
|
|
|
|
|
Ensure pip packages are installed in the given conda prefix.
|
|
|
|
|
"""
|
|
|
|
|
abspath = os.path.abspath(prefix)
|
|
|
|
|
pip_executable = [os.path.join(abspath, 'bin', 'python'), '-m', 'pip']
|
2021-10-27 15:34:44 +02:00
|
|
|
pip_cmd = pip_executable + ['install']
|
2021-10-27 02:29:04 +02:00
|
|
|
if upgrade:
|
|
|
|
|
pip_cmd.append('--upgrade')
|
|
|
|
|
utils.run_subprocess(pip_cmd + packages)
|
2019-01-30 16:42:35 +01:00
|
|
|
fix_permissions(prefix)
|
2018-07-18 01:00:48 -07:00
|
|
|
|
|
|
|
|
|
2021-10-27 02:29:04 +02:00
|
|
|
def ensure_pip_requirements(prefix, requirements_path, upgrade=False):
|
2018-07-18 01:00:48 -07:00
|
|
|
"""
|
|
|
|
|
Ensure pip packages from given requirements_path are installed in given conda prefix.
|
|
|
|
|
|
|
|
|
|
requirements_path can be a file or a URL.
|
|
|
|
|
"""
|
|
|
|
|
abspath = os.path.abspath(prefix)
|
|
|
|
|
pip_executable = [os.path.join(abspath, 'bin', 'python'), '-m', 'pip']
|
2021-10-27 15:34:44 +02:00
|
|
|
pip_cmd = pip_executable + ['install']
|
2021-10-27 02:29:04 +02:00
|
|
|
if upgrade:
|
|
|
|
|
pip_cmd.append('--upgrade')
|
|
|
|
|
utils.run_subprocess(pip_cmd + ['--requirement', requirements_path])
|
2019-01-30 16:42:35 +01:00
|
|
|
fix_permissions(prefix)
|