-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
94 additions
and
1 deletion.
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
3 changes: 3 additions & 0 deletions
3
conda-store-server/conda_store_server/plugins/lock/__init__.py
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,3 @@ | ||
# Copyright (c) conda-store development team. All rights reserved. | ||
# Use of this source code is governed by a BSD-style | ||
# license that can be found in the LICENSE file. |
85 changes: 85 additions & 0 deletions
85
conda-store-server/conda_store_server/plugins/lock/conda_lock.py
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,85 @@ | ||
# Copyright (c) conda-store development team. All rights reserved. | ||
# Use of this source code is governed by a BSD-style | ||
# license that can be found in the LICENSE file. | ||
|
||
import json | ||
import os | ||
import pathlib | ||
import typing | ||
|
||
import yaml | ||
|
||
from conda_lock.conda_lock import run_lock | ||
|
||
from conda_store_server._internal import conda_utils, schema | ||
from conda_store_server.plugins import hookspec | ||
from conda_store_server.plugins.plugin_context import PluginContext | ||
|
||
|
||
class CondaLock(): | ||
@classmethod | ||
def name(cls): | ||
return "lock-conda_lock" | ||
|
||
def __init__(self, conda_flags="--strict-channel-priority", conda_command="mamba", *kwargs): | ||
self.conda_command = conda_command | ||
self.conda_flags = conda_flags | ||
|
||
@hookspec.hookimpl | ||
def lock_environment( | ||
self, | ||
context: PluginContext, | ||
spec: schema.CondaSpecification, | ||
platforms: typing.List[str] = [conda_utils.conda_platform()], | ||
) -> str: | ||
context.log.info("lock_environment entrypoint for conda-lock") | ||
|
||
environment_filename = pathlib.Path.cwd() / "environment.yaml" | ||
lockfile_filename = pathlib.Path.cwd() / "conda-lock.yaml" | ||
|
||
with environment_filename.open("w") as f: | ||
json.dump(spec.dict(), f) | ||
|
||
context.log.info( | ||
"Note that the output of `conda config --show` displayed below only reflects " | ||
"settings in the conda configuration file, which might be overridden by " | ||
"variables required to be set by conda-store via the environment. Overridden " | ||
f"settings: CONDA_FLAGS={self.conda_flags}" | ||
) | ||
|
||
# The info command can be used with either mamba or conda | ||
context.run_command([self.conda_command, "info"]) | ||
# The config command is not supported by mamba | ||
context.run_command(["conda", "config", "--show"]) | ||
context.run_command(["conda", "config", "--show-sources"]) | ||
|
||
# conda-lock ignores variables defined in the specification, so this code | ||
# gets the value of CONDA_OVERRIDE_CUDA and passes it to conda-lock via | ||
# the with_cuda parameter, see: | ||
# https://github.com/conda-incubator/conda-store/issues/719 | ||
# https://docs.conda.io/projects/conda/en/latest/user-guide/tasks/manage-virtual.html#overriding-detected-packages | ||
# TODO: Support all variables once upstream fixes are made to conda-lock, | ||
# see the discussion in issue 719. | ||
if spec.variables is not None: | ||
cuda_version = spec.variables.get("CONDA_OVERRIDE_CUDA") | ||
else: | ||
cuda_version = None | ||
|
||
# CONDA_FLAGS is used by conda-lock in conda_solver.solve_specs_for_arch | ||
try: | ||
conda_flags_name = "CONDA_FLAGS" | ||
print(f"{conda_flags_name}={self.conda_flags}") | ||
os.environ[conda_flags_name] = self.conda_flags | ||
|
||
run_lock( | ||
environment_files=[environment_filename], | ||
platforms=platforms, | ||
lockfile_path=lockfile_filename, | ||
conda_exe=self.conda_command, | ||
with_cuda=cuda_version, | ||
) | ||
finally: | ||
os.environ.pop(conda_flags_name, None) | ||
|
||
with lockfile_filename.open() as f: | ||
return yaml.safe_load(f) |