-
Notifications
You must be signed in to change notification settings - Fork 372
/
Copy pathpip_utils.cpp
187 lines (163 loc) · 5.87 KB
/
pip_utils.cpp
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
// Copyright (c) 2024, 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 <fmt/color.h>
#include <fmt/format.h>
#include <fmt/ostream.h>
#include <fmt/ranges.h>
#include <reproc++/run.hpp>
#include <reproc/reproc.h>
// TODO includes to be removed after moving some functions/structs around
#include "mamba/api/install.hpp" // other_pkg_mgr_spec
#include "mamba/core/activation.hpp" // get_path_dirs
#include "mamba/core/context.hpp"
#include "mamba/core/util.hpp"
#include "mamba/fs/filesystem.hpp"
#include "mamba/util/environment.hpp"
#include "pip_utils.hpp"
namespace mamba
{
namespace
{
tl::expected<command_args, std::runtime_error> get_pip_install_command(
const std::string& name,
const std::string& target_prefix,
const fs::u8path& spec_file,
pip::Update update
)
{
const auto get_python_path = [&]
{ return util::which_in("python", get_path_dirs(target_prefix)).string(); };
command_args cmd = { get_python_path(), "-m", "pip", "install" };
command_args cmd_extension = { "-r", spec_file, "--no-input", "--quiet" };
if (update == pip::Update::Yes)
{
cmd.push_back("-U");
}
if (name == "pip --no-deps")
{
cmd.push_back("--no-deps");
}
cmd.insert(cmd.end(), cmd_extension.begin(), cmd_extension.end());
const std::unordered_map<std::string, command_args> pip_install_command{
{ "pip", cmd },
{ "pip --no-deps", cmd }
};
auto found_it = pip_install_command.find(name);
if (found_it != pip_install_command.end())
{
return found_it->second;
}
else
{
return tl::unexpected(std::runtime_error(fmt::format(
"no {} instruction found for package manager '{}'",
(update == pip::Update::Yes) ? "update" : "install",
name
)));
}
}
}
bool reproc_killed(int status)
{
return status == REPROC_SIGKILL;
}
bool reproc_terminated(int status)
{
return status == REPROC_SIGTERM;
}
void assert_reproc_success(const reproc::options& options, int status, std::error_code ec)
{
bool killed_not_an_err = (options.stop.first.action == reproc::stop::kill)
|| (options.stop.second.action == reproc::stop::kill)
|| (options.stop.third.action == reproc::stop::kill);
bool terminated_not_an_err = (options.stop.first.action == reproc::stop::terminate)
|| (options.stop.second.action == reproc::stop::terminate)
|| (options.stop.third.action == reproc::stop::terminate);
if (ec || (!killed_not_an_err && reproc_killed(status))
|| (!terminated_not_an_err && reproc_terminated(status)))
{
if (ec)
{
LOG_ERROR << "Subprocess call failed: " << ec.message();
}
else if (reproc_killed(status))
{
LOG_ERROR << "Subprocess call failed (killed)";
}
else
{
LOG_ERROR << "Subprocess call failed (terminated)";
}
throw std::runtime_error("Subprocess call failed. Aborting.");
}
}
tl::expected<command_args, std::runtime_error> install_for_other_pkgmgr(
const Context& ctx,
const detail::other_pkg_mgr_spec& other_spec,
pip::Update update
)
{
const auto& pkg_mgr = other_spec.pkg_mgr;
const auto& deps = other_spec.deps;
const auto& cwd = other_spec.cwd;
LOG_WARNING << fmt::format(
"You are using '{}' as an additional package manager.\nBe aware that packages installed with '{}' are managed independently from 'conda-forge' channel.",
pkg_mgr,
pkg_mgr
);
TemporaryFile specs("mambaf", "", cwd);
{
std::ofstream specs_f = open_ofstream(specs.path());
for (auto& d : deps)
{
specs_f << d.c_str() << '\n';
}
}
command_args command = [&]
{
const auto maybe_command = get_pip_install_command(
pkg_mgr,
ctx.prefix_params.target_prefix.string(),
specs.path(),
update
);
if (maybe_command)
{
return maybe_command.value();
}
else
{
throw maybe_command.error();
}
}();
auto [wrapped_command, tmpfile] = prepare_wrapped_call(
ctx,
ctx.prefix_params.target_prefix,
command
);
reproc::options options;
options.redirect.parent = true;
options.working_directory = cwd.c_str();
Console::stream() << fmt::format(
ctx.graphics_params.palette.external,
"\n{} {} packages: {}",
(update == pip::Update::Yes) ? "Updating" : "Installing",
pkg_mgr,
fmt::join(deps, ", ")
);
fmt::print(LOG_INFO, "Calling: {}", fmt::join(command, " "));
auto [status, ec] = reproc::run(wrapped_command, options);
assert_reproc_success(options, status, ec);
if (status != 0)
{
throw std::runtime_error(fmt::format(
"pip failed to {} packages",
(update == pip::Update::Yes) ? "update" : "install"
));
}
return command;
}
}