def do_conda_install(conda: PathLike, prefix: str, name: str, file: str) -> None: _conda = partial(_invoke_conda, conda, prefix, name, check_call=True) kind = "env" if file.endswith(".yml") else "explicit" if kind == "explicit": with open(file) as explicit_env: pip_requirements = [ line.split("# pip ")[1] for line in explicit_env if line.startswith("# pip ") ] else: pip_requirements = [] _conda([ *(["env"] if kind == "env" and not is_micromamba(conda) else []), "create", "--file", file, *([] if kind == "env" else ["--yes"]), ], ) if not pip_requirements: return with tempfile.NamedTemporaryFile() as tf: write_file("\n".join(pip_requirements), tf.name) _conda(["run"], ["pip", "install", "--no-deps", "-r", tf.name])
def _add_auth(lockfile: str, auth: Dict[str, str]) -> Iterator[str]: lockfile_with_auth = _add_auth_to_lockfile(lockfile, auth) # On Windows, NamedTemporaryFiles can't be opened a second time, so we have to close it first (and delete it manually later) tf = tempfile.NamedTemporaryFile(delete=False) try: tf.close() write_file(lockfile_with_auth, tf.name) yield tf.name finally: os.unlink(tf.name)
def lock( conda, mamba, micromamba, platform, channel_overrides, dev_dependencies, files, filename_template, strip_auth, ): """Generate fully reproducible lock files for conda environments. By default, the lock files are written to conda-{platform}.lock. These filenames can be customized using the --filename-template argument. The following tokens are available: \b platform: The platform this lock file was generated for (conda subdir). dev-dependencies: Whether or not dev dependencies are included in this lock file. spec-hash: A sha256 hash of the lock file spec. version: The version of conda-lock used to generate this lock file. timestamp: The approximate timestamp of the output file in ISO8601 basic format. """ files = [pathlib.Path(file) for file in files] lock_func = partial( run_lock, environment_files=files, conda_exe=conda, platforms=platform, mamba=mamba, micromamba=micromamba, include_dev_dependencies=dev_dependencies, channel_overrides=channel_overrides, ) if strip_auth: with tempfile.TemporaryDirectory() as tempdir: filename_template_temp = f"{tempdir}/{filename_template.split('/')[-1]}" lock_func(filename_template=filename_template_temp) filename_template_dir = "/".join(filename_template.split("/")[:-1]) for file in os.listdir(tempdir): lockfile = read_file(os.path.join(tempdir, file)) lockfile = _strip_auth_from_lockfile(lockfile) write_file(lockfile, os.path.join(filename_template_dir, file)) else: lock_func(filename_template=filename_template)
def _add_auth(lockfile: str, auth: Dict[str, str]) -> Iterator[str]: with tempfile.NamedTemporaryFile() as tf: lockfile_with_auth = _add_auth_to_lockfile(lockfile, auth) write_file(lockfile_with_auth, tf.name) yield tf.name
def lock( conda, mamba, micromamba, platform, channel_overrides, dev_dependencies, files, kind, filename_template, lockfile, strip_auth, extras, check_input_hash: bool, log_level, pdb, virtual_package_spec, update=None, ): """Generate fully reproducible lock files for conda environments. By default, the lock files are written to conda-{platform}.lock. These filenames can be customized using the --filename-template argument. The following tokens are available: \b platform: The platform this lock file was generated for (conda subdir). dev-dependencies: Whether or not dev dependencies are included in this lock file. input-hash: A sha256 hash of the lock file input specification. version: The version of conda-lock used to generate this lock file. timestamp: The approximate timestamp of the output file in ISO8601 basic format. """ logging.basicConfig(level=log_level) if pdb: def handle_exception(exc_type, exc_value, exc_traceback): import pdb pdb.post_mortem(exc_traceback) sys.excepthook = handle_exception if not virtual_package_spec: candidates = [ pathlib.Path("virtual-packages.yml"), pathlib.Path("virtual-packages.yaml"), ] for c in candidates: if c.exists(): logger.info("Using virtual packages from %s", c) virtual_package_spec = c break else: virtual_package_spec = pathlib.Path(virtual_package_spec) files = [pathlib.Path(file) for file in files] extras = set(extras) lock_func = partial( run_lock, environment_files=files, conda_exe=conda, platforms=platform, mamba=mamba, micromamba=micromamba, include_dev_dependencies=dev_dependencies, channel_overrides=channel_overrides, kinds=kind, lockfile_path=pathlib.Path(lockfile), extras=extras, virtual_package_spec=virtual_package_spec, update=update, ) if strip_auth: with tempfile.TemporaryDirectory() as tempdir: filename_template_temp = f"{tempdir}/{filename_template.split('/')[-1]}" lock_func(filename_template=filename_template_temp) filename_template_dir = "/".join(filename_template.split("/")[:-1]) for file in os.listdir(tempdir): lockfile = read_file(os.path.join(tempdir, file)) lockfile = _strip_auth_from_lockfile(lockfile) write_file(lockfile, os.path.join(filename_template_dir, file)) else: lock_func(filename_template=filename_template, check_input_hash=check_input_hash)