-
Notifications
You must be signed in to change notification settings - Fork 418
feat: add support for cloning environments with --clone option #4069
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
Open
longtsing
wants to merge
2
commits into
mamba-org:main
Choose a base branch
from
longtsing:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -11,7 +11,10 @@ | |||||
| #include "mamba/api/install.hpp" | ||||||
| #include "mamba/core/channel_context.hpp" | ||||||
| #include "mamba/core/context.hpp" | ||||||
| #include "mamba/core/environments_manager.hpp" | ||||||
| #include "mamba/core/prefix_data.hpp" | ||||||
| #include "mamba/core/util.hpp" | ||||||
| #include "mamba/util/path_manip.hpp" | ||||||
|
|
||||||
| namespace mamba | ||||||
| { | ||||||
|
|
@@ -30,13 +33,98 @@ namespace mamba | |||||
| config.load(); | ||||||
|
|
||||||
| auto& create_specs = config.at("specs").value<std::vector<std::string>>(); | ||||||
| auto& clone_env_name = config.at("clone_env").value<std::string>(); | ||||||
| auto& use_explicit = config.at("explicit_install").value<bool>(); | ||||||
| auto& json_format = config.at("json").get_cli_config<bool>(); | ||||||
| auto& env_vars = config.at("spec_file_env_vars").value<std::map<std::string, std::string>>(); | ||||||
| auto& no_env = config.at("no_env").value<bool>(); | ||||||
|
|
||||||
| auto channel_context = ChannelContext::make_conda_compatible(ctx); | ||||||
|
|
||||||
| // Handle clone environment logic | ||||||
| std::vector<std::string> clone_specs; | ||||||
| if (!clone_env_name.empty()) | ||||||
| { | ||||||
| // Validate that no other specs are provided when cloning | ||||||
| if (!create_specs.empty()) | ||||||
| { | ||||||
| const auto message = "Cannot specify packages when cloning an environment. Use --clone only."; | ||||||
| LOG_ERROR << message; | ||||||
| throw mamba_error(message, mamba_error_code::incorrect_usage); | ||||||
| } | ||||||
|
|
||||||
| auto& file_specs = config.at("file_specs").value<std::vector<std::string>>(); | ||||||
| if (!file_specs.empty()) | ||||||
| { | ||||||
| const auto message = "Cannot specify environment file when cloning an environment. Use --clone only."; | ||||||
| LOG_ERROR << message; | ||||||
| throw mamba_error(message, mamba_error_code::incorrect_usage); | ||||||
| } | ||||||
|
|
||||||
| // Determine source environment path | ||||||
| fs::u8path source_prefix; | ||||||
| if (fs::exists(clone_env_name)) | ||||||
| { | ||||||
| // Direct path provided | ||||||
| source_prefix = clone_env_name; | ||||||
| } | ||||||
| else | ||||||
| { | ||||||
| // Environment name provided, look for it | ||||||
| if (clone_env_name == "base") | ||||||
| { | ||||||
| source_prefix = ctx.prefix_params.root_prefix; | ||||||
| } | ||||||
| else | ||||||
| { | ||||||
| // Search in envs directories | ||||||
| bool found = false; | ||||||
| for (const auto& envs_dir : ctx.envs_dirs) | ||||||
| { | ||||||
| auto potential_path = envs_dir / clone_env_name; | ||||||
| if (fs::exists(potential_path) && is_conda_environment(potential_path)) | ||||||
| { | ||||||
| source_prefix = potential_path; | ||||||
| found = true; | ||||||
| break; | ||||||
| } | ||||||
| } | ||||||
| if (!found) | ||||||
| { | ||||||
| const auto message = "Environment '" + clone_env_name + "' not found."; | ||||||
| LOG_ERROR << message; | ||||||
| throw mamba_error(message, mamba_error_code::incorrect_usage); | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| // Validate source environment exists and is a conda environment | ||||||
| if (!fs::exists(source_prefix) || !is_conda_environment(source_prefix)) | ||||||
| { | ||||||
| const auto message = "Source environment '" + source_prefix.string() + "' is not a valid conda environment."; | ||||||
| LOG_ERROR << message; | ||||||
| throw mamba_error(message, mamba_error_code::incorrect_usage); | ||||||
| } | ||||||
|
|
||||||
| // Get package list from source environment | ||||||
| auto maybe_source_prefix_data = PrefixData::create(source_prefix, channel_context); | ||||||
| if (!maybe_source_prefix_data) | ||||||
| { | ||||||
| const auto message = "Could not load prefix data from source environment: " + maybe_source_prefix_data.error().what(); | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| LOG_ERROR << message; | ||||||
| throw mamba_error(message, mamba_error_code::incorrect_usage); | ||||||
| } | ||||||
| auto records = maybe_source_prefix_data.value().sorted_records(); | ||||||
|
|
||||||
| // Convert records to specs for installation | ||||||
| for (const auto& record : records) | ||||||
| { | ||||||
| clone_specs.push_back(record.name + "=" + record.version + "=" + record.build_string); | ||||||
| } | ||||||
|
|
||||||
| LOG_INFO << "Cloning environment '" << source_prefix.string() << "' with " << clone_specs.size() << " packages."; | ||||||
| } | ||||||
|
|
||||||
| bool remove_prefix_on_failure = false; | ||||||
| bool create_env = true; | ||||||
|
|
||||||
|
|
@@ -82,7 +170,7 @@ namespace mamba | |||||
| throw mamba_error(message, mamba_error_code::incorrect_usage); | ||||||
| } | ||||||
| } | ||||||
| if (create_specs.empty()) | ||||||
| if (create_specs.empty() && clone_specs.empty()) | ||||||
| { | ||||||
| detail::create_empty_target(ctx, ctx.prefix_params.target_prefix, env_vars, no_env); | ||||||
| } | ||||||
|
|
@@ -98,7 +186,7 @@ namespace mamba | |||||
| } | ||||||
| else | ||||||
| { | ||||||
| if (create_specs.empty() && json_format) | ||||||
| if (create_specs.empty() && clone_specs.empty() && json_format) | ||||||
| { | ||||||
| // Just print the JSON | ||||||
| nlohmann::json output; | ||||||
|
|
@@ -124,14 +212,17 @@ namespace mamba | |||||
| remove_prefix_on_failure | ||||||
| ); | ||||||
| } | ||||||
| else if (!create_specs.empty()) | ||||||
| else if (!create_specs.empty() || !clone_specs.empty()) | ||||||
| { | ||||||
| // Use clone_specs if available, otherwise use create_specs | ||||||
| const auto& specs_to_install = !clone_specs.empty() ? clone_specs : create_specs; | ||||||
|
|
||||||
| if (use_explicit) | ||||||
| { | ||||||
| install_explicit_specs( | ||||||
| ctx, | ||||||
| channel_context, | ||||||
| create_specs, | ||||||
| specs_to_install, | ||||||
| create_env, | ||||||
| remove_prefix_on_failure | ||||||
| ); | ||||||
|
|
@@ -142,7 +233,7 @@ namespace mamba | |||||
| ctx, | ||||||
| channel_context, | ||||||
| config, | ||||||
| create_specs, | ||||||
| specs_to_install, | ||||||
| create_env, | ||||||
| remove_prefix_on_failure | ||||||
| ); | ||||||
|
|
||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you move this code into a dedicated function so that we can write something like
The idea is to have the
createfunction "short" and easy to read (it would require some further refactoring, but this is orthogonal to your PR).