Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

conda install withenv #41

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 25 additions & 9 deletions src/Conda.jl
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,19 @@ const conda = joinpath(SCRIPTDIR, "conda")
"Path to the condarc file"
const CONDARC = joinpath(PREFIX, "condarc-julia")

"Run `cmd` with environment."
function run_with_env(cmd)
withenv(_get_conda_env()...) do
run(cmd)
end
end

"""
Use a cleaned up environment for the command `cmd`.
Get a cleaned up environment for the command `cmd`.

Any environment variable starting by CONDA will interact with the run.
"""
function _set_conda_env(cmd)
function _get_conda_env()
env = copy(ENV)
to_remove = AbstractString[]
for var in keys(env)
Expand All @@ -70,7 +77,16 @@ function _set_conda_env(cmd)
pop!(env, var)
end
env["CONDARC"] = CONDARC
setenv(cmd, env)
env
end

"""
Use a cleaned up environment for the command `cmd`.

Any environment variable starting by CONDA will interact with the run.
"""
function _set_conda_env(cmd)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you remove this function and replace other uses of if with the same withenv construct?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if so, it's need to refactor with the newer function like run_with_env.

run_with_env(`$conda install -y $pkg`)

run_with_env(`$conda remove -y $pkg`)

instead of

withenv(_get_conda_env()...) do
    run(`$conda install -y $pkg`)
end

run(_set_conda_env(`$conda remove -y $pkg`))

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, go for it! I just do not like to have two very similar functions with close names 😃

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changed something,
but it still needs the _set_conda_env function for
that is used to get the information from the commands.

 return collect(keys(JSON.parse(readstring(_set_conda_env(`$conda search $package --json`)))))

setenv(cmd, _get_conda_env())
end

"Get the miniconda installer URL."
Expand Down Expand Up @@ -143,20 +159,20 @@ end
"Install a new package."
function add(pkg::AbstractString)
_install_conda()
run(_set_conda_env(`$conda install -y $pkg`))
run_with_env(`$conda install -y $pkg`)
end

"Uninstall a package."
function rm(pkg::AbstractString)
_install_conda()
run(_set_conda_env(`$conda remove -y $pkg`))
run_with_env(`$conda remove -y $pkg`)
end

"Update all installed packages."
function update()
_install_conda()
for package in _installed_packages()
run(_set_conda_env(`$conda update -y $package`))
run_with_env(`$conda update -y $package`)
end
end

Expand Down Expand Up @@ -188,7 +204,7 @@ _installed_packages() = keys(_installed_packages_dict())
"List all installed packages to standard output."
function list()
_install_conda()
run(_set_conda_env(`$conda list`))
run_with_env(`$conda list`)
end

"Get the exact version of a package."
Expand Down Expand Up @@ -251,13 +267,13 @@ end
"Add a channel to the list of channels"
function add_channel(channel::Compat.String)
_install_conda()
run(_set_conda_env(`$conda config --add channels $channel --force`))
run_with_env(`$conda config --add channels $channel --force`)
end

"Remove a channel from the list of channels"
function rm_channel(channel::Compat.String)
_install_conda()
run(_set_conda_env(`$conda config --remove channels $channel --force`))
run_with_env(`$conda config --remove channels $channel --force`)
end

include("bindeps_conda.jl")
Expand Down
6 changes: 6 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,9 @@ Conda.add_channel("foo")
Conda.rm_channel("foo")
channels = Conda.channels()
@test (isempty(channels) || channels == ["defaults"])

# install qt
if is_windows()
Conda.add("qt")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should probably clean up after itself

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added removing qt. thanks.

Conda.rm("qt")
end