-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathConda.jl
364 lines (317 loc) · 11.4 KB
/
Conda.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
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
VERSION < v"0.7.0-beta2.199" && __precompile__()
"""
The Conda module provides access to the [conda](http://conda.pydata.org/) packages
manager to install binary dependencies of other Julia packages.
The main functions in Conda are:
- `Conda.add(package)`: install a package;
- `Conda.rm(package)`: remove (uninstall) a package;
- `Conda.update()`: update all installed packages to the latest version;
- `Conda.list()`: list all installed packages.
- `Conda.add_channel(channel)`: add a channel to the list of channels;
- `Conda.channels()`: get the current list of channels;
- `Conda.rm_channel(channel)`: remove a channel from the list of channels;
```
"""
module Conda
using Compat, JSON, VersionParsing
const deps_file = joinpath(dirname(@__FILE__), "..", "deps", "deps.jl")
if isfile(deps_file)
# Includes definition for ROOTENV, and MINICONDA_VERSION
include(deps_file)
else
error("Conda is not properly configured. Run Pkg.build(\"Conda\") before importing the Conda module.")
end
const Environment = Union{AbstractString,Symbol}
"Prefix for installation of the environment"
function prefix(name::Symbol)
sname = string(name)
if isempty(sname)
throw(ArgumentError("Environment name should be non empty."))
end
return joinpath(ROOTENV, "envs", sname)
end
function prefix(path::AbstractString)
if !isdir(path)
throw(ArgumentError("Path to conda environment is not valid: $path"))
end
return path
end
const PREFIX = prefix(ROOTENV)
"Prefix for the executable files installed with the packages"
function bin_dir(env::Environment)
return Compat.Sys.iswindows() ? joinpath(prefix(env), "Library", "bin") : joinpath(prefix(env), "bin")
end
const BINDIR = bin_dir(ROOTENV)
"Prefix for the shared libraries installed with the packages"
function lib_dir(env::Environment)
return Compat.Sys.iswindows() ? joinpath(prefix(env), "Library", "bin") : joinpath(prefix(env), "lib")
end
const LIBDIR = lib_dir(ROOTENV)
"Prefix for the python scripts. On UNIX, this is the same than Conda.BINDIR"
function script_dir(env::Environment)
return Compat.Sys.iswindows() ? joinpath(prefix(env), "Scripts") : bin_dir(env)
end
const SCRIPTDIR = script_dir(ROOTENV)
"Prefix where the `python` command lives"
function python_dir(env::Environment)
return Compat.Sys.iswindows() ? prefix(env) : bin_dir(env)
end
const PYTHONDIR = python_dir(ROOTENV)
# note: the same conda program is used for all environments
const conda = if Compat.Sys.iswindows()
p = script_dir(ROOTENV)
conda_bat = joinpath(p, "conda.bat")
isfile(conda_bat) ? conda_bat : joinpath(p, "conda.exe")
else
joinpath(bin_dir(ROOTENV), "conda")
end
"Path to the condarc file"
conda_rc(env::Environment) = joinpath(prefix(env), "condarc-julia.yml")
const CONDARC = conda_rc(ROOTENV)
"""
Use a cleaned up environment for the command `cmd`.
Any environment variable starting by CONDA or PYTHON will interact with the run.
"""
function _set_conda_env(cmd, env::Environment=ROOTENV)
env_var = copy(ENV)
to_remove = String[]
for var in keys(env_var)
if startswith(var, "CONDA") || startswith(var, "PYTHON")
push!(to_remove, var)
end
end
for var in to_remove
pop!(env_var, var)
end
env_var["PYTHONIOENCODING"]="UTF-8"
env_var["CONDARC"] = conda_rc(env)
env_var["CONDA_PREFIX"] = prefix(env)
setenv(cmd, env_var)
end
"Run conda command with environment variables set."
function runconda(args::Cmd, env::Environment=ROOTENV)
_install_conda(env)
Compat.@info("Running $(`conda $args`) in $(env==ROOTENV ? "root" : env) environment")
run(_set_conda_env(`$conda $args`, env))
return nothing
end
"Run conda command with environment variables set and return the json output as a julia object"
function parseconda(args::Cmd, env::Environment=ROOTENV)
_install_conda(env)
JSON.parse(read(_set_conda_env(`$conda $args --json`, env), String))
end
"Get the miniconda installer URL."
function _installer_url()
res = "https://repo.continuum.io/miniconda/Miniconda$(MINICONDA_VERSION)-latest-"
if Compat.Sys.isapple()
res *= "MacOSX"
elseif Compat.Sys.islinux()
res *= "Linux"
elseif Compat.Sys.iswindows()
if MINICONDA_VERSION == "3"
# Quick fix for:
# * https://github.com/JuliaLang/IJulia.jl/issues/739
# * https://github.com/ContinuumIO/anaconda-issues/issues/10082
# * https://github.com/conda/conda/issues/7789
res = "https://repo.continuum.io/miniconda/Miniconda$(MINICONDA_VERSION)-4.5.4-"
end
res *= "Windows"
else
error("Unsuported OS.")
end
res *= Sys.WORD_SIZE == 64 ? "-x86_64" : "-x86"
res *= Compat.Sys.iswindows() ? ".exe" : ".sh"
return res
end
"Suppress progress bar in continuous integration environments"
_quiet() = get(ENV, "CI", "false") == "true" ? `-q` : ``
"Install miniconda if it hasn't been installed yet; _install_conda(true) installs Conda even if it has already been installed."
function _install_conda(env::Environment, force::Bool=false)
if force || !isfile(Conda.conda)
Compat.@info("Downloading miniconda installer ...")
if Compat.Sys.isunix()
installer = joinpath(PREFIX, "installer.sh")
end
if Compat.Sys.iswindows()
installer = joinpath(PREFIX, "installer.exe")
end
download(_installer_url(), installer)
Compat.@info("Installing miniconda ...")
if Compat.Sys.isunix()
chmod(installer, 33261) # 33261 corresponds to 755 mode of the 'chmod' program
run(`$installer -b -f -p $PREFIX`)
end
if Compat.Sys.iswindows()
run(Cmd(`$installer /S /AddToPath=0 /RegisterPython=0 /D=$PREFIX`, windows_verbatim=true))
end
Conda.add_channel("defaults")
# Update conda because conda 4.0 is needed and miniconda download installs only 3.9
runconda(`update $(_quiet()) -y conda`)
end
if !isdir(prefix(env))
runconda(`create $(_quiet()) -y -p $(prefix(env))`)
end
end
const PkgOrPkgs = Union{AbstractString, AbstractVector{<: AbstractString}}
"Install a new package or packages."
function add(pkg::PkgOrPkgs, env::Environment=ROOTENV)
runconda(`install $(_quiet()) -y $pkg`, env)
end
"Uninstall a package or packages."
function rm(pkg::PkgOrPkgs, env::Environment=ROOTENV)
runconda(`remove $(_quiet()) -y $pkg`, env)
end
"Update all installed packages."
function update(env::Environment=ROOTENV)
if env == ROOTENV
runconda(`update $(_quiet()) -y --all conda`, env)
else
runconda(`update $(_quiet()) -y --all`, env)
end
end
"List all installed packages as an dict of tuples with (version_number, fullname)."
function _installed_packages_dict(env::Environment=ROOTENV)
_install_conda(env)
package_dict = Dict{String, Tuple{VersionNumber, String}}()
for line in eachline(_set_conda_env(`$conda list`, env))
line = chomp(line)
if !startswith(line, "#")
name, version, build_string = split(line)
try
package_dict[name] = (vparse(version), line)
catch
package_dict[name] = (v"9999.9999.9999", line)
warn("Failed parsing string: \"$(version)\" to a version number. Please open an issue!")
end
end
end
return package_dict
end
"List all installed packages as an array."
_installed_packages(env::Environment=ROOTENV) = keys(_installed_packages_dict(env))
"List all installed packages to standard output."
function list(env::Environment=ROOTENV)
runconda(`list`, env)
end
"""
export_list(filepath, env=$ROOTENV)
export_list(io, env=$ROOTENV)
List all packages and write them to an export file for use the Conda.import_list
"""
function export_list(filepath::AbstractString, env::Environment=ROOTENV)
_install_conda(env)
open(filepath, "w") do fobj
export_list(fobj, env)
end
end
function export_list(io::IO, env::Environment=ROOTENV)
write(io, read(_set_conda_env(`$conda list --export`, env)))
end
"Get the exact version of a package as a `VersionNumber`."
function version(name::AbstractString, env::Environment=ROOTENV)
packages = parseconda(`list`, env)
for package in packages
pname = get(package, "name", "")
startswith(pname, name) && return vparse(package["version"])
end
error("Could not find the $name package")
end
"Search packages for a string"
function search(package::AbstractString, env::Environment=ROOTENV)
return collect(keys(parseconda(`search $package`, env)))
end
"Search a specific version of a package"
function search(package::AbstractString, _ver::Union{AbstractString,VersionNumber}, env::Environment=ROOTENV)
ret=parseconda(`search $package`, env)
out = String[]
ver = string(_ver)
verv = vparse(ver)
for k in keys(ret)
for i in 1:length(ret[k])
kver = ret[k][i]["version"]
(kver==ver || vparse(kver)==verv) && push!(out,k)
end
end
out
end
"Check if a given package exists."
function exists(package::AbstractString, env::Environment=ROOTENV)
if occursin("==", package)
pkg,ver=split(package,"==") # Remove version if provided
return pkg in search(pkg,ver,env)
else
if package in search(package,env)
# Found exactly this package
return true
else
return false
end
end
end
"Get the list of channels used to search packages"
function channels(env::Environment=ROOTENV)
ret=parseconda(`config --get channels`, env)
if haskey(ret["get"], "channels")
return collect(String, ret["get"]["channels"])
else
return String[]
end
end
"Add a channel to the list of channels"
function add_channel(channel::AbstractString, env::Environment=ROOTENV)
runconda(`config --add channels $channel --force`, env)
end
"Remove a channel from the list of channels"
function rm_channel(channel::AbstractString, env::Environment=ROOTENV)
runconda(`config --remove channels $channel --force`, env)
end
"""
clean(;
debug=false, index=true, locks=true, tarballs=true, packages=true, sources=true
)
Runs `conda clean -y` with the specified flags.
"""
function clean(;
debug=false, index=true, locks=true, tarballs=true, packages=true, sources=true
)
kwargs = [debug, index, locks, tarballs, packages, sources]
if !any(kwargs[2:end])
@warn(
"Please specify 1 or more of the conda artifacts to clean up (e.g., `packages=true`)."
)
end
flags = [
"--debug",
"--index-cache",
"--lock",
"--tarballs",
"--packages",
"--source-cache",
]
cmd = Cmd([conda, "clean", "--yes", flags[kwargs]...])
run(_set_conda_env(cmd))
end
""""
import_list(filename, env=$ROOTENV, channels=String[])
import_list(io, env=$ROOTENV, channels=String[])
Create a new environment with various channels and a packages list file.
"""
function import_list(
filepath::AbstractString,
env::Environment=ROOTENV;
channels=String[]
)
channel_str = ["-c $channel" for channel in channels]
run(_set_conda_env(
`$conda create $(_quiet()) -y -p $(prefix(env)) $channel_str --file $filepath`,
env
))
end
function import_list(io::IO, args...; kwargs...)
mktemp() do path, fobj
write(fobj, read(io))
close(fobj)
import_list(path, args...; kwargs...)
end
end
end