-
Notifications
You must be signed in to change notification settings - Fork 439
Expand file tree
/
Copy pathrepoquery.cpp
More file actions
230 lines (218 loc) · 8.03 KB
/
repoquery.cpp
File metadata and controls
230 lines (218 loc) · 8.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
// Copyright (c) 2019, QuantStack and Mamba Contributors
//
// Distributed under the terms of the BSD 3-Clause License.
//
// The full license is in the file LICENSE, distributed with this software.
#include <iostream>
#include "mamba/api/channel_loader.hpp"
#include "mamba/api/configuration.hpp"
#include "mamba/api/repoquery.hpp"
#include "mamba/core/channel_context.hpp"
#include "mamba/core/package_cache.hpp"
#include "mamba/core/package_database_loader.hpp"
#include "mamba/core/prefix_data.hpp"
#include "mamba/solver/libsolv/database.hpp"
#include "mamba/solver/libsolv/repo_info.hpp"
#include "mamba/util/string.hpp"
#include "utils.hpp"
namespace mamba
{
namespace
{
auto repoquery_root_packages(QueryType type, const std::vector<std::string>& queries)
-> std::vector<std::string>
{
if (type == QueryType::Depends)
{
return extract_package_names_from_specs(queries);
}
if (type == QueryType::Search)
{
// Search queries that are not exact names (e.g. globs) require full repodata.
return extract_exact_package_names_from_specs(queries);
}
return {};
}
auto repoquery_init(
Context& ctx,
Configuration& config,
QueryType type,
QueryResultFormat format,
bool use_local,
const std::vector<std::string>& queries
)
{
config.at("use_target_prefix_fallback").set_value(true);
config.at("use_default_prefix_fallback").set_value(true);
config.at("use_root_prefix_fallback").set_value(true);
config.at("target_prefix_checks")
.set_value(
MAMBA_ALLOW_EXISTING_PREFIX | MAMBA_ALLOW_MISSING_PREFIX | MAMBA_ALLOW_NOT_ENV_PREFIX
);
config.load();
auto channel_context = ChannelContext::make_conda_compatible(ctx);
solver::libsolv::Database db{
channel_context.params(),
{
ctx.experimental_matchspec_parsing ? solver::libsolv::MatchSpecParser::Mamba
: solver::libsolv::MatchSpecParser::Libsolv,
},
};
add_logger_to_database(db);
// bool installed = (type == QueryType::kDepends) || (type == QueryType::kWhoneeds);
MultiPackageCache package_caches(ctx.pkgs_dirs, ctx.validation_params);
if (use_local)
{
if (format != QueryResultFormat::Json)
{
Console::stream() << "Using local repodata..." << std::endl;
}
auto exp_prefix_data = PrefixData::create(
ctx.prefix_params.target_prefix,
channel_context
);
if (!exp_prefix_data)
{
// TODO: propagate tl::expected mechanism
throw std::runtime_error(exp_prefix_data.error().what());
}
PrefixData& prefix_data = exp_prefix_data.value();
load_installed_packages_in_database(ctx, db, prefix_data);
if (format != QueryResultFormat::Json)
{
Console::stream()
<< "Loaded current active prefix: " << ctx.prefix_params.target_prefix
<< std::endl;
}
}
else
{
if (format != QueryResultFormat::Json)
{
Console::stream() << "Getting repodata from channels..." << std::endl;
}
const auto root_packages = ctx.repodata_use_shards
? repoquery_root_packages(type, queries)
: std::vector<std::string>{};
auto exp_load = load_channels(ctx, channel_context, db, package_caches, root_packages);
if (!exp_load)
{
throw std::runtime_error(exp_load.error().what());
}
}
return db;
}
}
auto make_repoquery(
solver::libsolv::Database& database,
QueryType type,
QueryResultFormat format,
const std::vector<std::string>& queries,
bool show_all_builds,
const Context::GraphicsParams& graphics_params,
std::ostream& out
) -> bool
{
if (type == QueryType::Search)
{
auto res = Query::find(database, queries);
switch (format)
{
case QueryResultFormat::Json:
out << res.groupby("name").json().dump(4);
break;
case QueryResultFormat::Pretty:
res.pretty(out, show_all_builds);
break;
default:
res.groupby("name").table(out);
}
return !res.empty();
}
else if (type == QueryType::Depends)
{
if (queries.size() != 1)
{
throw std::invalid_argument("Only one query supported for 'depends'.");
}
auto res = Query::depends(
database,
queries.front(),
/* tree= */ format == QueryResultFormat::Tree
|| format == QueryResultFormat::RecursiveTable
);
switch (format)
{
case QueryResultFormat::Tree:
case QueryResultFormat::Pretty:
res.tree(out, graphics_params);
break;
case QueryResultFormat::Json:
out << res.json().dump(4);
break;
case QueryResultFormat::Table:
case QueryResultFormat::RecursiveTable:
res.sort("name").table(out);
}
return !res.empty();
}
else if (type == QueryType::WhoNeeds)
{
if (queries.size() != 1)
{
throw std::invalid_argument("Only one query supported for 'whoneeds'.");
}
auto res = Query::whoneeds(
database,
queries.front(),
/* tree= */ format == QueryResultFormat::Tree
|| format == QueryResultFormat::RecursiveTable
);
switch (format)
{
case QueryResultFormat::Tree:
case QueryResultFormat::Pretty:
res.tree(out, graphics_params);
break;
case QueryResultFormat::Json:
out << res.json().dump(4);
break;
case QueryResultFormat::Table:
case QueryResultFormat::RecursiveTable:
res.sort("name").table(
out,
{ "Name",
"Version",
"Build",
printers::alignmentMarker(printers::alignment::left),
printers::alignmentMarker(printers::alignment::right),
util::concat("Depends:", queries.front()),
"Channel",
"Subdir" }
);
}
return !res.empty();
}
throw std::invalid_argument("Invalid QueryType");
}
bool repoquery(
Configuration& config,
QueryType type,
QueryResultFormat format,
bool use_local,
const std::vector<std::string>& queries
)
{
auto& ctx = config.context();
auto db = repoquery_init(ctx, config, type, format, use_local, queries);
return make_repoquery(
db,
type,
format,
queries,
ctx.output_params.verbosity > 0,
ctx.graphics_params,
std::cout
);
}
}