/
environment.py
99 lines (83 loc) · 3.31 KB
/
environment.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
"""Anaconda and Pip related utilities"""
import os
import logging
from jovian.utils.api import upload_file
from jovian.utils.misc import get_platform
from jovian.utils.constants import PLATFORMS, CONDA_NOT_FOUND
from jovian.utils.logger import log
from jovian.utils.error import CondaError
def get_conda_bin():
"""Get the path to the Anaconda binary"""
conda_bin = 'conda'
# Try executing the `conda` command
if os.popen(conda_bin).read().strip() == '':
# If it fails, look for $CONDA_EXE
conda_exe = os.popen('echo $CONDA_EXE').read().strip()
# Check if it returns a valid path
if conda_exe != '' and conda_exe != '$CONDA_EXE':
# Update binary and execute again
conda_bin = conda_exe
if os.popen(conda_bin).read().strip() == '':
raise CondaError(CONDA_NOT_FOUND)
logging.info('Anaconda binary: ' + conda_bin)
return conda_bin
def get_conda_env_name():
"""Get the name of the active conda environment"""
env_name = os.popen('echo $CONDA_DEFAULT_ENV').read().strip()
if env_name == '' or env_name == '$CONDA_DEFAULT_ENV':
env_name = 'base'
logging.info('Anaconda environment: ' + env_name)
return env_name
def read_conda_env(name=None):
"""Read the anaconda environment into a string"""
if name is None:
name = get_conda_env_name()
command = get_conda_bin() + ' env export -n ' + \
get_conda_env_name() + " --no-builds"
env_str = os.popen(command).read()
if env_str == '':
error = 'Failed to read Anaconda environment using command: "' + command + '"'
raise CondaError(error)
return env_str
def upload_conda_env(gist_slug, version=None):
"""Read and save the current Anaconda environment to server"""
# Export environment to YML string
env_str = read_conda_env(get_conda_env_name())
# Upload environment.yml
upload_file(gist_slug=gist_slug, file=('environment.yml', env_str), version=version)
# Check and include existing os-specific files
platform = get_platform()
for p in PLATFORMS:
pfname = 'environment-' + p + '.yml'
if p == platform:
# Use the new environment for current platform
upload_file(gist_slug=gist_slug, file=(pfname, env_str), version=version)
elif os.path.exists(pfname):
# Reuse old environments for other platforms
with open(pfname, 'rb') as f:
file = (pfname, f)
upload_file(gist_slug=gist_slug, file=file, version=version)
def print_conda_message(env_name):
if env_name:
message = ("""
#
# To activate this environment, use
#
# $ conda activate %s
#
# To deactivate an active environment, use
#
# $ conda deactivate
""") % env_name
log(message)
def read_pip_env():
"""Read the pip dependencies into a string"""
command = "pip --disable-pip-version-check freeze"
deps_str = os.popen(command).read()
if deps_str == '':
error = 'Failed to read Anaconda environment using command: "' + command + '"'
raise Exception(error)
return deps_str
def upload_pip_env(gist_slug, version=None):
"""Read and upload the current virtual environment to server"""
return upload_file(gist_slug=gist_slug, file=('requirements.txt', read_pip_env()), version=version)