-
Notifications
You must be signed in to change notification settings - Fork 380
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1577 from Klaim/klaim/env-lockfile
environment lockfile parsing & usage in `create` and `install` sub-commands
- Loading branch information
Showing
22 changed files
with
1,190 additions
and
31 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 |
---|---|---|
@@ -0,0 +1,123 @@ | ||
// Copyright (c) 2022, 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. | ||
|
||
|
||
#ifndef MAMBA_CORE_ENVIRONMENT_LOCKFILE_HPP | ||
#define MAMBA_CORE_ENVIRONMENT_LOCKFILE_HPP | ||
|
||
#include <string> | ||
#include <typeindex> | ||
#include <optional> | ||
#include <unordered_map> | ||
|
||
#include <tl/expected.hpp> | ||
|
||
#include "fsutil.hpp" | ||
#include "package_info.hpp" | ||
#include "error_handling.hpp" | ||
|
||
namespace mamba | ||
{ | ||
|
||
enum class file_parsing_error_code | ||
{ | ||
unknown_failure, /// Something failed while parsing but we can't identify what. | ||
unsuported_version, /// The version of the file does not matched supported ver. | ||
parsing_failure, /// The content of the file doesnt match the expected format/language | ||
/// structure or constraints. | ||
invalid_data, /// The structure of the data in the file is fine but some fields have | ||
/// invalid values for our purpose. | ||
}; | ||
|
||
struct EnvLockFileError | ||
{ | ||
file_parsing_error_code parsing_error_code = file_parsing_error_code::unknown_failure; | ||
std::optional<std::type_index> yaml_error_type{}; | ||
|
||
static const EnvLockFileError& get_details(const mamba_error& error) | ||
{ | ||
return std::any_cast<const EnvLockFileError&>(error.data()); | ||
} | ||
|
||
template <typename StringT> | ||
static mamba_error make_error(file_parsing_error_code error_code, | ||
StringT&& msg, | ||
std::optional<std::type_index> yaml_error_type = std::nullopt) | ||
{ | ||
return mamba_error{ std::forward<StringT>(msg), | ||
mamba_error_code::env_lockfile_parsing_failed, | ||
EnvLockFileError{ error_code, yaml_error_type } }; | ||
} | ||
}; | ||
|
||
class EnvironmentLockFile | ||
{ | ||
public: | ||
struct Channel | ||
{ | ||
std::string url; | ||
std::vector<std::string> used_env_vars; | ||
}; | ||
|
||
struct Meta | ||
{ | ||
std::unordered_map<std::string, std::string> content_hash; | ||
std::vector<Channel> channels; | ||
std::vector<std::string> platforms; | ||
std::vector<std::string> sources; | ||
}; | ||
|
||
struct Package | ||
{ | ||
mamba::PackageInfo info; | ||
bool is_optional = false; | ||
std::string category; | ||
std::string manager; | ||
std::string platform; | ||
}; | ||
|
||
EnvironmentLockFile(Meta metadata, std::vector<Package> packages) | ||
: metadata(std::move(metadata)) | ||
, packages(std::move(packages)) | ||
{ | ||
} | ||
|
||
std::vector<PackageInfo> get_packages_for(std::string_view category, | ||
std::string_view platform, | ||
std::string_view manager) const; | ||
|
||
const std::vector<Package>& get_all_packages() const | ||
{ | ||
return packages; | ||
} | ||
const Meta& get_metadata() const | ||
{ | ||
return metadata; | ||
} | ||
|
||
private: | ||
Meta metadata; | ||
std::vector<Package> packages; | ||
}; | ||
|
||
/// Read an environment lock YAML file and returns it's structured content or an error if | ||
/// failed. | ||
tl::expected<EnvironmentLockFile, mamba_error> read_environment_lockfile( | ||
const fs::path& lockfile_location); | ||
|
||
|
||
/// Returns `true` if the filename matches names of files which should be interpreted as conda | ||
/// environment lockfile. NOTE: this does not check if the file exists. | ||
inline bool is_env_lockfile_name(const std::string_view filename) | ||
{ | ||
return ends_with(filename, "-lock.yml") || ends_with(filename, "-lock.yaml"); | ||
} | ||
|
||
|
||
} | ||
|
||
|
||
#endif |
This file contains 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 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 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 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 |
---|---|---|
@@ -0,0 +1,46 @@ | ||
|
||
#ifndef MAMBA_CORE_UTIL_SCOPE_HPP | ||
#define MAMBA_CORE_UTIL_SCOPE_HPP | ||
|
||
#include <stdexcept> | ||
#include "spdlog/fmt/fmt.h" | ||
#include "mamba/core/output.hpp" | ||
|
||
namespace mamba | ||
{ | ||
|
||
template <typename F> | ||
struct on_scope_exit | ||
{ | ||
F func; | ||
|
||
explicit on_scope_exit(F&& f) | ||
: func(std::forward<F>(f)) | ||
{ | ||
} | ||
|
||
~on_scope_exit() | ||
{ | ||
try | ||
{ | ||
func(); | ||
} | ||
catch (const std::exception& ex) | ||
{ | ||
LOG_ERROR << fmt::format("Scope exit error (catched and ignored): {}", ex.what()); | ||
} | ||
catch (...) | ||
{ | ||
LOG_ERROR << "Scope exit unknown error (catched and ignored)"; | ||
} | ||
} | ||
|
||
// Deactivate copy & move until we implement moves | ||
on_scope_exit(const on_scope_exit&) = delete; | ||
on_scope_exit& operator=(const on_scope_exit&) = delete; | ||
on_scope_exit(on_scope_exit&&) = delete; | ||
on_scope_exit& operator=(on_scope_exit&&) = delete; | ||
}; | ||
} | ||
|
||
#endif |
This file contains 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.