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

Handle package cache in secondary locations. #856

Merged
merged 3 commits into from
Apr 19, 2021
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
7 changes: 6 additions & 1 deletion include/mamba/core/package_cache.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ namespace mamba
void set_writable(Writable writable);
Writable is_writable();
fs::path get_pkgs_dir() const;
void clear_query_cache(const PackageInfo& s);

bool query(const PackageInfo& s);

Expand All @@ -59,11 +60,15 @@ namespace mamba
MultiPackageCache(const std::vector<fs::path>& pkgs_dirs);
PackageCacheData& first_writable();

bool query(const PackageInfo& s);
fs::path query(const PackageInfo& s);
fs::path first_cache_path(const PackageInfo& s, bool return_empty = true);
std::vector<PackageCacheData*> writable_caches();

void clear_query_cache(const PackageInfo& s);

private:
std::vector<PackageCacheData> m_caches;
std::map<std::string, std::string> m_path_cache;
};
} // namespace mamba

Expand Down
9 changes: 5 additions & 4 deletions include/mamba/core/transaction.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ namespace mamba
ignore
};

MTransaction(MSolver& solver, MultiPackageCache& cache);
MTransaction(MSolver& solver, MultiPackageCache& cache, const std::string& cache_dir);
~MTransaction();

MTransaction(const MTransaction&) = delete;
Expand All @@ -116,11 +116,11 @@ namespace mamba
void init();
to_conda_type to_conda();
void log_json();
bool fetch_extract_packages(const std::string& cache_dir, std::vector<MRepo*>& repos);
bool fetch_extract_packages(std::vector<MRepo*>& repos);
bool empty();
bool prompt(const std::string& cache_dir, std::vector<MRepo*>& repos);
bool prompt(std::vector<MRepo*>& repos);
void print();
bool execute(PrefixData& prefix, const fs::path& cache_dir);
bool execute(PrefixData& prefix);
bool filter(Solvable* s);

std::string find_python_version();
Expand All @@ -131,6 +131,7 @@ namespace mamba

TransactionContext m_transaction_context;
MultiPackageCache m_multi_cache;
const fs::path m_cache_path;
std::vector<Solvable*> m_to_install, m_to_remove;
History::UserRequest m_history_entry;
Transaction* m_transaction;
Expand Down
16 changes: 8 additions & 8 deletions mamba/mamba.py
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -247,10 +247,10 @@ def remove(args, parser):
return exit_code

