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

Resume Context structuring #2460

Merged
merged 4 commits into from
Apr 25, 2023
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
6 changes: 3 additions & 3 deletions docs/source/python_api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ The mamba_api exposes the following objects:
.. code::
from mamba import mamba_api
mamba_api.Context().conda_prefix = "/home/wolfv/conda"
mamba_api.Context().prefix_params.conda_prefix = "/home/wolfv/conda"
ctx = mamba_api.Context()
print(ctx.root_prefix)
print(ctx.prefix_params.root_prefix)
Here is an example usage of the mamba_api:
Expand Down Expand Up @@ -84,7 +84,7 @@ Here is an example usage of the mamba_api:
def __init__(self, prefix, channels, platform):
api_ctx = mamba_api.Context()
api_ctx.conda_prefix = prefix
api_ctx.prefix_params.conda_prefix = prefix
self.channels = channels
self.platform = platform
Expand Down
23 changes: 16 additions & 7 deletions libmamba/include/mamba/core/context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,15 +169,24 @@ namespace mamba
bool is_micromamba{ false };
};

struct ThreadsParams
{
std::size_t download_threads{ 5 };
int extract_threads{ 0 };
};

struct PrefixParams
{
fs::u8path target_prefix;
fs::u8path root_prefix;
fs::u8path conda_prefix;
fs::u8path relocate_prefix;
};

// Configurable
bool experimental = false;
bool debug = false;

fs::u8path target_prefix;
fs::u8path root_prefix;
fs::u8path conda_prefix;
fs::u8path relocate_prefix;

// TODO check writable and add other potential dirs
std::vector<fs::u8path> envs_dirs;
std::vector<fs::u8path> pkgs_dirs;
Expand All @@ -190,8 +199,6 @@ namespace mamba
ChannelPriority channel_priority = ChannelPriority::kFlexible;
bool auto_activate_base = false;

std::size_t download_threads = 5;
int extract_threads = 0;
bool extract_sparse = false;

bool dev = false; // TODO this is always used as default=false and isn't set anywhere => to
Expand Down Expand Up @@ -230,6 +237,8 @@ namespace mamba
GraphicsParams graphics_params;
SrcParams src_params;
CommandParams command_params;
ThreadsParams threads_params;
PrefixParams prefix_params;

std::map<std::string, std::string> proxy_servers;

Expand Down
10 changes: 5 additions & 5 deletions libmamba/src/api/clean.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,14 +117,14 @@ namespace mamba
}
}

if (fs::exists(ctx.root_prefix / "conda-meta"))
if (fs::exists(ctx.prefix_params.root_prefix / "conda-meta"))
{
envs.push_back(ctx.root_prefix);
envs.push_back(ctx.prefix_params.root_prefix);
}

