Skip to content

Commit

Permalink
Recreating environments (#132)
Browse files Browse the repository at this point in the history
* Added Conda.freeze and Conda.create functions for re-creating environments.

* Support IO for create and freeze.

* Renamed freeze to export_list.

* Fix docstrings

* Rename create to import_list
  • Loading branch information
rofinn authored and stevengj committed Jan 3, 2019
1 parent d1a90cd commit 4725842
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
43 changes: 43 additions & 0 deletions src/Conda.jl
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,23 @@ 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)
Expand Down Expand Up @@ -318,4 +335,30 @@ function clean(;
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
18 changes: 18 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,21 @@ end

# Run conda clean
Conda.clean(; debug=true)

@testset "Exporting and creating environments" begin
new_env = :test_conda_jl_2
Conda.add("curl", env)
Conda.export_list("conda-pkg.txt", env)

# Create a new environment
rm(Conda.prefix(new_env); force=true, recursive=true)
Conda.import_list(
IOBuffer(read("conda-pkg.txt")), new_env; channels=["defaults", "conda-forge"]
)

# Ensure that our new environment has our channels and package installed.
Conda.channels(new_env) == ["defaults", "conda-forge"]
installed = Conda._installed_packages(new_env)
@test "curl" installed
rm("conda-pkg.txt")
end

0 comments on commit 4725842

Please sign in to comment.