Skip to content

Commit

Permalink
FEAT: implement Binder configuration for pixi+uv (#447)
Browse files Browse the repository at this point in the history
  • Loading branch information
redeboer authored Oct 17, 2024
1 parent 24c7c41 commit c183850
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/compwa_policy/check_dev_files/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def main(argv: Sequence[str] | None = None) -> int: # noqa: PLR0915
)
if has_notebooks:
if not args.no_binder:
do(binder.main, dev_python_version, doc_apt_packages)
do(binder.main, package_manager, dev_python_version, doc_apt_packages)
do(jupyter.main, args.no_ruff)
do(nbstripout.main, precommit_config, _to_list(args.allowed_cell_metadata))
do(
Expand Down
70 changes: 62 additions & 8 deletions src/compwa_policy/check_dev_files/binder.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,18 @@
if TYPE_CHECKING:
from pathlib import Path

from compwa_policy.check_dev_files.conda import PackageManagerChoice
from compwa_policy.utilities.pyproject import PythonVersion


def main(python_version: PythonVersion, apt_packages: list[str]) -> None:
def main(
package_manager: PackageManagerChoice,
python_version: PythonVersion,
apt_packages: list[str],
) -> None:
with Executor() as do:
do(_update_apt_txt, apt_packages)
do(_update_post_build)
do(_update_post_build, package_manager)
do(_make_executable, CONFIG_PATH.binder / "postBuild")
do(_update_runtime_txt, python_version)

Expand All @@ -44,14 +49,66 @@ def _update_apt_txt(apt_packages: list[str]) -> None:
)


def _update_post_build() -> None:
def _update_post_build(package_manager: PackageManagerChoice) -> None:
if package_manager == "pixi+uv":
expected_content = __get_post_builder_for_pixi_with_uv()
elif package_manager == "uv":
expected_content = __get_post_builder_for_uv()
else:
msg = f"Package manager {package_manager} is not supported."
raise NotImplementedError(msg)
__update_file(
expected_content.strip() + "\n",
path=CONFIG_PATH.binder / "postBuild",
)


def __get_post_builder_for_pixi_with_uv() -> str:
expected_content = dedent("""
#!/bin/bash
set -ex
curl -LsSf https://pixi.sh/install.sh | bash
export PATH="$HOME/.pixi/bin:$PATH"
pixi_packages="$(NO_COLOR= pixi list --explicit --no-install | awk 'NR > 1 {print $1}')"
if [[ -n "$pixi_packages" ]]; then
pixi global install $pixi_packages
fi
pixi clean cache --yes
""").strip()
expected_content += "\n"
notebook_extras = __get_notebook_extras()
if "uv.lock" in set(git_ls_files(untracked=True)):
expected_content += "\nuv export \\"
for extra in notebook_extras:
expected_content += f"\n --extra {extra} \\"
expected_content += dedent(R"""
> requirements.txt
uv pip install \
--requirement requirements.txt \
--system
uv cache clean
""")
else:
package = "."
if notebook_extras:
package = f"'.[{','.join(notebook_extras)}]'"
expected_content += dedent(Rf"""
uv pip install \
--editable {package} \
--no-cache \
--system
""")
return expected_content


def __get_post_builder_for_uv() -> str:
expected_content = dedent("""
#!/bin/bash
set -ex
curl -LsSf https://astral.sh/uv/install.sh | sh
source $HOME/.cargo/env
""").strip()

notebook_extras = __get_notebook_extras()
if "uv.lock" in set(git_ls_files(untracked=True)):
expected_content += "\nuv export \\"
Expand All @@ -74,10 +131,7 @@ def _update_post_build() -> None:
--no-cache \
--system
""")
__update_file(
expected_content.strip() + "\n",
path=CONFIG_PATH.binder / "postBuild",
)
return expected_content


def __get_notebook_extras() -> list[str]:
Expand Down

0 comments on commit c183850

Please sign in to comment.