def _prepare_env(session: nox.sessions.Session) -> None: lockfile = _session_lockfile(session) venv_dir = session.virtualenv.location_name if not _venv_populated(session): # Environment has been created but packages not yet installed. # Populate the environment from the lockfile. logger.debug(f"Populating conda env: {venv_dir}") session.conda_install(f"--file={lockfile}") _cache_venv(session) elif _venv_changed(session): # Destroy the environment and rebuild it. logger.debug(f"Lockfile changed. Recreating conda env: {venv_dir}") _reuse_original = session.virtualenv.reuse_existing session.virtualenv.reuse_existing = False session.virtualenv.create() session.conda_install(f"--file={lockfile}") session.virtualenv.reuse_existing = _reuse_original _cache_venv(session) logger.debug(f"Environment up to date: {venv_dir}") iris_artifact = _get_iris_github_artifact(session) if iris_artifact: # Install the iris source in develop mode. tmp_dir = Path(session.create_tmp()) iris_dir = tmp_dir / "iris" cwd = Path.cwd() if not iris_dir.is_dir(): session.run_always("git", "clone", IRIS_GITHUB, str(iris_dir), external=True) session.cd(str(iris_dir)) session.run_always("git", "fetch", "origin", external=True) session.run_always("git", "checkout", iris_artifact, external=True) session.cd(str(cwd)) session.install("--no-deps", "--editable", str(iris_dir)) # Determine whether verbose diagnostics have been requested # from the command line. verbose = "-v" in session.posargs or "--verbose" in session.posargs if verbose: session.run_always("conda", "info") session.run_always("conda", "list", f"--prefix={venv_dir}") session.run_always( "conda", "list", f"--prefix={venv_dir}", "--explicit", )
def session_cachefile(session: nox.sessions.Session) -> Path: """Returns the path of the session lockfile cache.""" lockfile = session_lockfile(session) tmp_dir = Path(session.create_tmp()) cache = tmp_dir / lockfile.name return cache
def update_lockfiles(session: nox.sessions.Session): """ Re-resolve env specs and store as lockfiles (``requirements/nox.lock/``). Original Conda environment specifications are at: ``requirements/py**.yml``. The output lock files denote the dependencies that iris-esmf-regrid is tested against, and therefore officially supports. Parameters ---------- session: object A `nox.sessions.Session` object. """ session.install("conda-lock") for req_file in Path("requirements").glob(r"py[0-9]*.yml"): python_string = req_file.stem # Generate the appropriate conda-lock template name, keeping the {platform} # placeholder to support conda-lock's internals. filename_template = _lockfile_path(python_string, platform_placeholder=True) lockfile_path = _lockfile_path(python_string, platform_placeholder=False) # Create the parent directory if it doesn't already exist. try: filename_template.parent.mkdir() except FileExistsError: pass # Use a copy of the requirements file in a tmp dir - the file will # be modified if installing a custom Iris checkout. tmp_dir = Path(session.create_tmp()) req_file_local = tmp_dir / req_file.name shutil.copy(req_file, req_file_local) conda_lock_cmd = [ "conda-lock", "lock", f"--filename-template={filename_template}", f"--file={req_file_local}", f"--platform={LOCKFILE_PLATFORM}", ] # Get the requirements for Iris too, if an Iris checkout is specified. iris_artifact = _get_iris_github_artifact(session) if iris_artifact: # Remove ``iris`` from dependencies, if present. with req_file_local.open("r+") as file: reqs = yaml.load(file, Loader=yaml.FullLoader) reqs["dependencies"] = [ spec for spec in reqs["dependencies"] if not spec.startswith("iris") ] yaml.dump(reqs, file) iris_req_name = f"{python_string}.yml" iris_req_url = (f"https://raw.githubusercontent.com/SciTools/iris/" f"{iris_artifact}/requirements/ci/{iris_req_name}") iris_req_file = (tmp_dir / iris_req_name).with_stem(f"{python_string}-iris") iris_req = urlopen(iris_req_url).read() with iris_req_file.open("wb") as file: file.write(iris_req) # Conda-lock can resolve multiple requirements files together. conda_lock_cmd.append(f"--file={iris_req_file}") session.run(*conda_lock_cmd, silent=True) print(f"Conda lock file created: {lockfile_path}")