Skip to content

Commit 2204026

Browse files
committed
Reuse effective_python_site_packages_path
Signed-off-by: Julien Jerphanion <git@jjerphan.xyz>
1 parent a65ebea commit 2204026

File tree

4 files changed

+73
-69
lines changed

4 files changed

+73
-69
lines changed

libmamba/src/core/query.cpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
#include "mamba/specs/version.hpp"
2626
#include "mamba/util/string.hpp"
2727

28+
#include "transaction_context.hpp"
29+
2830
namespace mamba
2931
{
3032
/******************************
@@ -230,7 +232,18 @@ namespace mamba
230232
.value();
231233
database.for_each_package_matching(
232234
ms,
233-
[&](specs::PackageInfo&& pkg) { g.add_node(std::move(pkg)); }
235+
[&](specs::PackageInfo&& pkg)
236+
{
237+
if (pkg.name == "python")
238+
{
239+
if (auto effective = effective_python_site_packages_path(pkg);
240+
!effective.empty())
241+
{
242+
pkg.python_site_packages_path = std::move(effective);
243+
}
244+
}
245+
g.add_node(std::move(pkg));
246+
}
234247
);
235248
}
236249

libmamba/src/core/transaction.cpp

Lines changed: 0 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -116,74 +116,6 @@ namespace mamba
116116
return out;
117117
}
118118

119-
/**
120-
* Prefer repodata's `python_site_packages_path`; if missing, infer free-threaded layout
121-
* (`lib/pythonX.Yt/site-packages`) from CPython's conda build string segment (`cpVVVt`).
122-
*
123-
* On Unix-like platforms, repodata may still advertise `lib/pythonX.Y/site-packages` for
124-
* `cpVVVt` builds; in that case use the free-threaded layout so `noarch` paths match the
125-
* interpreter (`lib/pythonX.Yt/...`).
126-
*
127-
* On Windows, always use the canonical `Lib/site-packages` location.
128-
*/
129-
auto effective_python_site_packages_path(const specs::PackageInfo& python_pkg) -> std::string
130-
{
131-
#ifdef _WIN32
132-
// On Windows, `libmamba` should install all python packages into the
133-
// canonical `Lib/site-packages` location (independently of the python version
134-
// and of freethreading).
135-
const std::string canonical_site_packages = (fs::u8path("Lib") / "site-packages")
136-
.generic_string();
137-
138-
return canonical_site_packages;
139-
#else
140-
const std::string short_ver = compute_short_python_version(python_pkg.version);
141-
std::string compact = short_ver;
142-
util::replace_all(compact, ".", "");
143-
const bool freethreaded_build = !python_pkg.build_string.empty() && !compact.empty()
144-
&& util::contains(
145-
python_pkg.build_string,
146-
util::concat("cp", compact, "t")
147-
);
148-
149-
const std::string ft_site_packages = (short_ver.empty()
150-
|| python_pkg.build_string.empty())
151-
? std::string{}
152-
: (fs::u8path("lib")
153-
/ util::concat("python", short_ver, "t")
154-
/ "site-packages")
155-
.generic_string();
156-
157-
if (!python_pkg.python_site_packages_path.empty())
158-
{
159-
if (freethreaded_build && !ft_site_packages.empty())
160-
{
161-
const std::string std_site_packages = short_ver.empty()
162-
? std::string{}
163-
: (fs::u8path("lib")
164-
/ util::concat("python", short_ver)
165-
/ "site-packages")
166-
.generic_string();
167-
if (python_pkg.python_site_packages_path == std_site_packages)
168-
{
169-
return ft_site_packages;
170-
}
171-
}
172-
return python_pkg.python_site_packages_path;
173-
}
174-
175-
if (short_ver.empty() || python_pkg.build_string.empty())
176-
{
177-
return {};
178-
}
179-
if (!freethreaded_build)
180-
{
181-
return {};
182-
}
183-
return ft_site_packages;
184-
#endif
185-
}
186-
187119
auto find_python_versions_and_site_packages(
188120
const solver::Solution& solution,
189121
const solver::libsolv::Database& database

libmamba/src/core/transaction_context.cpp

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,63 @@ namespace mamba
3535
return util::concat(sv[0], '.', sv[1]);
3636
}
3737

38+
auto effective_python_site_packages_path(const specs::PackageInfo& python_pkg) -> std::string
39+
{
40+
#ifdef _WIN32
41+
// On Windows, `libmamba` should install all python packages into the
42+
// canonical `Lib/site-packages` location (independently of the python version
43+
// and of freethreading).
44+
const std::string canonical_site_packages = (fs::u8path("Lib") / "site-packages")
45+
.generic_string();
46+
47+
return canonical_site_packages;
48+
#else
49+
const std::string short_ver = compute_short_python_version(python_pkg.version);
50+
std::string compact = short_ver;
51+
util::replace_all(compact, ".", "");
52+
const bool freethreaded_build = !python_pkg.build_string.empty() && !compact.empty()
53+
&& util::contains(
54+
python_pkg.build_string,
55+
util::concat("cp", compact, "t")
56+
);
57+
58+
const std::string ft_site_packages = (short_ver.empty() || python_pkg.build_string.empty())
59+
? std::string{}
60+
: (fs::u8path("lib")
61+
/ util::concat("python", short_ver, "t")
62+
/ "site-packages")
63+
.generic_string();
64+
65+
if (!python_pkg.python_site_packages_path.empty())
66+
{
67+
if (freethreaded_build && !ft_site_packages.empty())
68+
{
69+
const std::string std_site_packages = short_ver.empty()
70+
? std::string{}
71+
: (fs::u8path("lib")
72+
/ util::concat("python", short_ver)
73+
/ "site-packages")
74+
.generic_string();
75+
if (python_pkg.python_site_packages_path == std_site_packages)
76+
{
77+
return ft_site_packages;
78+
}
79+
}
80+
return python_pkg.python_site_packages_path;
81+
}
82+
83+
if (short_ver.empty() || python_pkg.build_string.empty())
84+
{
85+
return {};
86+
}
87+
if (!freethreaded_build)
88+
{
89+
return {};
90+
}
91+
return ft_site_packages;
92+
#endif
93+
}
94+
3895
// supply short python version, e.g. 2.7, 3.5...
3996
fs::u8path get_python_short_path(const std::string& python_version [[maybe_unused]])
4097
{

libmamba/src/core/transaction_context.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,12 @@
1515
#include "mamba/core/util.hpp"
1616
#include "mamba/fs/filesystem.hpp"
1717
#include "mamba/specs/match_spec.hpp"
18+
#include "mamba/specs/package_info.hpp"
1819

1920
namespace mamba
2021
{
2122
std::string compute_short_python_version(const std::string& long_version);
23+
std::string effective_python_site_packages_path(const specs::PackageInfo& python_pkg);
2224
// supply short python version, e.g. 2.7, 3.5...
2325
fs::u8path get_python_short_path(const std::string& python_version);
2426
fs::u8path get_python_site_packages_short_path(const std::string& python_version);

0 commit comments

Comments
 (0)