Skip to content

Commit

Permalink
First pass on context structuring
Browse files Browse the repository at this point in the history
  • Loading branch information
Hind-M committed Apr 3, 2023
1 parent 19b872d commit ff3d3de
Show file tree
Hide file tree
Showing 24 changed files with 257 additions and 203 deletions.
50 changes: 32 additions & 18 deletions libmamba/include/mamba/core/context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,32 @@ namespace mamba
{
public:

struct RemoteFetchInfo
{
// ssl_verify can be either an empty string (regular SSL verification),
// the string "<false>" to indicate no SSL verification, or a path to
// a directory with cert files, or a cert file.
std::string ssl_verify{ "" };
bool ssl_no_revoke{ false };

std::string user_agent{ "mamba/" LIBMAMBA_VERSION_STRING };

int connect_timeout_secs{ 10 };
// int read_timeout_secs { 60 };
int retry_timeout{ 2 }; // seconds
int retry_backoff{ 3 }; // retry_timeout * retry_backoff
int max_retries{ 3 }; // max number of retries
};

struct OutputInfo
{
int verbosity{ 0 };
log_level logging_level{ log_level::warn };

bool json{ false };
bool quiet{ false };
};

std::string caller_version = "";
std::string conda_version = "3.8.0";
std::string current_command = "mamba";
Expand All @@ -138,20 +164,14 @@ namespace mamba
bool use_index_cache = false;
std::size_t local_repodata_ttl = 1; // take from header
bool offline = false;
bool quiet = false;
bool json = false;

ChannelPriority channel_priority = ChannelPriority::kFlexible;
bool auto_activate_base = false;

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

int verbosity = 0;
void set_verbosity(int lvl);
void set_log_level(log_level level);

log_level logging_level = log_level::warn;
std::string log_pattern = "%^%-9!l%-8n%$ %v";
std::size_t log_backtrace = 0;

Expand Down Expand Up @@ -189,19 +209,10 @@ namespace mamba
// micromamba only
bool shell_completion = true;

std::string user_agent = "mamba/" LIBMAMBA_VERSION_STRING;
int connect_timeout_secs = 10;
// int read_timeout_secs = 60;
int retry_timeout = 2; // seconds
int retry_backoff = 3; // retry_timeout * retry_backoff
int max_retries = 3; // max number of retries
RemoteFetchInfo remote_fetch_info;
OutputInfo output_info;

std::map<std::string, std::string> proxy_servers;
// ssl verify can be either an empty string (regular SSL verification),
// the string "<false>" to indicate no SSL verification, or a path to
// a directory with cert files, or a cert file.
std::string ssl_verify = "";
bool ssl_no_revoke = false;

bool no_rc = false;
bool no_env = false;
Expand Down Expand Up @@ -263,6 +274,9 @@ namespace mamba
void debug_print() const;
void dump_backtrace_no_guards();

void set_verbosity(int lvl);
void set_log_level(log_level level);

protected:

Context();
Expand Down
24 changes: 12 additions & 12 deletions libmamba/src/api/configuration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -630,13 +630,13 @@ namespace mamba
{
auto& ctx = Context::instance();

if (ctx.json)
if (ctx.output_info.json)
{
return mamba::log_level::off;
}
else if (Configuration::instance().at("verbose").configured())
{
switch (ctx.verbosity)
switch (ctx.output_info.verbosity)
{
case 0:
return mamba::log_level::warn;
Expand All @@ -657,7 +657,7 @@ namespace mamba
void verbose_hook(int& lvl)
{
auto& ctx = Context::instance();
ctx.verbosity = lvl;
ctx.output_info.verbosity = lvl;
}

void target_prefix_checks_hook(int& options)
Expand Down Expand Up @@ -1210,7 +1210,7 @@ namespace mamba
.set_env_var_names()
.description("Force use cached repodata"));

insert(Configurable("ssl_no_revoke", &ctx.ssl_no_revoke)
insert(Configurable("ssl_no_revoke", &ctx.remote_fetch_info.ssl_no_revoke)
.group("Network")
.set_rc_configurable()
.set_env_var_names()
Expand All @@ -1220,7 +1220,7 @@ namespace mamba
It's only working for Windows back-end.
WARNING: this option loosens the SSL security.)")));

insert(Configurable("ssl_verify", &ctx.ssl_verify)
insert(Configurable("ssl_verify", &ctx.remote_fetch_info.ssl_verify)
.group("Network")
.set_rc_configurable()
.set_env_var_names()
Expand All @@ -1242,22 +1242,22 @@ namespace mamba
the value is the url of the proxy server, optionally with username and password
in the form of scheme://username:password@hostname.)")));

insert(Configurable("remote_connect_timeout_secs", &ctx.connect_timeout_secs)
insert(Configurable("remote_connect_timeout_secs", &ctx.remote_fetch_info.connect_timeout_secs)
.group("Network")
.set_rc_configurable()
.set_env_var_names()
.description(
"The number seconds conda will wait for your client to establish a connection to a remote url resource."
));

insert(Configurable("remote_backoff_factor", &ctx.retry_backoff)
insert(Configurable("remote_backoff_factor", &ctx.remote_fetch_info.retry_backoff)
.group("Network")
.set_rc_configurable()
.set_env_var_names()
.description("The factor determines the time HTTP connection should wait for attempt."
));

insert(Configurable("remote_max_retries", &ctx.max_retries)
insert(Configurable("remote_max_retries", &ctx.remote_fetch_info.max_retries)
.group("Network")
.set_rc_configurable()
.set_env_var_names()
Expand Down Expand Up @@ -1500,7 +1500,7 @@ namespace mamba
.description("Only download and extract packages, do not link them into environment."
));

insert(Configurable("log_level", &ctx.logging_level)
insert(Configurable("log_level", &ctx.output_info.logging_level)
.group("Output, Prompt and Flow Control")
.set_rc_configurable()
.set_env_var_names()
Expand Down Expand Up @@ -1529,7 +1529,7 @@ namespace mamba
.long_description(unindent(R"(
Set the log pattern.)")));

insert(Configurable("json", &ctx.json)
insert(Configurable("json", &ctx.output_info.json)
.group("Output, Prompt and Flow Control")
.set_rc_configurable()
.needs({ "print_config_only", "print_context_only" })
Expand Down Expand Up @@ -1612,7 +1612,7 @@ namespace mamba
.group("Output, Prompt and Flow Control")
.description("Display configs values"));

insert(Configurable("quiet", &ctx.quiet)
insert(Configurable("quiet", &ctx.output_info.quiet)
.group("Output, Prompt and Flow Control")
.set_rc_configurable()
.set_env_var_names()
Expand Down Expand Up @@ -1783,7 +1783,7 @@ namespace mamba
}

auto& ctx = Context::instance();
ctx.set_log_level(ctx.logging_level);
ctx.set_log_level(ctx.output_info.logging_level);

spdlog::apply_all([&](std::shared_ptr<spdlog::logger> l) { l->flush(); });
spdlog::flush_on(spdlog::level::off);
Expand Down
2 changes: 1 addition & 1 deletion libmamba/src/api/info.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ namespace mamba
{
void info_pretty_print(std::vector<std::tuple<std::string, nlohmann::json>> items)
{
if (Context::instance().json)
if (Context::instance().output_info.json)
{
return;
}
Expand Down
6 changes: 3 additions & 3 deletions libmamba/src/api/install.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,7 @@ namespace mamba
Console::instance().print("Possible hints:\n - 'freeze_installed' is turned on\n");
}

if (ctx.json)
if (ctx.output_info.json)
{
Console::instance().json_write({ { "success", false },
{ "solver_problems", solver.all_problems() } });
Expand All @@ -563,7 +563,7 @@ namespace mamba

MTransaction trans(pool, solver, package_caches);

if (ctx.json)
if (ctx.output_info.json)
{
trans.log_json();
}
Expand Down Expand Up @@ -613,7 +613,7 @@ namespace mamba
// so they must have been ready in the pool before this line
auto transaction = create_transaction(pool, pkg_caches, others);

if (ctx.json)
if (ctx.output_info.json)
{
transaction.log_json();
}
Expand Down
2 changes: 1 addition & 1 deletion libmamba/src/api/list.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ namespace mamba

std::regex spec_pat(regex);

if (ctx.json)
if (ctx.output_info.json)
{
auto jout = nlohmann::json::array();
std::vector<std::string> keys;
Expand Down
2 changes: 1 addition & 1 deletion libmamba/src/api/remove.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ namespace mamba

auto execute_transaction = [&](MTransaction& transaction)
{
if (ctx.json)
if (ctx.output_info.json)
{
transaction.log_json();
}
Expand Down
2 changes: 1 addition & 1 deletion libmamba/src/api/repoquery.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ namespace mamba
Query q(pool);
if (type == QueryType::kSEARCH)
{
if (ctx.json)
if (ctx.output_info.json)
{
std::cout << q.find(query).groupby("name").json().dump(4);
}
Expand Down
2 changes: 1 addition & 1 deletion libmamba/src/api/shell.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ namespace mamba
else if (action == "hook")
{
// TODO do we need to do something wtih `shell_prefix -> root_prefix?`?
if (ctx.json)
if (ctx.output_info.json)
{
Console::instance().json_write(
{ { "success", true },
Expand Down
2 changes: 1 addition & 1 deletion libmamba/src/api/update.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ namespace mamba

auto execute_transaction = [&](MTransaction& transaction)
{
if (ctx.json)
if (ctx.output_info.json)
{
transaction.log_json();
}
Expand Down
40 changes: 20 additions & 20 deletions libmamba/src/core/context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,48 +116,48 @@ namespace mamba

spdlog::set_default_logger(l);
logger = std::dynamic_pointer_cast<Logger>(l);
spdlog::set_level(convert_log_level(logging_level));
spdlog::set_level(convert_log_level(output_info.logging_level));
}

Context::~Context() = default;

void Context::set_verbosity(int lvl)
{
this->verbosity = lvl;
this->output_info.verbosity = lvl;

switch (lvl)
{
case -3:
this->logging_level = log_level::off;
this->output_info.logging_level = log_level::off;
break;
case -2:
this->logging_level = log_level::critical;
this->output_info.logging_level = log_level::critical;
break;
case -1:
this->logging_level = log_level::err;
this->output_info.logging_level = log_level::err;
break;
case 0:
this->logging_level = log_level::warn;
this->output_info.logging_level = log_level::warn;
break;
case 1:
this->logging_level = log_level::info;
this->output_info.logging_level = log_level::info;
break;
case 2:
this->logging_level = log_level::debug;
this->output_info.logging_level = log_level::debug;
break;
case 3:
this->logging_level = log_level::trace;
this->output_info.logging_level = log_level::trace;
break;
default:
this->logging_level = log_level::info;
this->output_info.logging_level = log_level::info;
break;
}
spdlog::set_level(convert_log_level(logging_level));
spdlog::set_level(convert_log_level(output_info.logging_level));
}

void Context::set_log_level(log_level level)
{
logging_level = level;
output_info.logging_level = level;
spdlog::set_level(convert_log_level(level));
}

Expand Down Expand Up @@ -328,22 +328,22 @@ namespace mamba
PRINT_CTX(out, always_yes);
PRINT_CTX(out, allow_softlinks);
PRINT_CTX(out, offline);
PRINT_CTX(out, quiet);
PRINT_CTX(out, output_info.quiet);
PRINT_CTX(out, no_rc);
PRINT_CTX(out, no_env);
PRINT_CTX(out, ssl_no_revoke);
PRINT_CTX(out, ssl_verify);
PRINT_CTX(out, retry_timeout);
PRINT_CTX(out, retry_backoff);
PRINT_CTX(out, max_retries);
PRINT_CTX(out, connect_timeout_secs);
PRINT_CTX(out, remote_fetch_info.ssl_no_revoke);
PRINT_CTX(out, remote_fetch_info.ssl_verify);
PRINT_CTX(out, remote_fetch_info.retry_timeout);
PRINT_CTX(out, remote_fetch_info.retry_backoff);
PRINT_CTX(out, remote_fetch_info.max_retries);
PRINT_CTX(out, remote_fetch_info.connect_timeout_secs);
PRINT_CTX(out, add_pip_as_python_dependency);
PRINT_CTX(out, override_channels_enabled);
PRINT_CTX(out, use_only_tar_bz2);
PRINT_CTX(out, auto_activate_base);
PRINT_CTX(out, extra_safety_checks);
PRINT_CTX(out, download_threads);
PRINT_CTX(out, verbosity);
PRINT_CTX(out, output_info.verbosity);
PRINT_CTX(out, channel_alias);
out << "channel_priority: " << static_cast<int>(channel_priority) << '\n';
PRINT_CTX_VEC(out, default_channels);
Expand Down
2 changes: 1 addition & 1 deletion libmamba/src/core/curl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ namespace mamba

// DO NOT SET TIMEOUT as it will also take into account multi-start time and
// it's just wrong curl_easy_setopt(m_handle, CURLOPT_TIMEOUT,
// Context::instance().read_timeout_secs);
// Context::instance().remote_fetch_info.read_timeout_secs);

// TODO while libcurl in conda now _has_ http2 support we need to fix mamba to
// work properly with it this includes:
Expand Down
Loading

0 comments on commit ff3d3de

Please sign in to comment.