From b804afbe2e32c997892d1ddf6dc08b4c5068892f Mon Sep 17 00:00:00 2001 From: Marc Wouts Date: Sat, 27 Apr 2024 17:47:45 +0100 Subject: [PATCH] Don't build the extension in pre-commit envs --- hatch_build.py | 34 ++++++++++++++++++++++++++++++++++ pyproject.toml | 4 ++-- 2 files changed, 36 insertions(+), 2 deletions(-) create mode 100644 hatch_build.py diff --git a/hatch_build.py b/hatch_build.py new file mode 100644 index 00000000..1303488d --- /dev/null +++ b/hatch_build.py @@ -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 diff --git a/pyproject.toml b/pyproject.toml index 6d61197d..e12cb36c 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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" ]