Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENH: automatically update pip install cell metadata #218

Merged
merged 2 commits into from
Nov 20, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 22 additions & 12 deletions src/repoma/pin_nb_requirements.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
from typing import List, Optional, Sequence

import nbformat
from nbformat import NotebookNode

from repoma.utilities.executor import Executor

from .errors import PrecommitError

Expand All @@ -31,9 +34,11 @@ def check_pinned_requirements(filename: str) -> None:
cell_content = "".join(s.strip("\\") for s in src_lines)
if not cell_content.startswith(__PIP_INSTALL_STATEMENT):
continue
__check_install_statement(filename, cell_content)
__check_requirements(filename, cell_content)
__check_metadata(filename, cell["metadata"])
executor = Executor()
executor(__check_install_statement, filename, cell_content)
executor(__check_requirements, filename, cell_content)
executor(__update_metadata, filename, cell["metadata"], notebook)
executor.finalize()
return
msg = (
f'Notebook "{filename}" does not contain a pip install cell of the form'
Expand Down Expand Up @@ -93,18 +98,23 @@ def __check_requirements(filename: str, install_statement: str) -> None:
raise PrecommitError(msg)


def __check_metadata(filename: str, metadata: dict) -> None:
source_hidden = metadata.get("jupyter", {}).get("source_hidden")
if not source_hidden:
msg = f'Install cell in notebook "{filename}" is not hidden'
raise PrecommitError(msg)
def __update_metadata(filename: str, metadata: dict, notebook: NotebookNode) -> None:
updated_metadata = False
jupyter_metadata = metadata.get("jupyter")
if jupyter_metadata is not None and jupyter_metadata.get("source_hidden"):
if len(jupyter_metadata) == 1:
metadata.pop("jupyter")
else:
jupyter_metadata.pop("source_hidden")
updated_metadata = True
tags = set(metadata.get("tags", []))
expected_tags = {"remove-cell"}
if expected_tags != tags:
msg = (
f'Install cell in notebook "{filename}" should have tags'
f" {sorted(expected_tags)}"
)
metadata["tags"] = sorted(expected_tags)
updated_metadata = True
if updated_metadata:
nbformat.write(notebook, filename)
msg = f'Updated metadata of pip install cell in notebook "{filename}"'
raise PrecommitError(msg)


Expand Down
Loading