Skip to content

Commit

Permalink
Don't build the extension in pre-commit envs
Browse files Browse the repository at this point in the history
  • Loading branch information
mwouts committed Apr 27, 2024
1 parent 12644e1 commit b804afb
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
34 changes: 34 additions & 0 deletions hatch_build.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import os
import sys
from typing import Any

from hatch_jupyter_builder.plugin import JupyterBuildHook, _get_log


class CustomBuildHook(JupyterBuildHook):
"""
We use a custom build hook to detect pre-commit environments. In those
environments there is no need to build the extension (and, in addition
the build will fail if npm is not available, which is often the case
of pre-commit environments).
"""

def initialize(self, version: str, _: dict[str, Any]) -> None:
log = _get_log()
log.info(
"Preparing the build of the JupyterLab extension for Jupytext "
f"in this environment: {sys.executable}"
)
if "/.cache/pre-commit/".replace("/", os.path.sep) in sys.executable:
log.info(
"This environment looks like a pre-commit environment. "
"Skipping the build of the extension"
)
self._skipped = True
return

return super().initialize(version=version, _=_)


# hatch wants a single hook
del JupyterBuildHook
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -130,14 +130,14 @@ packages = ["src/jupytext", "src/jupytext_config", "jupyterlab/jupyterlab_jupyte
"jupyterlab/jupyter-config" = "etc/jupyter"
"jupyterlab/jupyterlab_jupytext/labextension" = "share/jupyter/labextensions/jupyterlab-jupytext"

[tool.hatch.build.hooks.jupyter-builder]
[tool.hatch.build.hooks.custom]
# Enable running hook by default.
# We disable this hook only by setting env var HATCH_BUILD_HOOKS_ENABLE=false
# So `HATCH_BUILD_HOOKS_ENABLE=false pip install .` will **not** build JupyterLab related
# extension. A simple `pip install .` will **always** build extension
enable-by-default = true
# Runtime dependency for this build hook
# We need jupyterlab as build time depdendency to get jlpm (wrapper around yarn)
# We need jupyterlab as build time dependency to get jlpm (wrapper around yarn)
dependencies = [
"hatch-jupyter-builder>=0.5", "jupyterlab>=4"
]
Expand Down

0 comments on commit b804afb

Please sign in to comment.