Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate expand/shrink_home #2990

Merged
merged 6 commits into from
Nov 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions libmamba/include/mamba/core/environment.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,7 @@

namespace mamba::env
{

auto which(const std::string& exe, const std::string& override_path = "") -> fs::u8path;
auto which(const std::string& exe, const std::vector<fs::u8path>& search_paths) -> fs::u8path;

auto expand_user(const fs::u8path& path) -> fs::u8path;
auto shrink_user(const fs::u8path& path) -> fs::u8path;


}
#endif
2 changes: 1 addition & 1 deletion libmamba/include/mamba/fs/filesystem.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
// code like this:
//
// fs::u8path prefix = ...;
// prefix = rstrip(fs::weakly_canonical(env::expand_user(prefix)).string(), sep);
// prefix = rstrip(fs::weakly_canonical(util::expand_user(prefix)).string(), sep);
//
// Here if `fs::weakly_canonical` is just an alias for `std::filesystem::weakly_canonical` it
// then returns a `std::filesystem::path` and we then call `.string()` on it. That conversion
Expand Down
49 changes: 49 additions & 0 deletions libmamba/include/mamba/util/path_manip.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,26 @@ namespace mamba::util
*/
[[nodiscard]] auto path_has_drive_letter(std::string_view path) -> bool;

/**
* Detect the separator used in a path.
*/
[[nodiscard]] auto path_win_detect_sep(std::string_view path) -> std::optional<char>;

/**
* Convert the Windows path separators to Posix ones.
*/
[[nodiscard]] auto path_win_to_posix(std::string path) -> std::string;

/**
* Convert the Posix path separators to Windows ones.
*/
[[nodiscard]] auto path_posix_to_win(std::string path) -> std::string;

/**
* Convert the path separators to the desired one.
*/
[[nodiscard]] auto path_to_sep(std::string path, char sep) -> std::string;

/**
* Convert the Windows path separators to Posix ones on Windows only.
*/
Expand All @@ -62,5 +77,39 @@ namespace mamba::util
* Concatenate paths with '/' on Unix and detected separator on Windows.
*/
[[nodiscard]] auto path_concat(std::string_view parent, std::string_view child) -> std::string;

/**
* Expand a leading '~' with the given home directory, assuming the given separator.
*/
[[nodiscard]] auto expand_home(std::string_view path, std::string_view home, char sep)
-> std::string;

/**
* Expand a leading '~' with the given home directory.
*/
[[nodiscard]] auto expand_home(std::string_view path, std::string_view home) -> std::string;

/**
* Expand a leading '~' with the user home directory.
*/
[[nodiscard]] auto expand_home(std::string_view path) -> std::string;

/**
* If the path starts with the given home directory, replace it with a leading '~'.
*
* This assumes the given separator is used separate paths.
*/
[[nodiscard]] auto shrink_home(std::string_view path, std::string_view home, char sep)
-> std::string;

/**
* If the path starts with the given home directory, replace it with a leading '~'.
*/
[[nodiscard]] auto shrink_home(std::string_view path, std::string_view home) -> std::string;

/**
* If the path starts with the user home directory, replace it with a leading '~'.
*/
[[nodiscard]] auto shrink_home(std::string_view path) -> std::string;
}
#endif
6 changes: 3 additions & 3 deletions libmamba/src/api/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include <iostream>

#include "mamba/api/configuration.hpp"
#include "mamba/core/environment.hpp"
#include "mamba/util/path_manip.hpp"


namespace mamba
Expand Down Expand Up @@ -94,11 +94,11 @@ namespace mamba
auto found_s = std::find(valid_srcs.begin(), valid_srcs.end(), s);
if (found_s != valid_srcs.end())
{
std::cout << env::shrink_user(s).string() << std::endl;
std::cout << util::shrink_home(s.string()) << std::endl;
}
else
{
std::cout << env::shrink_user(s).string() + " (invalid)" << std::endl;
std::cout << util::shrink_home(s.string()) + " (invalid)" << std::endl;
}
}
}
Expand Down
17 changes: 10 additions & 7 deletions libmamba/src/api/configuration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@

