Skip to content

Commit

Permalink
Merge pull request #198 from isuruf/miniforge
Browse files Browse the repository at this point in the history
use miniforge optionally
  • Loading branch information
isuruf authored Mar 5, 2021
2 parents dc1c1c7 + 2c4690c commit 15713e5
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 20 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ julia:
env:
- CONDA_JL_VERSION="2"
- CONDA_JL_VERSION="3"
- CONDA_JL_VERSION="3" CONDA_JL_USE_MINIFORGE="yes"

notifications:
email: false
Expand Down
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,24 @@ tell you how to delete your existing Miniconda installation if needed.
Most users will not need to use Python 2. This is provided primarily for developers wishing to test their packages for both Python 2 and Python, e.g. by setting the `CONDA_JL_VERSION`
variable on [TravisCI](https://docs.travis-ci.com/user/environment-variables/) and/or [AppVeyor](https://www.appveyor.com/docs/build-configuration/#environment-variables).
## Using Miniforge
Miniforge is a community based conda installer by `conda-forge`, a part of NumFOCUS.
Using miniforge and conda-forge in general avoids using `repo.anaconda.com`
maintained by Anaconda, Inc which has terms of conditions that you may want to avoid.
`conda-forge` packages are hosted on `anaconda.org`, but Anaconda, Inc has been
providing hosting for free under the terms of `conda-forge` which is `BSD-3-Clause`
on top of the original license of the software packages. To use miniforge, use
the `CONDA_JL_USE_MINIFORGE` environment variable.
```jl
julia> ENV["CONDA_JL_USE_MINIFORGE"] = "1"
pkg> build Conda
```
Also note that you have to use Miniforge for `aarch64-linux-gnu` and
`aarch64-apple-darwin` platforms as Miniconda is not available for those platforms yet.
## Bugs and suggestions
Expand Down
6 changes: 6 additions & 0 deletions deps/build.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ module DefaultDeps
if !isdefined(@__MODULE__, :ROOTENV)
const ROOTENV = joinpath(Main.condadir, MINICONDA_VERSION)
end
if !isdefined(@__MODULE__, :USE_MINIFORGE)
const USE_MINIFORGE = false
end
end

MINICONDA_VERSION = get(ENV, "CONDA_JL_VERSION", DefaultDeps.MINICONDA_VERSION)
Expand All @@ -27,6 +30,8 @@ ROOTENV = get(ENV, "CONDA_JL_HOME") do
end
end

USE_MINIFORGE = lowercase(get(ENV, "CONDA_JL_USE_MINIFORGE", DefaultDeps.USE_MINIFORGE ? "1" : "0")) in ("1","true","yes")

if isdir(ROOTENV) && MINICONDA_VERSION != DefaultDeps.MINICONDA_VERSION
error("""Miniconda version changed, since last build.
However, a root enviroment already exists at $(ROOTENV).
Expand All @@ -42,6 +47,7 @@ end
deps = """
const ROOTENV = "$(escape_string(ROOTENV))"
const MINICONDA_VERSION = "$(escape_string(MINICONDA_VERSION))"
const USE_MINIFORGE = $USE_MINIFORGE
"""

mkpath(condadir)
Expand Down
57 changes: 46 additions & 11 deletions src/Conda.jl
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,20 @@ else
end

"Path to the condarc file"
conda_rc(env::Environment) = joinpath(prefix(env), "condarc-julia.yml")
function conda_rc(env::Environment)
#=
sys_condarc is looked at by conda for almost operations
except when adding channels with --file argument.
we copy it to env_condarc avoid this conda bug
=#
env_condarc = joinpath(prefix(env), "condarc-julia.yml")
sys_condarc = joinpath(prefix(ROOTENV), ".condarc")
if isdir(prefix(env)) && !isfile(env_condarc) && isfile(sys_condarc)
cp(sys_condarc, env_condarc)
end
return env_condarc
end

const CONDARC = conda_rc(ROOTENV)

"""
Expand Down Expand Up @@ -125,26 +138,47 @@ end

"Get the miniconda installer URL."
function _installer_url()
res = "https://repo.continuum.io/miniconda/Miniconda$(MINICONDA_VERSION)-latest-"
if Sys.isapple()
res *= "MacOSX"
conda_os = "MacOSX"
elseif Sys.islinux()
res *= "Linux"
conda_os = "Linux"
elseif Sys.iswindows()
res *= "Windows"
conda_os = "Windows"
else
error("Unsuported OS.")
end

# mapping of Julia architecture names to Conda architecture names, where they differ
arch2conda = Dict(:i686 => :x86, :powerpc64le => :ppc64le)

if Sys.ARCH in (:i686, :x86_64, :ppc64le, :powerpc64le)
res *= string('-', get(arch2conda, Sys.ARCH, Sys.ARCH))
if Sys.isapple()
arch2conda[:aarch64] = :arm64
end

conda_platform = string(conda_os, '-', get(arch2conda, Sys.ARCH, Sys.ARCH))

MINIFORGE_PLATFORMS = ["Linux-aarch64", "Linux-x86_64", "Linux-ppc64le",
"MacOSX-arm64", "MacOSX-x86_64",
"Windows-x86_64"]
MINICONDA_PLATFORMS = ["Linux-x86_64", "Linux-x86",
"MacOSX-x86", "MacOSX-x86_64",
"Windows-x86", "Windows-x86_64"]

if USE_MINIFORGE
if !(conda_platform in MINIFORGE_PLATFORMS)
error("Unsupported miniforge platform: $(conda_platform)")
else
res = "https://github.com/conda-forge/miniforge/releases/latest/download/Miniforge3-"
end
else
error("Unsupported architecture: $(Sys.ARCH)")
if !(conda_platform in MINICONDA_PLATFORMS)
error("Unsupported miniconda platform: $(conda_platform)")
else
res = "https://repo.continuum.io/miniconda/Miniconda$(MINICONDA_VERSION)-latest-"
end
end

res *= conda_platform
res *= Sys.iswindows() ? ".exe" : ".sh"
return res
end
Expand Down Expand Up @@ -173,9 +207,6 @@ function _install_conda(env::Environment, force::Bool=false)
if 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))`)
Expand Down Expand Up @@ -361,6 +392,10 @@ function import_list(
`$conda create $(_quiet()) -y -p $(prefix(env)) $channel_str --file $filepath`,
env
))
# persist the channels given for this environment
for channel in reverse(channels)
add_channel(channel, env)
end
end

function import_list(io::IO, args...; kwargs...)
Expand Down
48 changes: 39 additions & 9 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@ exe = Sys.iswindows() ? ".exe" : ""

Conda.update()

if Conda.USE_MINIFORGE
default_channel = "conda-forge"
alt_channel = "defaults"
else
default_channel = "defaults"
alt_channel = "conda-forge"
end

env = :test_conda_jl
rm(Conda.prefix(env); force=true, recursive=true)

Expand Down Expand Up @@ -42,7 +50,7 @@ condarc_path = joinpath(homedir(), ".condarc")
last_modified = mtime(condarc_path)

Conda.add_channel("foo", env)
@test Conda.channels(env) == ["foo", "defaults"]
@test Conda.channels(env) == ["foo", default_channel]

# Testing that calling the function twice do not fail
Conda.add_channel("foo", env)
Expand All @@ -54,10 +62,10 @@ Conda.add_channel("foo", env)
Conda.rm_channel("foo", env)
@test mtime(condarc_path) == last_modified

@test Conda.channels(env) == ["defaults"]
@test Conda.channels(env) == [default_channel]

# Add a package from a specific channel
Conda.add("requests", env; channel="conda-forge")
Conda.add("zlib", env; channel=alt_channel)

@testset "Batch install and uninstall" begin
Conda.add(["affine", "ansi2html"], env)
Expand All @@ -82,11 +90,11 @@ Conda.clean(; debug=true)
# 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"]
IOBuffer(read("conda-pkg.txt")), new_env; channels=["foo", alt_channel, default_channel]
)

# Ensure that our new environment has our channels and package installed.
Conda.channels(new_env) == ["defaults", "conda-forge"]
@test Conda.channels(new_env) == ["foo", alt_channel, default_channel]
installed = Conda._installed_packages(new_env)
@test "curl" installed
rm("conda-pkg.txt")
Expand Down Expand Up @@ -146,11 +154,29 @@ end
@test !isfile(depsfile)
@test !isfile(joinpath(condadir, "deps.jl"))

withenv("CONDA_JL_VERSION" => nothing, "CONDA_JL_HOME" => nothing) do
withenv("CONDA_JL_VERSION" => nothing, "CONDA_JL_HOME" => nothing, "CONDA_JL_USE_MINIFORGE" => nothing) do
Pkg.build("Conda")
@test read(depsfile, String) == """
const ROOTENV = "$(escape_string(joinpath(condadir, "3")))"
const MINICONDA_VERSION = "3"
const USE_MINIFORGE = false
"""
end
end
end

@testset "miniforge" begin
preserve_build() do
# In order to test the defaults no depsfiles must be present
@test !isfile(depsfile)
@test !isfile(joinpath(condadir, "deps.jl"))

withenv("CONDA_JL_VERSION" => nothing, "CONDA_JL_HOME" => nothing, "CONDA_JL_USE_MINIFORGE" => "1") do
Pkg.build("Conda")
@test read(depsfile, String) == """
const ROOTENV = "$(escape_string(joinpath(condadir, "3")))"
const MINICONDA_VERSION = "3"
const USE_MINIFORGE = true
"""
end
end
Expand All @@ -159,11 +185,12 @@ end
@testset "custom home" begin
preserve_build() do
mktempdir() do dir
withenv("CONDA_JL_VERSION" => "3", "CONDA_JL_HOME" => dir) do
withenv("CONDA_JL_VERSION" => "3", "CONDA_JL_HOME" => dir, "CONDA_JL_USE_MINIFORGE" => nothing) do
Pkg.build("Conda")
@test read(depsfile, String) == """
const ROOTENV = "$(escape_string(dir))"
const MINICONDA_VERSION = "3"
const USE_MINIFORGE = false
"""
end
end
Expand All @@ -176,22 +203,25 @@ end
write(depsfile, """
const ROOTENV = "$(escape_string(joinpath(condadir, "3")))"
const MINICONDA_VERSION = "2"
const USE_MINIFORGE = false
""")

withenv("CONDA_JL_VERSION" => nothing, "CONDA_JL_HOME" => nothing) do
withenv("CONDA_JL_VERSION" => nothing, "CONDA_JL_HOME" => nothing, "CONDA_JL_USE_MINIFORGE" => nothing) do
Pkg.build("Conda")
@test read(depsfile, String) == """
const ROOTENV = "$(escape_string(joinpath(condadir, "2")))"
const MINICONDA_VERSION = "2"
const USE_MINIFORGE = false
"""
end

# ROOTENV should be replaced since CONDA_JL_HOME wasn't explicitly set
withenv("CONDA_JL_VERSION" => "3", "CONDA_JL_HOME" => nothing) do
withenv("CONDA_JL_VERSION" => "3", "CONDA_JL_HOME" => nothing, "CONDA_JL_USE_MINIFORGE" => nothing) do
Pkg.build("Conda")
@test read(depsfile, String) == """
const ROOTENV = "$(escape_string(joinpath(condadir, "3")))"
const MINICONDA_VERSION = "3"
const USE_MINIFORGE = false
"""
end
end
Expand Down

0 comments on commit 15713e5

Please sign in to comment.