From c607e7df6614cebb00e989a7396e0820a2a5a3fa 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 | 31 +++++++++++++++++++++++++++++++ pyproject.toml | 8 ++++---- 2 files changed, 35 insertions(+), 4 deletions(-) create mode 100644 hatch_build.py diff --git a/hatch_build.py b/hatch_build.py new file mode 100644 index 00000000..f0990e7e --- /dev/null +++ b/hatch_build.py @@ -0,0 +1,31 @@ +import logging +import os +import sys + +from hatch_jupyter_builder.plugin import JupyterBuildHook + +LOGGER = logging.getLogger(__name__) + + +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, _): + if "/.cache/pre-commit/".replace("/", os.path.sep) in sys.executable: + LOGGER.info( + "This environment looks like a pre-commit environment. " + "We will skip the build of the Jupytext extension for JupyterLab." + ) + 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..5835093f 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" ] @@ -154,7 +154,7 @@ ensured-targets = [ # the extension, build will be triggered even if the build assets exist skip-if-exists = ["jupyterlab/jupyterlab_jupytext/labextension/static/style.js"] -[tool.hatch.build.hooks.jupyter-builder.build-kwargs] +[tool.hatch.build.hooks.custom.build-kwargs] # Root directory where build should be done path = "jupyterlab" # Build command that is defined in package.json @@ -162,7 +162,7 @@ build_cmd = "build:prod" # We use jlpm, which is wrapper around yarn to build transpiled assets npm = ["jlpm"] -[tool.hatch.build.hooks.jupyter-builder.editable-build-kwargs] +[tool.hatch.build.hooks.custom.editable-build-kwargs] path = "jupyterlab" build_cmd = "build" npm = ["jlpm"]