if (fs::exists(ctx.root_prefix / "envs"))
if (fs::exists(ctx.prefix_params.root_prefix / "envs"))
{
for (auto& p : fs::directory_iterator(ctx.root_prefix / "envs"))
for (auto& p : fs::directory_iterator(ctx.prefix_params.root_prefix / "envs"))
{
if (p.is_directory() && fs::exists(p.path() / "conda-meta"))
{
Expand All @@ -137,7 +137,7 @@ namespace mamba
if (clean_trash)
{
Console::stream() << "Cleaning *.mamba_trash files" << std::endl;
clean_trash_files(ctx.root_prefix, true);
clean_trash_files(ctx.prefix_params.root_prefix, true);
}

// globally, collect installed packages
Expand Down
38 changes: 19 additions & 19 deletions libmamba/src/api/configuration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -663,7 +663,7 @@ namespace mamba
void target_prefix_checks_hook(int& options)
{
auto& ctx = Context::instance();
auto& prefix = ctx.target_prefix;
auto& prefix = ctx.prefix_params.target_prefix;

bool no_checks = options & MAMBA_NO_PREFIX_CHECK;
bool allow_missing = options & MAMBA_ALLOW_MISSING_PREFIX;
Expand Down Expand Up @@ -781,7 +781,7 @@ namespace mamba

std::vector<fs::u8path> fallback_envs_dirs_hook()
{
return { Context::instance().root_prefix / "envs" };
return { Context::instance().prefix_params.root_prefix / "envs" };
}

void envs_dirs_hook(std::vector<fs::u8path>& dirs)
Expand All @@ -799,7 +799,7 @@ namespace mamba

std::vector<fs::u8path> fallback_pkgs_dirs_hook()
{
std::vector<fs::u8path> paths = { Context::instance().root_prefix / "pkgs",
std::vector<fs::u8path> paths = { Context::instance().prefix_params.root_prefix / "pkgs",
env::home_directory() / ".mamba" / "pkgs" };
#ifdef _WIN32
auto appdata = env::get("APPDATA");
Expand Down Expand Up @@ -837,7 +837,7 @@ namespace mamba

void extract_threads_hook()
{
DownloadExtractSemaphore::set_max(Context::instance().extract_threads);
DownloadExtractSemaphore::set_max(Context::instance().threads_params.extract_threads);
}
}

Expand Down Expand Up @@ -1010,7 +1010,7 @@ namespace mamba
auto& ctx = Context::instance();

// Basic
insert(Configurable("root_prefix", &ctx.root_prefix)
insert(Configurable("root_prefix", &ctx.prefix_params.root_prefix)
.group("Basic")
.set_env_var_names()
.needs({ "create_base", "rc_files" })
Expand All @@ -1023,7 +1023,7 @@ namespace mamba
.set_single_op_lifetime()
.description("Define if base environment will be initialized empty"));

insert(Configurable("target_prefix", &ctx.target_prefix)
insert(Configurable("target_prefix", &ctx.prefix_params.target_prefix)
.group("Basic")
.set_env_var_names()
.needs({ "root_prefix",
Expand All @@ -1036,7 +1036,7 @@ namespace mamba
.set_post_merge_hook(detail::target_prefix_hook)
.set_post_context_hook(detail::post_target_prefix_rc_loading));

insert(Configurable("relocate_prefix", &ctx.relocate_prefix)
insert(Configurable("relocate_prefix", &ctx.prefix_params.relocate_prefix)
.group("Basic")
.set_env_var_names()
.needs({ "target_prefix" })
Expand Down Expand Up @@ -1364,7 +1364,7 @@ namespace mamba
.description("Allow downgrade when installing packages. Default is false."));

// Extract, Link & Install
insert(Configurable("download_threads", &ctx.download_threads)
insert(Configurable("download_threads", &ctx.threads_params.download_threads)
.group("Extract, Link & Install")
.set_rc_configurable()
.set_env_var_names()
Expand All @@ -1374,7 +1374,7 @@ namespace mamba
Defines the number of threads for package download.
It has to be strictly positive.)")));

insert(Configurable("extract_threads", &ctx.extract_threads)
insert(Configurable("extract_threads", &ctx.threads_params.extract_threads)
.group("Extract, Link & Install")
.set_rc_configurable()
.set_env_var_names()
Expand Down Expand Up @@ -1715,37 +1715,37 @@ namespace mamba
"C:\\ProgramData\\conda\\.mambarc" };
}

std::vector<fs::u8path> root = { ctx.root_prefix / ".condarc",
ctx.root_prefix / "condarc",
ctx.root_prefix / "condarc.d",
ctx.root_prefix / ".mambarc" };
std::vector<fs::u8path> root = { ctx.prefix_params.root_prefix / ".condarc",
ctx.prefix_params.root_prefix / "condarc",
ctx.prefix_params.root_prefix / "condarc.d",
ctx.prefix_params.root_prefix / ".mambarc" };

std::vector<fs::u8path> home = { env::home_directory() / ".conda/.condarc",
env::home_directory() / ".conda/condarc",
env::home_directory() / ".conda/condarc.d",
env::home_directory() / ".condarc",
env::home_directory() / ".mambarc" };

std::vector<fs::u8path> prefix = { ctx.target_prefix / ".condarc",
ctx.target_prefix / "condarc",
ctx.target_prefix / "condarc.d",
ctx.target_prefix / ".mambarc" };
std::vector<fs::u8path> prefix = { ctx.prefix_params.target_prefix / ".condarc",
ctx.prefix_params.target_prefix / "condarc",
ctx.prefix_params.target_prefix / "condarc.d",
ctx.prefix_params.target_prefix / ".mambarc" };

std::vector<fs::u8path> sources;

if (level >= RCConfigLevel::kSystemDir)
{
sources.insert(sources.end(), system.begin(), system.end());
}
if ((level >= RCConfigLevel::kRootPrefix) && !ctx.root_prefix.empty())
if ((level >= RCConfigLevel::kRootPrefix) && !ctx.prefix_params.root_prefix.empty())
{
sources.insert(sources.end(), root.begin(), root.end());
}
if (level >= RCConfigLevel::kHomeDir)
{
sources.insert(sources.end(), home.begin(), home.end());
}
if ((level >= RCConfigLevel::kTargetPrefix) && !ctx.target_prefix.empty())
if ((level >= RCConfigLevel::kTargetPrefix) && !ctx.prefix_params.target_prefix.empty())
{
sources.insert(sources.end(), prefix.begin(), prefix.end());
}
Expand Down
15 changes: 8 additions & 7 deletions libmamba/src/api/create.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,22 @@ namespace mamba

if (!ctx.dry_run)
{
if (fs::exists(ctx.target_prefix))
if (fs::exists(ctx.prefix_params.target_prefix))
{
if (ctx.target_prefix == ctx.root_prefix)
if (ctx.prefix_params.target_prefix == ctx.prefix_params.root_prefix)
{
LOG_ERROR << "Overwriting root prefix is not permitted";
throw std::runtime_error("Aborting.");
}
else if (fs::exists(ctx.target_prefix / "conda-meta"))
else if (fs::exists(ctx.prefix_params.target_prefix / "conda-meta"))
{
if (Console::prompt(
"Found conda-prefix at '" + ctx.target_prefix.string() + "'. Overwrite?",
"Found conda-prefix at '" + ctx.prefix_params.target_prefix.string()
+ "'. Overwrite?",
'n'
))
{
fs::remove_all(ctx.target_prefix);
fs::remove_all(ctx.prefix_params.target_prefix);
}
else
{
Expand All @@ -60,12 +61,12 @@ namespace mamba
}
if (create_specs.empty())
{
detail::create_empty_target(ctx.target_prefix);
detail::create_empty_target(ctx.prefix_params.target_prefix);
}

if (config.at("platform").configured() && !config.at("platform").rc_configured())
{
detail::store_platform_config(ctx.target_prefix, ctx.platform);
detail::store_platform_config(ctx.prefix_params.target_prefix, ctx.platform);
}
}

Expand Down
17 changes: 9 additions & 8 deletions libmamba/src/api/info.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,25 +98,26 @@ namespace mamba
std::vector<std::tuple<std::string, nlohmann::json>> items;

std::string name, location;
if (!ctx.target_prefix.empty())
if (!ctx.prefix_params.target_prefix.empty())
{
name = env_name(ctx.target_prefix);
location = ctx.target_prefix.string();
name = env_name(ctx.prefix_params.target_prefix);
location = ctx.prefix_params.target_prefix.string();
}
else
{
name = "None";
location = "-";
}

if (std::getenv("CONDA_PREFIX") && (std::getenv("CONDA_PREFIX") == ctx.target_prefix))
if (std::getenv("CONDA_PREFIX")
&& (std::getenv("CONDA_PREFIX") == ctx.prefix_params.target_prefix))
{
name += " (active)";
}
else if (fs::exists(ctx.target_prefix))
else if (fs::exists(ctx.prefix_params.target_prefix))
{
if (!(fs::exists(ctx.target_prefix / "conda-meta")
|| (ctx.target_prefix == ctx.root_prefix)))
if (!(fs::exists(ctx.prefix_params.target_prefix / "conda-meta")
|| (ctx.prefix_params.target_prefix == ctx.prefix_params.root_prefix)))
{
name += " (not env)";
}
Expand Down Expand Up @@ -172,7 +173,7 @@ namespace mamba
}
items.push_back({ "channels", channel_urls });

items.push_back({ "base environment", ctx.root_prefix.string() });
items.push_back({ "base environment", ctx.prefix_params.root_prefix.string() });

items.push_back({ "platform", ctx.platform });

Expand Down
25 changes: 14 additions & 11 deletions libmamba/src/api/install.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ namespace mamba
{
const auto maybe_instructions = get_other_pkg_mgr_install_instructions(
pkg_mgr,
ctx.target_prefix.string(),
ctx.prefix_params.target_prefix.string(),
specs.path()
);
if (maybe_instructions)
Expand All @@ -136,7 +136,10 @@ namespace mamba
}
}();

auto [wrapped_command, tmpfile] = prepare_wrapped_call(ctx.target_prefix, install_instructions);
auto [wrapped_command, tmpfile] = prepare_wrapped_call(
ctx.prefix_params.target_prefix,
install_instructions
);

reproc::options options;
options.redirect.parent = true;
Expand Down Expand Up @@ -433,14 +436,14 @@ namespace mamba
auto& only_deps = config.at("only_deps").value<bool>();
auto& retry_clean_cache = config.at("retry_clean_cache").value<bool>();

if (ctx.target_prefix.empty())
if (ctx.prefix_params.target_prefix.empty())
{
throw std::runtime_error("No active target prefix");
}
if (!fs::exists(ctx.target_prefix) && create_env == false)
if (!fs::exists(ctx.prefix_params.target_prefix) && create_env == false)
{
throw std::runtime_error(
fmt::format("Prefix does not exist at: {}", ctx.target_prefix.string())
fmt::format("Prefix does not exist at: {}", ctx.prefix_params.target_prefix.string())
);
}

Expand All @@ -465,16 +468,16 @@ namespace mamba
// which limits this syntax
/*auto exp_prefix_data = load_channels(pool, package_caches, is_retry)
.and_then([&ctx](const auto&) { return
PrefixData::create(ctx.target_prefix); } ) .map_error([](const mamba_error& err) { throw
std::runtime_error(err.what());
PrefixData::create(ctx.prefix_params.target_prefix); } ) .map_error([](const mamba_error&
err) { throw std::runtime_error(err.what());
});*/
auto exp_load = load_channels(pool, package_caches, is_retry);
if (!exp_load)
{
throw std::runtime_error(exp_load.error().what());
}

auto exp_prefix_data = PrefixData::create(ctx.target_prefix);
auto exp_prefix_data = PrefixData::create(ctx.prefix_params.target_prefix);
if (!exp_prefix_data)
{
throw std::runtime_error(exp_prefix_data.error().what());
Expand Down Expand Up @@ -577,7 +580,7 @@ namespace mamba
{
if (create_env && !Context::instance().dry_run)
{
detail::create_target_directory(ctx.target_prefix);
detail::create_target_directory(ctx.prefix_params.target_prefix);
}

trans.execute(prefix_data);
Expand All @@ -598,7 +601,7 @@ namespace mamba
{
MPool pool;
auto& ctx = Context::instance();
auto exp_prefix_data = PrefixData::create(ctx.target_prefix);
auto exp_prefix_data = PrefixData::create(ctx.prefix_params.target_prefix);
if (!exp_prefix_data)
{
// TODO: propagate tl::expected mechanism
Expand All @@ -625,7 +628,7 @@ namespace mamba
{
if (create_env && !Context::instance().dry_run)
{
detail::create_target_directory(ctx.target_prefix);
detail::create_target_directory(ctx.prefix_params.target_prefix);
}

transaction.execute(prefix_data);
Expand Down
Loading