def conda_lock(specification: schema.CondaSpecification, conda_exe: str = "mamba"): from conda_lock.conda_lock import run_lock from conda.models.dist import Dist with tempfile.TemporaryDirectory() as tmpdir: environment_path = pathlib.Path(tmpdir) / "environment.yaml" lockfile_path = pathlib.Path(tmpdir) / "environment-lock.yaml" with environment_path.open("w") as f: f.write(specification.json()) try: run_lock( environment_files=[environment_path], platforms=[conda_platform()], lockfile_path=lockfile_path, conda_exe=conda_exe, ) except subprocess.CalledProcessError as e: raise ValueError(e.output) with lockfile_path.open() as f: lockfile = yaml.safe_load(f) conda_packages = [] pip_packages = [] for package in lockfile["package"]: if package["manager"] == "conda": dist = Dist.from_string(package["url"]) conda_packages.append({ "name": dist.name, "build": dist.build, "build_number": dist.build_number, "constrains": None, "depends": [], "license": None, "license_family": None, "size": -1, "subdir": dist.subdir, "timestamp": None, "version": dist.version, "channel_id": dist.base_url, "md5": package["hash"].get("md5"), "sha256": package["hash"].get("sha256", ""), "summary": None, "description": None, }) elif package["manager"] == "pip": pip_packages.append({ "name": package["name"], "url": package["url"], "version": package["version"], "sha256": package["hash"]["sha256"], }) return {"conda": conda_packages, "pip": pip_packages}
def test_run_lock_with_pip_environment_different_names_same_deps( monkeypatch, pip_environment_different_names_same_deps, conda_exe): with filelock.FileLock( str(pip_environment_different_names_same_deps.parent / "filelock")): monkeypatch.chdir(pip_environment_different_names_same_deps.parent) if is_micromamba(conda_exe): monkeypatch.setenv("CONDA_FLAGS", "-v") run_lock([pip_environment_different_names_same_deps], conda_exe=conda_exe)
def test_run_lock_with_locked_environment_files(monkeypatch, update_environment, conda_exe): """run_lock() with default args uses source files from lock""" monkeypatch.chdir(update_environment.parent) make_lock_files = MagicMock() monkeypatch.setattr("conda_lock.conda_lock.make_lock_files", make_lock_files) run_lock(DEFAULT_FILES, conda_exe=conda_exe, update=["pydantic"]) assert [ p.resolve() for p in make_lock_files.call_args.kwargs["src_files"] ] == [ pathlib.Path(update_environment.parent / "environment-preupdate.yml") ]
def test_run_lock_with_update(monkeypatch, update_environment, conda_exe): monkeypatch.chdir(update_environment.parent) if is_micromamba(conda_exe): monkeypatch.setenv("CONDA_FLAGS", "-v") pre_lock = { p.name: p for p in parse_conda_lock_file(update_environment.parent / DEFAULT_LOCKFILE_NAME).package } run_lock([update_environment], conda_exe=conda_exe, update=["pydantic"]) post_lock = { p.name: p for p in parse_conda_lock_file(update_environment.parent / DEFAULT_LOCKFILE_NAME).package } assert post_lock["pydantic"].version == "1.8.2" assert post_lock["python"].version == pre_lock["python"].version
def test_run_lock_with_input_hash_check( monkeypatch, input_hash_zlib_environment: pathlib.Path, conda_exe, capsys): with filelock.FileLock(str(input_hash_zlib_environment.parent / "filelock")): monkeypatch.chdir(input_hash_zlib_environment.parent) if is_micromamba(conda_exe): monkeypatch.setenv("CONDA_FLAGS", "-v") lockfile = input_hash_zlib_environment.parent / "conda-linux-64.lock" if lockfile.exists(): lockfile.unlink() run_lock( [input_hash_zlib_environment], platforms=["linux-64"], conda_exe=conda_exe, check_input_hash=True, ) stat = lockfile.stat() created = stat.st_mtime_ns with open(lockfile) as f: previous_hash = extract_input_hash(f.read()) assert previous_hash is not None assert len(previous_hash) == 64 capsys.readouterr() run_lock( [input_hash_zlib_environment], platforms=["linux-64"], conda_exe=conda_exe, check_input_hash=True, ) stat = lockfile.stat() assert stat.st_mtime_ns == created output = capsys.readouterr() assert "Spec hash already locked for" in output.err
def test_run_lock_mamba(monkeypatch, zlib_environment): if not shutil.which("mamba"): raise pytest.skip("mamba is not installed") monkeypatch.chdir(zlib_environment.parent) run_lock(zlib_environment, conda_exe="mamba")
def test_run_lock_conda(monkeypatch, zlib_environment): monkeypatch.chdir(zlib_environment.parent) run_lock(zlib_environment, conda_exe="conda")
def test_run_lock(monkeypatch, zlib_environment, conda_exe): monkeypatch.chdir(zlib_environment.parent) run_lock([zlib_environment], conda_exe=conda_exe)
def test_run_lock_with_pip(monkeypatch, pip_environment, conda_exe): monkeypatch.chdir(pip_environment.parent) if is_micromamba(conda_exe): monkeypatch.setenv("CONDA_FLAGS", "-v") run_lock([pip_environment], conda_exe=conda_exe)
def test_run_lock_blas_mkl(monkeypatch, blas_mkl_environment, conda_exe): monkeypatch.chdir(blas_mkl_environment.parent) run_lock([blas_mkl_environment], conda_exe=conda_exe)
def test_run_lock(monkeypatch, zlib_environment, conda_exe): monkeypatch.chdir(zlib_environment.parent) if is_micromamba(conda_exe): monkeypatch.setenv("CONDA_FLAGS", "-v") run_lock([zlib_environment], conda_exe=conda_exe)