| 1 |
"""A custom hatch build hook for ipykernel.""" |
| 2 |
|
| 3 |
import shutil |
| 4 |
import sys |
| 5 |
from pathlib import Path |
| 6 |
|
| 7 |
from hatchling.builders.hooks.plugin.interface import BuildHookInterface |
| 8 |
|
| 9 |
|
| 10 |
class CustomHook(BuildHookInterface): |
| 11 |
"""The IPykernel build hook.""" |
| 12 |
|
| 13 |
def initialize(self, version, build_data): |
| 14 |
"""Initialize the hook.""" |
| 15 |
here = Path(__file__).parent.resolve() |
| 16 |
sys.path.insert(0, str(here)) |
| 17 |
from ipykernel.kernelspec import make_ipkernel_cmd, write_kernel_spec |
| 18 |
|
| 19 |
overrides = {} |
| 20 |
|
| 21 |
# When building a standard wheel, the executable specified in the kernelspec is simply 'python'. |
| 22 |
if version == "standard": |
| 23 |
overrides["metadata"] = dict(debugger=True) |
| 24 |
argv = make_ipkernel_cmd(executable="python") |
| 25 |
|
| 26 |
# When installing an editable wheel, the full `sys.executable` can be used. |
| 27 |
else: |
| 28 |
argv = make_ipkernel_cmd() |
| 29 |
|
| 30 |
overrides["argv"] = argv |
| 31 |
|
| 32 |
dest = Path(here) / "data_kernelspec" |
| 33 |
if Path(dest).exists(): |
| 34 |
shutil.rmtree(dest) |
| 35 |
|
| 36 |
write_kernel_spec(dest, overrides=overrides) |