Skip to content
Merged
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
5 changes: 4 additions & 1 deletion libmamba/include/mamba/core/activation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ namespace mamba
const std::string& conda_default_env,
int old_conda_shlvl);

std::vector<fs::path> get_path_dirs(const fs::path& prefix);
std::vector<fs::path> get_clean_dirs();

std::string add_prefix_to_path(const fs::path& prefix, int old_conda_shlvl);
Expand Down Expand Up @@ -181,6 +180,10 @@ namespace mamba
std::string hook_postamble() override;
fs::path hook_source_path() override;
};


std::vector<fs::path> get_path_dirs(const fs::path& prefix);

} // namespace mamba

#endif
1 change: 1 addition & 0 deletions libmamba/include/mamba/core/environment.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ namespace mamba
void unset(const std::string& key);

fs::path which(const std::string& exe, const std::string& override_path = "");
fs::path which(const std::string& exe, const std::vector<fs::path>& search_paths);
std::map<std::string, std::string> copy();
std::string platform();
fs::path home_directory();
Expand Down
39 changes: 31 additions & 8 deletions libmamba/src/api/install.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,33 @@
#include "mamba/core/util.hpp"
#include "mamba/core/virtual_packages.hpp"
#include "mamba/core/env_lockfile.hpp"
#include "mamba/core/activation.hpp"

#include "termcolor/termcolor.hpp"

namespace mamba
{
namespace
{
std::map<std::string, std::string> other_pkg_mgr_install_instructions
= { { "pip", "python -m pip install -r {0} --no-input" } };
tl::expected<std::string, std::runtime_error> get_other_pkg_mgr_install_instructions(
const std::string& name, const std::string& target_prefix)
{
const auto get_python_path
= [&] { return env::which("python", get_path_dirs(target_prefix)).u8string(); };

const std::unordered_map<std::string, std::string> other_pkg_mgr_install_instructions{
{ "pip",
fmt::format("{} {}", get_python_path(), "-m pip install -r {0} --no-input") }
};

auto found_it = other_pkg_mgr_install_instructions.find(name);
if (found_it != other_pkg_mgr_install_instructions.end())
return found_it->second;
else
return tl::unexpected(std::runtime_error(
fmt::format("no install instruction found for package manager '{}'", name)));
}

}

bool reproc_killed(int status)
Expand Down Expand Up @@ -70,7 +88,17 @@ namespace mamba
const auto& deps = other_spec.deps;
const auto& cwd = other_spec.cwd;

std::string install_instructions = other_pkg_mgr_install_instructions[pkg_mgr];
const auto& ctx = Context::instance();

std::string install_instructions = [&]
{
const auto maybe_instructions
= get_other_pkg_mgr_install_instructions(pkg_mgr, ctx.target_prefix);
if (maybe_instructions)
return maybe_instructions.value();
else
throw maybe_instructions.error();
}();

TemporaryFile specs;
std::ofstream specs_f = open_ofstream(specs.path());
Expand All @@ -80,12 +108,7 @@ namespace mamba

replace_all(install_instructions, "{0}", specs.path());

const auto& ctx = Context::instance();

std::vector<std::string> install_args = split(install_instructions, " ");

install_args[0]
= (ctx.target_prefix / get_bin_directory_short_path() / install_args[0]).string();
auto [wrapped_command, tmpfile] = prepare_wrapped_call(ctx.target_prefix, install_args);

reproc::options options;
Expand Down
2 changes: 1 addition & 1 deletion libmamba/src/core/activation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ namespace mamba
}
}

std::vector<fs::path> Activator::get_path_dirs(const fs::path& prefix)
std::vector<fs::path> get_path_dirs(const fs::path& prefix)
{
if (on_win)
{
Expand Down
50 changes: 35 additions & 15 deletions libmamba/src/core/environment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,23 +81,12 @@ namespace mamba
if (env_path)
{
std::string path = env_path.value();
auto parts = mamba::split(path, pathsep());
for (auto& p : parts)
{
if (!fs::exists(p) || !fs::is_directory(p))
{
continue;
}
for (const auto& entry : fs::directory_iterator(p))
{
if (entry.path().filename() == exe)
{
return entry.path();
}
}
}
const auto parts = mamba::split(path, pathsep());
const std::vector<fs::path> search_paths(parts.begin(), parts.end());
return which(exe, search_paths);
}


#ifndef _WIN32
if (override_path == "")
{
Expand All @@ -115,6 +104,37 @@ namespace mamba
return ""; // empty path
}

fs::path which(const std::string& exe, const std::vector<fs::path>& search_paths)
{
for (auto& p : search_paths)
{
if (!fs::exists(p) || !fs::is_directory(p))
{
continue;
}

#ifdef _WIN32
const auto exe_with_extension = exe + ".exe";
#endif
for (const auto& entry : fs::directory_iterator(p))
{
const auto filename = entry.path().filename();
if (filename == exe

#ifdef _WIN32
|| filename == exe_with_extension
#endif
)
{
return entry.path();
}
}
}


return ""; // empty path
}

std::map<std::string, std::string> copy()
{
std::map<std::string, std::string> m;
Expand Down