Skip to content

Commit

Permalink
Context structuring (#2432)
Browse files Browse the repository at this point in the history
Context structuring

Co-authored-by: Johan Mabille <johan.mabille@gmail.com>
  • Loading branch information
Hind-M and JohanMabille authored Apr 12, 2023
1 parent 032e404 commit 40c220c
Show file tree
Hide file tree
Showing 39 changed files with 382 additions and 289 deletions.
2 changes: 1 addition & 1 deletion libmamba/include/mamba/api/configuration.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -708,7 +708,7 @@ namespace mamba
}
}

if (rc_configured() && !ctx.no_rc && (level >= ConfigurationLevel::kFile))
if (rc_configured() && !ctx.src_params.no_rc && (level >= ConfigurationLevel::kFile))
{
m_sources.insert(m_sources.end(), m_rc_sources.begin(), m_rc_sources.end());
m_values.insert(m_rc_values.begin(), m_rc_values.end());
Expand Down
101 changes: 66 additions & 35 deletions libmamba/include/mamba/core/context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,58 @@ namespace mamba
{
public:

std::string caller_version = "";
std::string conda_version = "3.8.0";
std::string current_command = "mamba";
std::string custom_banner = "";
bool is_micromamba = false;
struct RemoteFetchParams
{
// 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 curl_initialized{ false }; // non configurable, used in fetch only

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 OutputParams
{
int verbosity{ 0 };
log_level logging_level{ log_level::warn };

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

std::string log_pattern{ "%^%-9!l%-8n%$ %v" };
std::size_t log_backtrace{ 0 };
};

struct GraphicsParams
{
bool no_progress_bars{ false };
Palette palette;
};

struct SrcParams
{
bool no_rc{ false };
bool no_env{ false };
};

struct CommandParams
{
std::string caller_version{ "" };
std::string conda_version{ "3.8.0" };
std::string current_command{ "mamba" };
std::string custom_banner{ "" };
bool is_micromamba{ false };
};

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

Expand All @@ -139,32 +186,20 @@ 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;

bool dev = false;
bool on_ci = false;
bool dev = false; // TODO this is always used as default=false and isn't set anywhere => to
// be removed if this is the case...
bool dry_run = false;
bool download_only = false;
bool always_yes = false;

bool no_progress_bars = false;
Palette palette;

bool allow_softlinks = false;
bool always_copy = false;
bool always_softlink = false;
Expand All @@ -190,23 +225,13 @@ namespace mamba
// micromamba only
bool shell_completion = true;

std::string user_agent = "mamba/" LIBMAMBA_VERSION_STRING;
bool curl_initialized = false;
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
RemoteFetchParams remote_fetch_params;
OutputParams output_params;
GraphicsParams graphics_params;
SrcParams src_params;
CommandParams command_params;

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;

std::size_t lock_timeout = 0;
bool use_lockfiles = true;
Expand Down Expand Up @@ -265,6 +290,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 All @@ -273,6 +301,9 @@ namespace mamba

private:

// Used internally
bool on_ci = false;

void load_authentication_info();
std::map<std::string, AuthenticationInfo> m_authentication_info;
bool m_authentication_infos_loaded = false;
Expand Down
48 changes: 24 additions & 24 deletions libmamba/src/api/configuration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ namespace mamba
{
bool ConfigurableImplBase::env_var_configured() const
{
if (Context::instance().no_env)
if (Context::instance().src_params.no_env)
{
return false;
}
Expand All @@ -50,12 +50,12 @@ namespace mamba

bool ConfigurableImplBase::env_var_active() const
{
return !Context::instance().no_env || (m_name == "no_env");
return !Context::instance().src_params.no_env || (m_name == "no_env");
}

bool ConfigurableImplBase::rc_configured() const
{
return m_rc_configured && !Context::instance().no_rc;
return m_rc_configured && !Context::instance().src_params.no_rc;
}
}

Expand Down Expand Up @@ -609,7 +609,7 @@ namespace mamba
void post_root_prefix_rc_loading()
{
auto& config = Configuration::instance();
if (!Context::instance().no_rc)
if (!Context::instance().src_params.no_rc)
{
rc_loading_hook(RCConfigLevel::kHomeDir);
config.at("no_env").compute(MAMBA_CONF_FORCE_COMPUTE);
Expand All @@ -619,7 +619,7 @@ namespace mamba
void post_target_prefix_rc_loading()
{
auto& config = Configuration::instance();
if (!Context::instance().no_rc)
if (!Context::instance().src_params.no_rc)
{
rc_loading_hook(RCConfigLevel::kTargetPrefix);
config.at("no_env").compute(MAMBA_CONF_FORCE_COMPUTE);
Expand All @@ -630,13 +630,13 @@ namespace mamba
{
auto& ctx = Context::instance();

if (ctx.json)
if (ctx.output_params.json)
{
return mamba::log_level::off;
}
else if (Configuration::instance().at("verbose").configured())
{
switch (ctx.verbosity)
switch (ctx.output_params.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_params.verbosity = lvl;
}

void target_prefix_checks_hook(int& options)
Expand Down Expand Up @@ -717,7 +717,7 @@ namespace mamba

if (!files.empty())
{
if (ctx.no_rc)
if (ctx.src_params.no_rc)
{
LOG_ERROR << "Configuration files disabled by 'no_rc'";
throw std::runtime_error("Incompatible configuration. Aborting.");
Expand Down Expand Up @@ -1217,7 +1217,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_params.ssl_no_revoke)
.group("Network")
.set_rc_configurable()
.set_env_var_names()
Expand All @@ -1227,7 +1227,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_params.ssl_verify)
.group("Network")
.set_rc_configurable()
.set_env_var_names()
Expand All @@ -1249,22 +1249,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_params.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_params.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_params.max_retries)
.group("Network")
.set_rc_configurable()
.set_env_var_names()
Expand Down Expand Up @@ -1507,7 +1507,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_params.logging_level)
.group("Output, Prompt and Flow Control")
.set_rc_configurable()
.set_env_var_names()
Expand All @@ -1519,7 +1519,7 @@ namespace mamba
be one of {'off', 'fatal', 'error', 'warning', 'info',
'debug', 'trace'}.)")));

insert(Configurable("log_backtrace", &ctx.log_backtrace)
insert(Configurable("log_backtrace", &ctx.output_params.log_backtrace)
.group("Output, Prompt and Flow Control")
.set_rc_configurable()
.set_env_var_names()
Expand All @@ -1528,15 +1528,15 @@ namespace mamba
Set the log backtrace size. It will replay the n last
logs if an error is thrown during the execution.)")));

insert(Configurable("log_pattern", &ctx.log_pattern)
insert(Configurable("log_pattern", &ctx.output_params.log_pattern)
.group("Output, Prompt and Flow Control")
.set_rc_configurable()
.set_env_var_names()
.description("Set the log pattern")
.long_description(unindent(R"(
Set the log pattern.)")));

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

insert(Configurable("quiet", &ctx.quiet)
insert(Configurable("quiet", &ctx.output_params.quiet)
.group("Output, Prompt and Flow Control")
.set_rc_configurable()
.set_env_var_names()
Expand Down Expand Up @@ -1650,12 +1650,12 @@ namespace mamba
.set_env_var_names()
.description("Whether to override rc files by highest precedence"));

insert(Configurable("no_rc", &ctx.no_rc)
insert(Configurable("no_rc", &ctx.src_params.no_rc)
.group("Config sources")
.set_env_var_names()
.description("Disable the use of configuration files"));

insert(Configurable("no_env", &ctx.no_env)
insert(Configurable("no_env", &ctx.src_params.no_env)
.group("Config sources")
.set_env_var_names()
.description("Disable the use of environment variables"));
Expand Down Expand Up @@ -1790,15 +1790,15 @@ namespace mamba
}

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

spdlog::apply_all([&](std::shared_ptr<spdlog::logger> l) { l->flush(); });
spdlog::flush_on(spdlog::level::off);

Context::instance().dump_backtrace_no_guards();
if (ctx.log_backtrace > 0)
if (ctx.output_params.log_backtrace > 0)
{
spdlog::enable_backtrace(ctx.log_backtrace);
spdlog::enable_backtrace(ctx.output_params.log_backtrace);
}
else
{
Expand Down
9 changes: 5 additions & 4 deletions libmamba/src/api/info.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,15 @@ namespace mamba
std::string banner()
{
auto& ctx = Context::instance();
return ctx.custom_banner.empty() ? mamba_banner : ctx.custom_banner;
return ctx.command_params.custom_banner.empty() ? mamba_banner
: ctx.command_params.custom_banner;
}

namespace detail
{
void info_pretty_print(std::vector<std::tuple<std::string, nlohmann::json>> items)
{
if (Context::instance().json)
if (Context::instance().output_params.json)
{
return;
}
Expand Down Expand Up @@ -142,9 +143,9 @@ namespace mamba

items.push_back({ "libmamba version", version() });

if (ctx.is_micromamba && !ctx.caller_version.empty())
if (ctx.command_params.is_micromamba && !ctx.command_params.caller_version.empty())
{
items.push_back({ "micromamba version", ctx.caller_version });
items.push_back({ "micromamba version", ctx.command_params.caller_version });
}

items.push_back({ "curl version", curl_version() });
Expand Down
Loading

0 comments on commit 40c220c

Please sign in to comment.