forked from mamba-org/mamba
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtransaction.cpp
More file actions
1444 lines (1292 loc) · 52.8 KB
/
transaction.cpp
File metadata and controls
1444 lines (1292 loc) · 52.8 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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// 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 <algorithm>
#include <iostream>
#include <iterator>
#include <ranges>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#include <fmt/color.h>
#include <fmt/format.h>
#include <fmt/ostream.h>
#include <reproc++/run.hpp>
#include "mamba/core/channel_context.hpp"
#include "mamba/core/context.hpp"
#include "mamba/core/download_progress_bar.hpp"
#include "mamba/core/env_lockfile.hpp"
#include "mamba/core/execution.hpp"
#include "mamba/core/output.hpp"
#include "mamba/core/package_fetcher.hpp"
#include "mamba/core/repo_checker_store.hpp"
#include "mamba/core/thread_utils.hpp"
#include "mamba/core/transaction.hpp"
#include "mamba/core/util.hpp"
#include "mamba/core/util_scope.hpp"
#include "mamba/solver/libsolv/database.hpp"
#include "mamba/specs/match_spec.hpp"
#include "mamba/util/environment.hpp"
#include "mamba/util/path_manip.hpp"
#include "mamba/util/variant_cmp.hpp"
#include "solver/helpers.hpp"
#include "link.hpp"
#include "progress_bar_impl.hpp"
#include "transaction_context.hpp"
namespace mamba
{
namespace nl = nlohmann;
namespace
{
bool need_pkg_download(const specs::PackageInfo& pkg_info, MultiPackageCache& caches)
{
return caches.get_extracted_dir_path(pkg_info).empty()
&& caches.get_tarball_path(pkg_info).empty();
}
// TODO duplicated function, consider moving it to Pool
auto database_has_package(solver::libsolv::Database& database, const specs::MatchSpec& spec)
-> bool
{
bool found = false;
database.for_each_package_matching(
spec,
[&](const auto&)
{
found = true;
return util::LoopControl::Break;
}
);
return found;
};
auto explicit_spec(const specs::PackageInfo& pkg) -> specs::MatchSpec
{
auto out = specs::MatchSpec();
out.set_name(specs::MatchSpec::NameSpec(pkg.name));
if (!pkg.version.empty())
{
out.set_version(
specs::VersionSpec::parse(fmt::format("=={}", pkg.version))
.or_else([](specs::ParseError&& error) { throw std::move(error); })
.value()
);
}
if (!pkg.build_string.empty())
{
out.set_build_string(
specs::MatchSpec::BuildStringSpec(specs::GlobSpec(pkg.build_string))
);
}
return out;
}
auto installed_python(const solver::libsolv::Database& database)
-> std::optional<specs::PackageInfo>
{
// TODO combine Repo and MatchSpec search API in Pool
auto out = std::optional<specs::PackageInfo>();
if (auto repo = database.installed_repo())
{
database.for_each_package_in_repo(
*repo,
[&](specs::PackageInfo&& pkg)
{
if (pkg.name == "python")
{
out = std::move(pkg);
return util::LoopControl::Break;
}
return util::LoopControl::Continue;
}
);
}
return out;
}
auto find_python_versions_and_site_packages(
const solver::Solution& solution,
const solver::libsolv::Database& database
) -> std::pair<std::pair<std::string, std::string>, std::string>
{
// We need to find the python version that will be there after this
// Transaction is finished in order to compile the noarch packages correctly,
// We need to look into installed packages in case we are not installing a new python
// version but keeping the current one.
// Could also be written in term of PrefixData.
std::string python_site_packages_path = {};
std::string installed_py_ver = {};
if (auto pkg = installed_python(database))
{
python_site_packages_path = pkg->python_site_packages_path;
installed_py_ver = pkg->version;
LOG_INFO << "Found python in installed packages " << installed_py_ver;
}
std::string new_py_ver = installed_py_ver;
if (auto py = solver::find_new_python_in_solution(solution))
{
new_py_ver = py->get().version;
python_site_packages_path = py->get().python_site_packages_path;
}
return {
{ std::move(new_py_ver), std::move(installed_py_ver) },
std::move(python_site_packages_path),
};
}
}
MTransaction::MTransaction(const CommandParams& command_params, MultiPackageCache& caches)
: m_multi_cache(caches)
, m_history_entry(History::UserRequest::prefilled(command_params))
{
}
MTransaction::MTransaction(
const Context& ctx,
solver::libsolv::Database& database,
std::vector<specs::PackageInfo> pkgs_to_remove,
std::vector<specs::PackageInfo> pkgs_to_install,
MultiPackageCache& caches
)
: MTransaction(ctx.command_params, caches)
{
auto not_found = std::stringstream();
for (const auto& pkg : pkgs_to_remove)
{
auto spec = explicit_spec(pkg);
if (!database_has_package(database, spec))
{
not_found << "\n - " << spec.to_string();
}
}
if (auto list = not_found.str(); !list.empty())
{
LOG_ERROR << "Could not find packages to remove:" << list << '\n';
Console::instance().json_write({ { "success", false } });
throw std::runtime_error("Could not find packages to remove:" + list);
}
Console::instance().json_write({ { "success", true } });
m_requested_specs.reserve(pkgs_to_install.size());
std::transform(
pkgs_to_install.begin(),
pkgs_to_install.end(),
std::back_insert_iterator(m_requested_specs),
[](const auto& pkg) { return explicit_spec(pkg); }
);
m_history_entry.update.reserve(pkgs_to_install.size());
for (auto& pkg : pkgs_to_install)
{
m_history_entry.update.push_back(explicit_spec(pkg).to_string());
}
m_history_entry.remove.reserve(pkgs_to_remove.size());
for (auto& pkg : pkgs_to_remove)
{
m_history_entry.remove.push_back(explicit_spec(pkg).to_string());
}
m_solution.actions.reserve(pkgs_to_install.size() + pkgs_to_remove.size());
std::transform(
std::move_iterator(pkgs_to_install.begin()),
std::move_iterator(pkgs_to_install.end()),
std::back_insert_iterator(m_solution.actions),
[](specs::PackageInfo&& pkg) { return solver::Solution::Install{ std::move(pkg) }; }
);
std::transform(
std::move_iterator(pkgs_to_remove.begin()),
std::move_iterator(pkgs_to_remove.end()),
std::back_insert_iterator(m_solution.actions),
[](specs::PackageInfo&& pkg) { return solver::Solution::Remove{ std::move(pkg) }; }
);
// if no action required, don't even start logging them
if (!empty())
{
Console::instance().json_down("actions");
Console::instance().json_write({ { "PREFIX", ctx.prefix_params.target_prefix.string() } });
}
std::tie(
m_py_versions,
m_python_site_packages_path
) = find_python_versions_and_site_packages(m_solution, database);
}
MTransaction::MTransaction(
const Context& ctx,
solver::libsolv::Database& database,
const solver::Request& request,
solver::Solution solution,
MultiPackageCache& caches
)
: MTransaction(ctx.command_params, caches)
{
const auto& flags = request.flags;
m_solution = std::move(solution);
if (flags.keep_user_specs)
{
using Request = solver::Request;
solver::for_each_of<Request::Install, Request::Update>(
request,
[&](const auto& item) { m_history_entry.update.push_back(item.spec.to_string()); }
);
solver::for_each_of<Request::Remove, Request::Update>(
request,
[&](const auto& item) { m_history_entry.remove.push_back(item.spec.to_string()); }
);
}
else
{
// The specs to install become all the dependencies of the non intstalled specs
for (const specs::PackageInfo& pkg : m_solution.packages_to_omit())
{
for (const auto& dep : pkg.dependencies)
{
m_history_entry.update.push_back(dep);
}
}
}
using Request = solver::Request;
solver::for_each_of<Request::Install, Request::Update>(
request,
[&](const auto& item) { m_requested_specs.push_back(item.spec); }
);
std::tie(
m_py_versions,
m_python_site_packages_path
) = find_python_versions_and_site_packages(m_solution, database);
// if no action required, don't even start logging them
if (!empty())
{
Console::instance().json_down("actions");
Console::instance().json_write(
{
{ "PREFIX", ctx.prefix_params.target_prefix.string() },
}
);
}
}
MTransaction::MTransaction(
const Context& ctx,
solver::libsolv::Database& database,
std::vector<specs::PackageInfo> packages,
MultiPackageCache& caches
)
: MTransaction(ctx.command_params, caches)
{
LOG_INFO << "MTransaction::MTransaction - packages already resolved (lockfile)";
m_requested_specs.reserve(packages.size());
std::transform(
packages.cbegin(),
packages.cend(),
std::back_insert_iterator(m_requested_specs),
[](const auto& pkg)
{
return specs::MatchSpec::parse(
fmt::format("{}=={}={}", pkg.name, pkg.version, pkg.build_string)
)
.or_else([](specs::ParseError&& err) { throw std::move(err); })
.value();
}
);
m_solution.actions.reserve(packages.size());
std::transform(
std::move_iterator(packages.begin()),
std::move_iterator(packages.end()),
std::back_insert_iterator(m_solution.actions),
[](specs::PackageInfo&& pkg) { return solver::Solution::Install{ std::move(pkg) }; }
);
std::tie(
m_py_versions,
m_python_site_packages_path
) = find_python_versions_and_site_packages(m_solution, database);
}
class TransactionRollback
{
public:
void record(const UnlinkPackage& unlink)
{
m_unlink_stack.push(unlink);
}
void record(const LinkPackage& link)
{
m_link_stack.push(link);
}
void rollback(const Context&)
{
while (!m_link_stack.empty())
{
m_link_stack.top().undo();
m_link_stack.pop();
}
while (!m_unlink_stack.empty())
{
m_unlink_stack.top().undo();
m_unlink_stack.pop();
}
}
private:
std::stack<UnlinkPackage> m_unlink_stack;
std::stack<LinkPackage> m_link_stack;
};
bool
MTransaction::execute(const Context& ctx, ChannelContext& channel_context, PrefixData& prefix)
{
// If an exception exists this function, we must consider the whole operation a failure.
Console::JSonFailureOnException fail_json_on_exception;
// JSON output
// back to the top level if any action was required
if (!empty())
{
Console::instance().json_up();
}
Console::instance().json_write(
{ { "dry_run", ctx.dry_run }, { "prefix", ctx.prefix_params.target_prefix.string() } }
);
if (empty())
{
Console::instance().json_write(
{ { "message", "All requested packages already installed" } }
);
}
if (ctx.dry_run)
{
Console::stream() << "Dry run. Not executing the transaction.";
return true;
}
// before user confirmation
if (is_sig_interrupted())
{
Console::stream() << "Interrupted by user - stopped before transaction.";
return true;
}
auto lf = LockFile(ctx.prefix_params.target_prefix / "conda-meta");
clean_trash_files(ctx.prefix_params.target_prefix, false);
// after user confirmation or ctrl-c
if (is_sig_interrupted())
{
Console::stream() << "Interrupted by user - stopped before transaction.";
return true;
}
Console::stream() << "\nTransaction starting";
fetch_extract_packages(ctx, channel_context);
if (is_sig_interrupted())
{
Console::stream() << "Interrupted by user - transaction stopped.";
return true;
}
if (ctx.download_only)
{
Console::stream()
<< "Download only - packages download and extraction is done. Skipping the linking phase.";
return true;
}
// Channels coming from the repodata (packages to install) don't have the same channel
// format than packages coming from the prefix (packages to remove). We set all the channels
// to be URL like (i.e. explicit). Below is a loop to fix the channel of the linked
// packages (fix applied to the unlinked packages to avoid potential bugs). Ideally, this
// should be normalised when reading the data.
// Store which packages are pip packages before channel normalization
// (pip packages have channel == "pypi" before normalization)
std::set<std::string> pip_package_names;
std::set<std::string> packages_to_install_names;
std::set<std::string> requested_package_names;
if (ctx.prefix_data_interoperability)
{
// Collect pip packages that are marked for removal
for (const specs::PackageInfo& pkg : m_solution.packages_to_remove())
{
if (pkg.channel == "pypi")
{
pip_package_names.insert(pkg.name);
}
}
// Collect names of packages being installed (including dependencies)
for (const specs::PackageInfo& pkg : m_solution.packages_to_install())
{
packages_to_install_names.insert(pkg.name);
}
// Collect names from original request specs
// This is important because the solver might not add a package to the install list
// if it sees a pip version as already installed, but we still need to remove the pip
// version
for (const auto& spec : m_requested_specs)
{
if (spec.name().is_exact())
{
requested_package_names.insert(spec.name().to_string());
}
}
// Also check for pip packages in prefix that conflict with packages being installed
// or requested. These need to be removed even if the solver didn't mark them for
// removal
for (const auto& [name, pip_pkg] : prefix.pip_records())
{
// Check if this pip package conflicts with a conda package being installed or
// requested
const bool conflicts = (packages_to_install_names.contains(name)
|| requested_package_names.contains(name))
&& !pip_package_names.contains(name);
if (conflicts)
{
// This pip package conflicts with a conda package being installed/requested
// but wasn't marked for removal by the solver - add it to removal list
pip_package_names.insert(name);
}
}
}
for (specs::PackageInfo& pkg : m_solution.packages())
{
const auto unresolved_pkg_channel = mamba::specs::UnresolvedChannel::parse(pkg.channel)
.value();
const auto pkg_channel = mamba::specs::Channel::resolve(
unresolved_pkg_channel,
channel_context.params()
)
.value();
assert(not pkg_channel.empty());
const auto channel_url = pkg_channel.front().platform_url(pkg.platform).str();
pkg.channel = channel_url;
if (pkg.package_url.empty())
{
pkg.package_url = pkg.url_for_channel_platform(channel_url);
}
};
TransactionRollback rollback;
TransactionContext transaction_context(
ctx.transaction_params(),
m_py_versions,
m_python_site_packages_path,
m_requested_specs
);
const std::vector<std::pair<std::string, std::string>> pip_environment_variables{
pip_environment_variables_kv.begin(),
pip_environment_variables_kv.end()
};
// Helper function to uninstall a pip package
const auto uninstall_pip_package = [&](const std::string& name)
{
const auto get_python_path = [&]
{
return util::which_in("python", util::get_path_dirs(ctx.prefix_params.target_prefix))
.string();
};
const std::vector<std::string> full_args{ get_python_path(), "-m", "pip",
"uninstall", "-y", name };
const auto env = pip_environment_variables;
reproc::options run_options;
run_options.env.extra = reproc::env{ env };
const auto working_dir = ctx.prefix_params.target_prefix.string();
run_options.working_directory = working_dir.c_str();
std::string out, err;
{
util::ForceColorScope force_color_scope;
auto [status, ec] = reproc::run(
full_args,
run_options,
reproc::sink::string(out),
reproc::sink::string(err)
);
if (ec)
{
LOG_WARNING << "Failed to uninstall pip package " << name << ": " << err;
// Continue anyway - the package might already be removed or not exist
}
else
{
LOG_DEBUG << "Successfully uninstalled pip package " << name;
}
}
};
// Uninstall all pip packages that conflict with packages being installed BEFORE installing
// This ensures pip packages are removed before conda packages are installed, avoiding
// duplicate uninstall attempts and warnings
if (ctx.prefix_data_interoperability)
{
const auto& pip_records = prefix.pip_records();
std::set<std::string> pip_packages_to_uninstall;
// Collect all pip packages that need to be uninstalled:
// 1. Pip packages marked for removal by the solver
// 2. Pip packages that conflict with packages being installed or requested
for (const auto& [name, pip_pkg] : pip_records)
{
// Inlining the conditions for better performance
const bool conflicts = pip_package_names.contains(name)
|| packages_to_install_names.contains(name)
|| requested_package_names.contains(name);
if (conflicts)
{
pip_packages_to_uninstall.insert(name);
}
}
// Uninstall all conflicting pip packages before any conda operations
for (const auto& name : pip_packages_to_uninstall)
{
Console::stream() << "Uninstalling pip package " << name;
uninstall_pip_package(name);
}
}
for (const specs::PackageInfo& pkg : m_solution.packages_to_remove())
{
if (is_sig_interrupted())
{
break;
}
// Check if this is a pip package
// Pip packages are already uninstalled before this loop, so we skip them here
const bool is_pip_package = pip_package_names.contains(pkg.name)
|| prefix.pip_records().contains(pkg.name)
|| pkg.channel.find("pypi") != std::string::npos;
if (is_pip_package)
{
// Pip packages are already uninstalled before this loop
// Just record it in history and continue
}
else
{
// Only unlink non-pip packages
Console::stream() << "Unlinking " << pkg.str();
const fs::u8path cache_path(m_multi_cache.get_extracted_dir_path(pkg));
UnlinkPackage up(pkg, cache_path, &transaction_context);
up.execute();
rollback.record(up);
}
m_history_entry.unlink_dists.push_back(pkg.long_str());
}
for (const specs::PackageInfo& pkg : m_solution.packages_to_install())
{
if (is_sig_interrupted())
{
break;
}
// When interoperability is disabled, skip installing conda packages that conflict with
// pip packages UNLESS the package is explicitly requested (to allow updates to work)
if (!ctx.prefix_data_interoperability && prefix.pip_records().contains(pkg.name))
{
// Skip if not explicitly requested (preserve pip package)
const bool is_explicitly_requested = std::ranges::any_of(
m_requested_specs,
[&pkg](const auto& spec)
{ return spec.name().is_exact() && spec.name().to_string() == pkg.name; }
);
if (!is_explicitly_requested)
{
continue;
}
}
Console::stream() << "Linking " << pkg.str();
const fs::u8path cache_path(m_multi_cache.get_extracted_dir_path(pkg, false));
LinkPackage lp(pkg, cache_path, &transaction_context);
lp.execute();
rollback.record(lp);
m_history_entry.link_dists.push_back(pkg.long_str());
}
if (is_sig_interrupted())
{
Console::stream() << "Transaction interrupted, rollbacking";
rollback.rollback(ctx);
return false;
}
LOG_INFO << "Waiting for pyc compilation to finish";
transaction_context.wait_for_pyc_compilation();
Console::stream() << "\nTransaction finished\n";
prefix.history().add_entry(m_history_entry);
return true;
}
auto MTransaction::to_conda() -> to_conda_type
{
namespace views = std::ranges::views;
auto to_remove_range = m_solution.packages_to_remove() //
| views::transform(
[](const auto& pkg)
{ return to_remove_type::value_type(pkg.channel, pkg.filename); }
);
// TODO(C++23): std::ranges::to
auto to_remove_structured = to_remove_type(to_remove_range.begin(), to_remove_range.end());
auto to_install_range = m_solution.packages_to_install() //
| views::transform(
[](const auto& pkg)
{
return to_install_type::value_type(
pkg.channel,
pkg.filename,
nl::json(pkg).dump(4)
);
}
);
// TODO(C++23): std::ranges::to
auto to_install_structured = to_install_type(to_install_range.begin(), to_install_range.end());
to_specs_type specs;
std::get<0>(specs) = m_history_entry.update;
std::get<1>(specs) = m_history_entry.remove;
return std::make_tuple(specs, to_install_structured, to_remove_structured);
}
void MTransaction::log_json()
{
namespace views = std::ranges::views;
// TODO(C++23): std::ranges::to
auto to_fetch_range = m_solution.packages_to_install()
| views::filter([this](const auto& pkg)
{ return need_pkg_download(pkg, m_multi_cache); });
auto to_fetch = std::vector<nl::json>(to_fetch_range.begin(), to_fetch_range.end());
// TODO(C++23): std::ranges::to
auto to_link_range = m_solution.packages_to_install();
auto to_link = std::vector<nl::json>(to_link_range.begin(), to_link_range.end());
// TODO(C++23): std::ranges::to
auto to_unlink_range = m_solution.packages_to_remove();
auto to_unlink = std::vector<nl::json>(to_unlink_range.begin(), to_unlink_range.end());
auto add_json = [](const auto& jlist, const char* s)
{
if (!jlist.empty())
{
Console::instance().json_down(s);
for (nl::json j : jlist)
{
Console::instance().json_append(j);
}
Console::instance().json_up();
}
};
add_json(to_fetch, "FETCH");
add_json(to_link, "LINK");
add_json(to_unlink, "UNLINK");
}
namespace
{
using FetcherList = std::vector<PackageFetcher>;
// Free functions instead of private method to avoid exposing downloaders
// and package fetchers in the header. Ideally we may want a pimpl or
// a private implementation header when we refactor this class.
FetcherList build_fetchers(
const Context& ctx,
ChannelContext& channel_context,
const solver::Solution& solution,
MultiPackageCache& multi_cache
)
{
FetcherList fetchers;
if (ctx.validation_params.verify_artifacts)
{
LOG_INFO << "Content trust is enabled, package(s) signatures will be verified";
}
for (const auto& pkg : solution.packages_to_install())
{
if (ctx.validation_params.verify_artifacts)
{
LOG_INFO << "Creating RepoChecker...";
auto repo_checker_store = RepoCheckerStore::make(ctx, channel_context, multi_cache);
for (auto& chan : channel_context.make_channel(pkg.channel))
{
auto repo_checker = repo_checker_store.find_checker(chan);
if (repo_checker)
{
LOG_INFO << "RepoChecker successfully created.";
repo_checker->generate_index_checker();
repo_checker->verify_package(
pkg.json_signable(),
std::string_view(pkg.signatures)
);
}
else
{
LOG_ERROR << "Could not create a valid RepoChecker.";
throw std::runtime_error(
fmt::format(
R"(Could not verify "{}". Please make sure the package signatures are available and 'trusted-channels' are configured correctly. Alternatively, try downloading without '--verify-artifacts' flag.)",
pkg.name
)
);
}
}
LOG_INFO << "'" << pkg.name << "' trusted from '" << pkg.channel << "'";
}
// FIXME: only do this for micromamba for now
if (ctx.command_params.is_mamba_exe)
{
using Credentials = typename specs::CondaURL::Credentials;
auto l_pkg = pkg;
if (!pkg.package_url.empty())
{
auto channels = channel_context.make_channel(pkg.package_url);
assert(channels.size() == 1); // A URL can only resolve to one channel
const auto platform_urls = channels.front().platform_urls();
if (!platform_urls.empty())
{
l_pkg.package_url = platform_urls.front().str(Credentials::Show);
}
}
{
auto channels = channel_context.make_channel(pkg.channel);
assert(channels.size() == 1); // A URL can only resolve to one channel
const auto& channel = channels.front();
l_pkg.channel = channel.id();
if (l_pkg.package_url.empty())
{
l_pkg.package_url = l_pkg.url_for_channel(
channel.url().str(Credentials::Show)
);
}
}
fetchers.emplace_back(l_pkg, multi_cache);
}
else
{
fetchers.emplace_back(pkg, multi_cache);
}
}
if (ctx.validation_params.verify_artifacts)
{
auto out = Console::stream();
fmt::print(
out,
"Content trust verifications successful, {} ",
fmt::styled("package(s) are trusted", ctx.graphics_params.palette.safe)
);
LOG_INFO << "All package(s) are trusted";
}
return fetchers;
}
using ExtractTaskList = std::vector<PackageExtractTask>;
ExtractTaskList
build_extract_tasks(const Context& context, FetcherList& fetchers, std::size_t extract_size)
{
auto extract_options = ExtractOptions::from_context(context);
ExtractTaskList extract_tasks;
extract_tasks.reserve(extract_size);
std::transform(
fetchers.begin(),
fetchers.begin() + static_cast<std::ptrdiff_t>(extract_size),
std::back_inserter(extract_tasks),
[extract_options](auto& f) { return f.build_extract_task(extract_options); }
);
return extract_tasks;
}
using ExtractTrackerList = std::vector<std::future<PackageExtractTask::Result>>;
download::MultiRequest build_download_requests(
FetcherList& fetchers,
ExtractTaskList& extract_tasks,
ExtractTrackerList& extract_trackers,
std::size_t download_size
)
{
download::MultiRequest download_requests;
download_requests.reserve(download_size);
for (auto [fit, eit] = std::tuple{ fetchers.begin(), extract_tasks.begin() };
fit != fetchers.begin() + static_cast<std::ptrdiff_t>(download_size);
++fit, ++eit)
{
auto ceit = eit; // Apple Clang cannot capture eit
auto task = std::make_shared<std::packaged_task<PackageExtractTask::Result(std::size_t)>>(
[ceit](std::size_t downloaded_size) { return ceit->run(downloaded_size); }
);
extract_trackers.push_back(task->get_future());
download_requests.push_back(fit->build_download_request(
[extract_task = std::move(task)](std::size_t downloaded_size)
{
MainExecutor::instance().schedule(
[t = std::move(extract_task)](std::size_t ds) { (*t)(ds); },
downloaded_size
);
}
));
}
return download_requests;
}
void schedule_remaining_extractions(
ExtractTaskList& extract_tasks,
ExtractTrackerList& extract_trackers,
std::size_t download_size
)
{
// We schedule extractions for packages that don't need to be downloaded,
// because downloading a package already triggers its extraction.
for (auto it = extract_tasks.begin() + static_cast<std::ptrdiff_t>(download_size);
it != extract_tasks.end();
++it)
{
std::packaged_task<mamba::PackageExtractTask::Result()> task{ [=]
{ return it->run(); } };
extract_trackers.push_back(task.get_future());
MainExecutor::instance().schedule(std::move(task));
}
}
bool trigger_download(
download::MultiRequest requests,
const Context& context,
download::Options options,
PackageDownloadMonitor* monitor
)
{
auto result = download::download(
std::move(requests),
context.mirrors,
context.remote_fetch_params,
context.authentication_info(),
options,
monitor
);
bool all_downloaded = std::all_of(
result.begin(),
result.end(),
[](const auto& r) { return r; }
);
return all_downloaded;
}
bool clear_invalid_caches(const FetcherList& fetchers, ExtractTrackerList& trackers)
{
bool all_valid = true;
for (auto [fit, eit] = std::tuple{ fetchers.begin(), trackers.begin() };
eit != trackers.end();
++fit, ++eit)
{
PackageExtractTask::Result res = eit->get();
if (!res.valid || !res.extracted)
{
fit->clear_cache();
all_valid = false;
}
}
return all_valid;
}
}
bool MTransaction::fetch_extract_packages(const Context& ctx, ChannelContext& channel_context)
{
PackageFetcherSemaphore::set_max(ctx.threads_params.extract_threads);
FetcherList fetchers = build_fetchers(ctx, channel_context, m_solution, m_multi_cache);
auto download_end = std::partition(
fetchers.begin(),
fetchers.end(),
[](const auto& f) { return f.needs_download(); }
);
auto extract_end = std::partition(
download_end,
fetchers.end(),
[](const auto& f) { return f.needs_extract(); }
);
// At this point:
// - [fetchers.begin(), download_end) contains packages that need to be downloaded,
// validated and extracted
// - [download_end, extract_end) contains packages that need to be extracted only
// - [extract_end, fetchers.end()) contains packages already installed and extracted
auto download_size = static_cast<std::size_t>(std::distance(fetchers.begin(), download_end));
auto extract_size = static_cast<std::size_t>(std::distance(fetchers.begin(), extract_end));
ExtractTaskList extract_tasks = build_extract_tasks(ctx, fetchers, extract_size);
ExtractTrackerList extract_trackers;
extract_trackers.reserve(extract_tasks.size());
download::MultiRequest download_requests = build_download_requests(
fetchers,
extract_tasks,
extract_trackers,
download_size
);
std::unique_ptr<PackageDownloadMonitor> monitor = nullptr;
auto download_options = ctx.download_options();
download_options.fail_fast = true;
if (PackageDownloadMonitor::can_monitor(ctx))
{
monitor = std::make_unique<PackageDownloadMonitor>();
monitor->observe(download_requests, extract_tasks, download_options);
}
schedule_remaining_extractions(extract_tasks, extract_trackers, download_size);
bool all_downloaded = trigger_download(