#include "mamba/api/configuration.hpp"
#include "mamba/api/install.hpp"
#include "mamba/core/environment.hpp"
#include "mamba/core/fsutil.hpp"
#include "mamba/core/output.hpp"
#include "mamba/core/package_fetcher.hpp"
#include "mamba/core/util.hpp"
#include "mamba/util/build.hpp"
#include "mamba/util/environment.hpp"
#include "mamba/util/path_manip.hpp"
#include "mamba/util/string.hpp"

namespace mamba
Expand Down Expand Up @@ -602,7 +602,10 @@ namespace mamba
#endif
if (!prefix.empty())
{
prefix = util::rstrip(fs::weakly_canonical(env::expand_user(prefix)).string(), sep);
prefix = util::rstrip(
fs::weakly_canonical(util::expand_home(prefix.string())).string(),
sep
);
}

if ((prefix == root_prefix) && config.at("create_base").value<bool>())
Expand Down Expand Up @@ -671,7 +674,7 @@ namespace mamba
}
}

prefix = fs::weakly_canonical(env::expand_user(prefix));
prefix = fs::weakly_canonical(util::expand_home(prefix.string()));
}

void rc_loading_hook(Configuration& config, const RCConfigLevel& level)
Expand Down Expand Up @@ -792,7 +795,7 @@ namespace mamba
}
for (auto& f : files)
{
f = env::expand_user(f);
f = util::expand_home(f.string());
if (!fs::exists(f))
{
LOG_ERROR << "Configuration file specified but does not exist at '"
Expand Down Expand Up @@ -856,7 +859,7 @@ namespace mamba
{
for (auto& d : dirs)
{
d = fs::weakly_canonical(env::expand_user(d)).string();
d = fs::weakly_canonical(util::expand_home(d.string())).string();
if (fs::exists(d) && !fs::is_directory(d))
{
LOG_ERROR << "Env dir specified is not a directory: " << d.string();
Expand Down Expand Up @@ -922,7 +925,7 @@ namespace mamba
{
for (auto& d : dirs)
{
d = fs::weakly_canonical(env::expand_user(d)).string();
d = fs::weakly_canonical(util::expand_home(d.string())).string();
if (fs::exists(d) && !fs::is_directory(d))
{
LOG_ERROR << "Packages dir specified is not a directory: " << d.string();
Expand Down Expand Up @@ -2239,7 +2242,7 @@ namespace mamba
continue;
}

c.set_rc_yaml_value(yaml[key], env::shrink_user(source).string());
c.set_rc_yaml_value(yaml[key], util::shrink_home(source.string()));
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion libmamba/src/api/install.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include "mamba/core/transaction.hpp"
#include "mamba/core/virtual_packages.hpp"
#include "mamba/fs/filesystem.hpp"
#include "mamba/util/path_manip.hpp"
#include "mamba/util/string.hpp"

namespace mamba
Expand Down Expand Up @@ -216,7 +217,7 @@ namespace mamba

yaml_file_contents read_yaml_file(fs::u8path yaml_file, const std::string platform)
{
auto file = fs::weakly_canonical(env::expand_user(yaml_file));
auto file = fs::weakly_canonical(util::expand_home(yaml_file.string()));
if (!fs::exists(file))
{
LOG_ERROR << "YAML spec file '" << file.string() << "' not found";
Expand Down
6 changes: 3 additions & 3 deletions libmamba/src/api/shell.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
#include "mamba/api/shell.hpp"
#include "mamba/core/activation.hpp"
#include "mamba/core/context.hpp"
#include "mamba/core/environment.hpp"
#include "mamba/core/shell_init.hpp"
#include "mamba/fs/filesystem.hpp"
#include "mamba/util/path_manip.hpp"

#ifdef _WIN32
#include "mamba/core/util_os.hpp"
Expand Down Expand Up @@ -66,7 +66,7 @@ namespace mamba
}
else
{
init_shell(ctx, shell_type, fs::weakly_canonical(env::expand_user(prefix)));
init_shell(ctx, shell_type, fs::weakly_canonical(util::expand_home(prefix.string())));
}
}

Expand All @@ -78,7 +78,7 @@ namespace mamba
}
else
{
deinit_shell(ctx, shell_type, fs::weakly_canonical(env::expand_user(prefix)));
deinit_shell(ctx, shell_type, fs::weakly_canonical(util::expand_home(prefix.string())));
}
}

Expand Down
4 changes: 2 additions & 2 deletions libmamba/src/core/context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@
#include <spdlog/spdlog.h>

#include "mamba/core/context.hpp"
#include "mamba/core/environment.hpp"
#include "mamba/core/execution.hpp"
#include "mamba/core/output.hpp"
#include "mamba/core/thread_utils.hpp"
#include "mamba/core/util.hpp"
#include "mamba/core/util_os.hpp"
#include "mamba/util/environment.hpp"
#include "mamba/util/path_manip.hpp"
#include "mamba/util/string.hpp"
#include "mamba/util/url_manip.hpp"

Expand Down Expand Up @@ -200,7 +200,7 @@ namespace mamba

for (const auto& loc : token_locations)
{
auto px = env::expand_user(loc);
auto px = util::expand_home(loc.string());
if (!fs::exists(px) || !fs::is_directory(px))
{
continue;
Expand Down
21 changes: 0 additions & 21 deletions libmamba/src/core/environment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,25 +93,4 @@ namespace mamba::env

return ""; // empty path
}

fs::u8path expand_user(const fs::u8path& path)
{
auto p = path.string();
if (p[0] == '~')
{
p.replace(0, 1, util::user_home_dir());
}
return p;
}

fs::u8path shrink_user(const fs::u8path& path)
{
auto p = path.string();
auto home = util::user_home_dir();
if (util::starts_with(p, home))
{
p.replace(0, home.size(), "~");
}
return p;
}
}
8 changes: 4 additions & 4 deletions libmamba/src/core/fsutil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,21 @@
#include <string>
#include <system_error>

#include "mamba/core/environment.hpp"
#include "mamba/core/fsutil.hpp"
#include "mamba/core/output.hpp"
#include "mamba/core/util.hpp"
#include "mamba/core/util_scope.hpp"
#include "mamba/fs/filesystem.hpp"
#include "mamba/util/path_manip.hpp"
#include "mamba/util/string.hpp"

namespace mamba::path
{
bool starts_with_home(const fs::u8path& p)
{
std::string path = p.string();
return path[0] == '~'
|| util::starts_with(env::expand_user(path).string(), env::expand_user("~").string());
return util::starts_with(path, '~')
|| util::starts_with(util::expand_home(path), util::expand_home("~"));
}

// TODO more error handling
Expand Down Expand Up @@ -55,7 +55,7 @@ namespace mamba::path
bool touch_impl(fs::u8path path, bool mkdir, bool sudo_safe)
{
// TODO error handling!
path = env::expand_user(path);
path = util::expand_home(path.string());
if (lexists(path))
{
fs::last_write_time(path, fs::now());
Expand Down
4 changes: 3 additions & 1 deletion libmamba/src/core/match_spec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "mamba/core/util.hpp"
#include "mamba/specs/archive.hpp"
#include "mamba/specs/platform.hpp"
#include "mamba/util/path_manip.hpp"
#include "mamba/util/string.hpp"
#include "mamba/util/url_manip.hpp"

Expand Down Expand Up @@ -89,7 +90,8 @@ namespace mamba
if (!util::url_has_scheme(spec_str))
{
LOG_INFO << "need to expand path!";
spec_str = util::path_or_url_to_url(fs::absolute(env::expand_user(spec_str)).string());
spec_str = util::path_or_url_to_url(fs::absolute(util::expand_home(spec_str)).string()
);
}

const auto& parsed_channel = channel_context.make_channel(spec_str);
Expand Down
Loading