Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
holoviz
GitHub Repository: holoviz/panel
Path: blob/main/hatch_build.py
1979 views
1
from __future__ import annotations
2
3
import json
4
import os
5
import sys
6
import typing as t
7
8
from pathlib import Path
9
10
from hatchling.builders.hooks.plugin.interface import BuildHookInterface
11
12
BASE_DIR = Path(__file__).parent
13
GREEN, RED, RESET = "\033[0;32m", "\033[0;31m", "\033[0m"
14
15
16
def build_models():
17
from bokeh.ext import build
18
19
print(f"{GREEN}[PANEL]{RESET} Starting building custom models", flush=True)
20
panel_dir = BASE_DIR / "panel"
21
success = build(panel_dir)
22
if sys.platform != "win32":
23
# npm can cause non-blocking stdout; so reset it just in case
24
import fcntl
25
26
flags = fcntl.fcntl(sys.stdout, fcntl.F_GETFL)
27
fcntl.fcntl(sys.stdout, fcntl.F_SETFL, flags & ~os.O_NONBLOCK)
28
29
if success:
30
print(f"{GREEN}[PANEL]{RESET} Finished building custom models", flush=True)
31
else:
32
print(f"{RED}[PANEL]{RESET} Failed building custom models", flush=True)
33
sys.exit(1)
34
35
def bundle_resources():
36
sys.path.insert(0, str(BASE_DIR))
37
from panel.compiler import bundle_resources
38
39
print(f"{GREEN}[PANEL]{RESET} Starting bundling custom model resources", flush=True)
40
try:
41
bundle_resources()
42
print(f"{GREEN}[PANEL]{RESET} Finished bundling custom model resources", flush=True)
43
except Exception as e:
44
print(f"{GREEN}[PANEL]{RESET} Failed bundling custom model resources", flush=True)
45
raise e
46
47
def clean_js_version(version):
48
version = version.replace("-", "")
49
for dev in ("a", "b", "rc"):
50
version = version.replace(dev + ".", dev)
51
return version
52
53
54
def validate_js_version(version):
55
# TODO: Double check the logic in this function
56
version = version.split(".post")[0]
57
with open("./panel/package.json") as f:
58
package_json = json.load(f)
59
js_version = package_json["version"]
60
version = version.split("+")[0]
61
if any(dev in version for dev in ("a", "b", "rc")) and "-" not in js_version:
62
raise ValueError(f"panel.js dev versions ({js_version}) must separate dev suffix with a dash, e.g. v1.0.0rc1 should be v1.0.0-rc.1.")
63
if version != "None" and version != clean_js_version(js_version):
64
raise ValueError(f"panel.js version ({js_version}) does not match panel version ({version}). Cannot build release.")
65
66
67
class BuildHook(BuildHookInterface):
68
"""The hatch build hook."""
69
70
PLUGIN_NAME = "install"
71
72
def initialize(self, version: str, build_data: dict[str, t.Any]) -> None:
73
"""Initialize the plugin."""
74
if self.target_name not in ["wheel", "sdist"]:
75
return
76
77
validate_js_version(self.metadata.version)
78
79
if "PANEL_LITE" not in os.environ:
80
build_models()
81
bundle_resources()
82
83