module Conda

Constants

BINDIR
CONDA
CONDARC
LIBDIR
PREFIX
PYTHONDIR
SCRIPTDIR

Public Class Methods

_install_conda(force=false) click to toggle source
# File lib/conda.rb, line 64
def Conda._install_conda(force=false)

    if force || !(File.file? (CONDA))
        # Ensure PREFIX exists
        Dir.mkdir(PREFIX) unless File.exists?(PREFIX)
        print("Downloading miniconda installer ...\n")
        if Gem.win_platform?
            installer = File.join(PREFIX, "installer.exe")
        else
            installer = File.join(PREFIX, "installer.sh")
        end
        IO.copy_stream(open(Conda._installer_url()), installer)

        print("Installing miniconda ...\n")
        if Gem.win_platform?
            prefix = PREFIX.gsub("/", "\\")
            `#{installer} /S /AddToPath=0 /RegisterPython=0 /D=#{prefix} 1>&2`
        else
            `chmod 755 #{installer}`
            `#{installer} -b -f -p #{PREFIX}`
        end
        Conda.add_channel("defaults")
    end
end
_installer_url() click to toggle source
# File lib/conda.rb, line 47
def Conda._installer_url()
    res = "https://repo.continuum.io/miniconda/Miniconda3-latest-"
    if RbConfig::CONFIG["host"].include? "darwin"
        res += "MacOSX"
    elsif RbConfig::CONFIG["host"].include? "linux"
        res += "Linux"
    elsif Gem.win_platform?
        res += "Windows"
    else
        raise("Unsuported OS.")
    end
    res += (RbConfig::CONFIG["host"].include? "x86_64") ? "-x86_64" : "-x86"
    res += Gem.win_platform? ? ".exe" : ".sh"
    return res
end
add(pkg) click to toggle source
# File lib/conda.rb, line 90
def Conda.add(pkg)
    Conda._install_conda()
    Conda.run_in_env("#{CONDA} install -y #{pkg} 1>&2")
end
add_channel(channel) click to toggle source
# File lib/conda.rb, line 185
def Conda.add_channel(channel)
    Conda._install_conda()
    Conda.run_in_env("#{CONDA} config --add channels #{channel} --force")
end
channels() click to toggle source
# File lib/conda.rb, line 174
def Conda.channels()
    Conda._install_conda()
    ret=JSON.parse(Conda.run_in_env("#{CONDA} config --get channels --json"))
    if ret["get"].has_key? "channels"
        return ret["get"]["channels"]
    else
        return []
    end
end
exists(package) click to toggle source
# File lib/conda.rb, line 164
def Conda.exists(package)
    if package.include? ("==")
      pkg,ver=package.split("==")  # Remove version if provided
      return Conda.search(pkg,ver).include? pkg
    else
      return Conda.search(package).include? package
    end
end
installed_packages() click to toggle source
# File lib/conda.rb, line 123
def Conda.installed_packages()
    return Conda.installed_packages_dict().keys
end
installed_packages_dict() click to toggle source
# File lib/conda.rb, line 110
def Conda.installed_packages_dict()
    Conda._install_conda()
    package_dict = {}
    for line in Conda.run_in_env("#{CONDA} list --export").split()
        if not line.start_with? "#"
            name, version, build_string = line.split("=")
            package_dict[name] = [Gem::Version.new(version), line]
        end
    end
    return package_dict
end
list() click to toggle source
# File lib/conda.rb, line 128
def Conda.list()
    Conda._install_conda()
    Conda.run_in_env("#{CONDA} list 1>&2")
end
rm(pkg) click to toggle source
# File lib/conda.rb, line 96
def Conda.rm(pkg)
    Conda._install_conda()
    run_in_env("#{CONDA} remove -y #{pkg} 1>&2")
end
rm_channel(channel) click to toggle source
# File lib/conda.rb, line 191
def Conda.rm_channel(channel)
    Conda._install_conda()
    Conda.run_in_env("#{CONDA} config --remove channels #{channel} --force")
end
run_in_env(cmd) click to toggle source
# File lib/conda.rb, line 30
def Conda.run_in_env(cmd)
    env = ENV.to_hash
    to_remove = []
    for var in env.keys
        if var.start_with?("CONDA")
            to_remove << var
        end
    end
    for var in to_remove
        env[var] = nil
    end
    env["CONDARC"] = CONDARC
    ENV.replace(env)
    return `#{cmd}`
end
update() click to toggle source
# File lib/conda.rb, line 102
def Conda.update()
    Conda._install_conda()
    for pkg in Conda.installed_packages()
        run_in_env("#{CONDA} update -y #{pkg} 1>&2")
    end
end
version(name) click to toggle source
# File lib/conda.rb, line 134
def Conda.version(name)
    Conda._install_conda()
    packages = JSON.parse(run_in_env("#{CONDA} list --json"))
    for package in packages
        if package.startswith? name or package.include? "::#{name}"
            return package
        end
    end
    raise("Could not find the #{name} package")
end