Skip to content

Commit

Permalink
ENH: determine jupyter and notebooks extras
Browse files Browse the repository at this point in the history
  • Loading branch information
redeboer committed Oct 16, 2024
1 parent f2582c8 commit 7f30368
Showing 1 changed file with 22 additions and 28 deletions.
50 changes: 22 additions & 28 deletions src/compwa_policy/check_dev_files/binder.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,35 +23,12 @@

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


def _check_optional_dependencies() -> None:
required_for_binder = "This is required for Binder."
if not CONFIG_PATH.pyproject.exists():
msg = f"{CONFIG_PATH.pyproject} does not exist. {required_for_binder}"
raise PrecommitError(msg)
pyproject = Pyproject.load()
table_key = "project.optional-dependencies"
if not pyproject.has_table(table_key):
msg = f"Missing [{table_key}] in {CONFIG_PATH.pyproject}. {required_for_binder}"
raise PrecommitError
optional_dependencies = pyproject.get_table(table_key)
optional_dependency_sections = set(optional_dependencies)
expected_sections = {"jupyter", "notebooks"}
missing_sections = expected_sections - optional_dependency_sections
if missing_sections:
msg = (
f"Missing sections in [{table_key}]: {', '.join(sorted(missing_sections))}"
f". {required_for_binder}"
)
raise PrecommitError(msg)


def _update_apt_txt(apt_packages: list[str]) -> None:
apt_txt = CONFIG_PATH.binder / "apt.txt"
if not apt_packages:
Expand All @@ -74,21 +51,26 @@ def _update_post_build() -> None:
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 \\"
for extra in notebook_extras:
expected_content += f"\n --extra {extra} \\"
expected_content += dedent(R"""
uv export \
--extra jupyter \
--extra notebooks \
> requirements.txt
uv pip install \
--requirement requirements.txt \
--system
uv cache clean
""")
else:
expected_content += dedent(R"""
package = "."
if notebook_extras:
package = f"'.[{','.join(notebook_extras)}]'"
expected_content += dedent(Rf"""
uv pip install \
--editable '.[jupyter,notebooks]' \
--editable {package} \
--no-cache \
--system
""")
Expand All @@ -98,6 +80,18 @@ def _update_post_build() -> None:
)


def __get_notebook_extras() -> list[str]:
if not CONFIG_PATH.pyproject.exists():
return []
pyproject = Pyproject.load()
table_key = "project.optional-dependencies"
if not pyproject.has_table(table_key):
return []
optional_dependencies = pyproject.get_table(table_key)
allowed_sections = {"jupyter", "notebooks"}
return sorted(allowed_sections & set(optional_dependencies))


def _make_executable(path: Path) -> None:
if os.access(path, os.X_OK):
return
Expand Down

0 comments on commit 7f30368

Please sign in to comment.