Skip to content

Commit

Permalink
Filter at readtime
Browse files Browse the repository at this point in the history
Signed-off-by: Julien Jerphanion <[email protected]>

Co-authored-by: Klaim <[email protected]>
  • Loading branch information
jjerphan and Klaim committed Dec 6, 2024
1 parent a41c9a8 commit 519de68
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 21 deletions.
33 changes: 12 additions & 21 deletions libmamba/src/api/install.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -883,11 +883,13 @@ namespace mamba
}
else
{
const std::vector<std::string> file_contents = read_lines(file);
std::vector<std::string> file_contents = read_lines(file);
if (file_contents.size() == 0)
{
throw std::runtime_error(util::concat("Got an empty file: ", file));
}

// Inferring potential explicit environment specification
for (std::size_t i = 0; i < file_contents.size(); ++i)
{
auto& line = file_contents[i];
Expand Down Expand Up @@ -930,34 +932,23 @@ namespace mamba
}
}

std::vector<std::string> f_specs;
for (auto& line : file_contents)
{
auto lstrip_line = util::lstrip(line);

// Skip comment lines and empty lines
// Comment lines start with '#' or '@' preceded by whitespaces or tabs
auto is_comment = util::starts_with(lstrip_line, "#")
|| util::starts_with(lstrip_line, "@");
auto is_empty = lstrip_line.empty();

if (!is_comment && !is_empty)
{
f_specs.push_back(line);
}
}

// If we reach here, we have a file with no explicit env, and the content of the
// file just lists MatchSpecs.
if (specs.cli_configured())
{
auto current_specs = specs.cli_value<std::vector<std::string>>();
current_specs.insert(current_specs.end(), f_specs.cbegin(), f_specs.cend());
current_specs.insert(
current_specs.end(),
file_contents.cbegin(),
file_contents.cend()
);
specs.set_cli_value(current_specs);
}
else
{
if (!f_specs.empty())
if (!file_contents.empty())
{
specs.set_cli_value(f_specs);
specs.set_cli_value(file_contents);
}
}
}
Expand Down
27 changes: 27 additions & 0 deletions libmamba/src/core/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,33 @@ namespace mamba
line.pop_back();
}

const auto lstrip_line = util::lstrip(line);

// Skipping empty lines
if (lstrip_line.empty())
{
continue;
}

// Skipping comment lines starting with #
if (util::starts_with(lstrip_line, "#"))
{
continue;
}

// Skipping comment lines starting with @ BUT headers of explicit environment specs
if (util::starts_with(lstrip_line, "@"))
{
auto is_explicit_header = util::starts_with(lstrip_line, "@EXPLICIT");

if (is_explicit_header)
{
output.push_back(line);
}
continue;
}

// By default, add the line to the output (MatchSpecs, etc.)
output.push_back(line);
}
file_stream.close();
Expand Down

0 comments on commit 519de68

Please sign in to comment.