-
Notifications
You must be signed in to change notification settings - Fork 15
/
env.jl
113 lines (100 loc) · 2.58 KB
/
env.jl
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
"""
This file defines functions for interacting with the environment, such as `withenv()` and
`which()`. They all automatically resolve first.
"""
"""
activate!(env)
"Activate" the Conda environment by modifying the given dict of environment variables.
"""
function activate!(e)
old_path = get(e, "PATH", "")
d = envdir()
path_sep = Sys.iswindows() ? ';' : ':'
new_path = join(bindirs(), path_sep)
new_path = "$(new_path)$(path_sep)$(dirname(MicroMamba.executable()))"
if old_path != ""
new_path = "$(new_path)$(path_sep)$(old_path)"
end
e["PATH"] = new_path
e["CONDA_PREFIX"] = d
e["CONDA_DEFAULT_ENV"] = d
e["CONDA_SHLVL"] = "1"
e["CONDA_PROMPT_MODIFIER"] = "($d) "
e["MAMBA_ROOT_PREFIX"] = MicroMamba.root_dir()
e
end
"""
withenv(f::Function)
Call `f()` while the Conda environment is active.
"""
function withenv(f::Function)
old_env = copy(ENV)
# shell("activate")
activate!(ENV)
frozen = STATE.frozen
STATE.frozen = true
try
return f()
finally
STATE.frozen = frozen
# shell("deactivate")
# copy!(ENV, old_env) does not work (empty!(ENV) not implemented)
for k in collect(keys(ENV))
if !haskey(old_env, k)
delete!(ENV, k)
end
end
for (k, v) in old_env
ENV[k] = v
end
end
end
"""
envdir(...)
The root directory of the Conda environment.
Any additional arguments are joined to the path.
"""
function envdir(args...)
resolve()
joinpath(STATE.meta_dir, "env", args...)
end
"""
bindirs()
The directories containing binaries in the Conda environment.
"""
function bindirs()
e = envdir()
if Sys.iswindows()
("$e", "$e\\Library\\mingw-w64\\bin", "$e\\Library\\usr\\bin", "$e\\Library\\bin", "$e\\Scripts", "$e\\bin")
else
("$e/bin", #="$(micromamba_root_prefix)/condabin"=#)
end
end
"""
which(progname)
Find the binary called `progname` in the Conda environment.
"""
function which(progname)
# Set the PATH to dirs in the environment, then use Sys.which().
old_path = get(ENV, "PATH", nothing)
ENV["PATH"] = join(bindirs(), Sys.iswindows() ? ';' : ':')
try
Sys.which(progname)
finally
if old_path === nothing
delete!(ENV, "PATH")
else
ENV["PATH"] = old_path
end
end
end
"""
gc()
Remove unused packages and caches.
"""
function gc(; io::IO=stderr)
resolve()
cmd = MicroMamba.cmd(`clean -y --all`, io=io)
_run(io, cmd, "Removing unused caches")
nothing
end