package_cache = api.MultiPackageCache(context.pkgs_dirs)
transaction = api.Transaction(solver, package_cache)
downloaded = transaction.prompt(
PackageCacheData.first_writable().pkgs_dir, repos
transaction = api.Transaction(
solver, package_cache, PackageCacheData.first_writable().pkgs_dir
)
downloaded = transaction.prompt(repos)
if not downloaded:
exit(0)

Expand Down Expand Up @@ -576,17 +576,17 @@ def install(args, parser, command="install"):
return exit_code

package_cache = api.MultiPackageCache(context.pkgs_dirs)
transaction = api.Transaction(solver, package_cache)
transaction = api.Transaction(
solver, package_cache, PackageCacheData.first_writable().pkgs_dir
)
mmb_specs, to_link, to_unlink = transaction.to_conda()

specs_to_add = [MatchSpec(m) for m in mmb_specs[0]]
specs_to_remove = [MatchSpec(m) for m in mmb_specs[1]]

transaction.log_json()

downloaded = transaction.prompt(
PackageCacheData.first_writable().pkgs_dir, repos
)
downloaded = transaction.prompt(repos)
if not downloaded:
exit(0)
PackageCacheData.first_writable().reload()
Expand All @@ -596,7 +596,7 @@ def install(args, parser, command="install"):
if newenv and not isdir(context.target_prefix) and not context.dry_run:
mkdir_p(prefix)

transaction.execute(prefix_data, PackageCacheData.first_writable().pkgs_dir)
transaction.execute(prefix_data)
else:
conda_transaction = to_txn(
specs_to_add,
Expand Down
5 changes: 4 additions & 1 deletion mamba/mamba_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import sys

from conda.base.context import context
from conda.core.package_cache_data import PackageCacheData
from conda.core.prefix_data import PrefixData
from conda.core.solve import get_pinned_specs
from conda.models.channel import prioritize_channels
Expand Down Expand Up @@ -112,7 +113,9 @@ def mamba_install(prefix, specs, args, env, *_, **kwargs):
exit(1)

package_cache = api.MultiPackageCache(context.pkgs_dirs)
transaction = api.Transaction(solver, package_cache)
transaction = api.Transaction(
solver, package_cache, PackageCacheData.first_writable().pkgs_dir
)
if not (context.quiet or context.json):
transaction.print()
mmb_specs, to_link, to_unlink = transaction.to_conda()
Expand Down
6 changes: 3 additions & 3 deletions src/api/install.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ namespace mamba
}

MultiPackageCache package_caches({ pkgs_dirs });
MTransaction trans(solver, package_caches);
MTransaction trans(solver, package_caches, pkgs_dirs);

if (ctx.json)
{
Expand All @@ -253,14 +253,14 @@ namespace mamba

std::cout << std::endl;

bool yes = trans.prompt(pkgs_dirs, repo_ptrs);
bool yes = trans.prompt(repo_ptrs);
if (yes)
{
if (create_env && !Context::instance().dry_run)
{
detail::create_target_directory(ctx.target_prefix);
}
trans.execute(prefix_data, pkgs_dirs);
trans.execute(prefix_data);
}
}

Expand Down
9 changes: 5 additions & 4 deletions src/api/remove.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,9 @@ namespace mamba
solver.add_jobs(specs, SOLVER_ERASE);
solver.solve();

MultiPackageCache package_caches({ ctx.root_prefix / "pkgs" });
MTransaction trans(solver, package_caches);
const fs::path pkgs_dirs(ctx.root_prefix / "pkgs");
MultiPackageCache package_caches({ pkgs_dirs });
MTransaction trans(solver, package_caches, pkgs_dirs);

if (ctx.json)
{
Expand All @@ -90,9 +91,9 @@ namespace mamba
repo_ptrs.push_back(&r);
}

bool yes = trans.prompt(ctx.root_prefix / "pkgs", repo_ptrs);
bool yes = trans.prompt(repo_ptrs);
if (yes)
trans.execute(prefix_data, ctx.root_prefix / "pkgs");
trans.execute(prefix_data);
}
}
} // mamba
42 changes: 39 additions & 3 deletions src/core/package_cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ namespace mamba
return m_pkgs_dir;
}

void PackageCacheData::clear_query_cache(const PackageInfo& s)
{
m_valid_cache.erase(s.str());
}

PackageCacheData PackageCacheData::first_writable(const std::vector<fs::path>* pkgs_dirs)
{
const std::vector<fs::path>* dirs = pkgs_dirs ? pkgs_dirs : &Context::instance().pkgs_dirs;
Expand Down Expand Up @@ -288,13 +293,44 @@ namespace mamba
return res;
}

bool MultiPackageCache::query(const PackageInfo& s)
fs::path MultiPackageCache::query(const PackageInfo& s)
{
for (auto& c : m_caches)
{
if (c.query(s))
return true;
return c.get_pkgs_dir();
}
return {};
}

fs::path MultiPackageCache::first_cache_path(const PackageInfo& s, bool return_empty)
{
const std::string pkg(s.str());
const auto cache_iter(m_path_cache.find(pkg));
if (cache_iter != m_path_cache.end())
{
return cache_iter->second;
}
for (auto& c : m_caches)
{
const fs::path cache_path(c.get_pkgs_dir());
if (c.query(s) && fs::exists(cache_path / strip_package_extension(s.fn)))
{
m_path_cache[pkg] = cache_path;
return cache_path;
}
}
if (return_empty)
return {};
else
throw std::runtime_error("Cannot find cache for " + s.fn);
}

void MultiPackageCache::clear_query_cache(const PackageInfo& s)
{
for (auto& c : m_caches)
{
c.clear_query_cache(s);
}
return false;
}
} // namespace mamba
6 changes: 5 additions & 1 deletion src/core/package_handling.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,11 @@ namespace mamba
for (auto& p : paths_data)
{
fs::path full_path = pkg_folder / p.path;
if (!fs::exists(full_path))
// "exists" follows symlink so if the symlink doesn't link to existing target it
// will return false. There is such symlink in _openmp_mutex package. So if the file
// is a symlink we don't want to follow the symlink. The "paths_data" should include
// path of all the files and we shound't need to follow symlink.
if (!(fs::exists(full_path) || fs::is_symlink(full_path)))
{
if (is_warn || is_fail)
{
Expand Down
Loading