-
-
Notifications
You must be signed in to change notification settings - Fork 77
/
hatch_build.py
70 lines (53 loc) · 2.21 KB
/
hatch_build.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
from __future__ import annotations
import json
import os
import sys
import typing as t
from pathlib import Path
from hatchling.builders.hooks.plugin.interface import BuildHookInterface
BASE_DIR = Path(__file__).parent
GREEN, RED, RESET = "\033[0;32m", "\033[0;31m", "\033[0m"
def build_models():
from bokeh.ext import build
print(f"{GREEN}[GEOVIEWS]{RESET} Starting building custom models", flush=True)
geoviews_dir = BASE_DIR / "geoviews"
success = build(geoviews_dir)
if sys.platform != "win32":
# npm can cause non-blocking stdout; so reset it just in case
import fcntl
flags = fcntl.fcntl(sys.stdout, fcntl.F_GETFL)
fcntl.fcntl(sys.stdout, fcntl.F_SETFL, flags & ~os.O_NONBLOCK)
if success:
print(f"{GREEN}[GEOVIEWS]{RESET} Finished building custom models", flush=True)
else:
print(f"{RED}[GEOVIEWS]{RESET} Failed building custom models", flush=True)
sys.exit(1)
def clean_js_version(version):
version = version.replace("-", "")
for dev in ("a", "b", "rc"):
version = version.replace(dev + ".", dev)
return version
def validate_js_version(version):
# TODO: Double check the logic in this function
version = version.split(".post")[0]
with open("./geoviews/package.json") as f:
package_json = json.load(f)
js_version = package_json["version"]
version = version.split("+")[0]
if any(dev in version for dev in ("a", "b", "rc")) and "-" not in js_version:
raise ValueError(
f"geoviews.js dev versions ({js_version}) must separate dev suffix with a dash, e.g. v1.0.0rc1 should be v1.0.0-rc.1."
)
if version != "None" and version != clean_js_version(js_version):
raise ValueError(
f"geoviews.js version ({js_version}) does not match geoviews version ({version}). Cannot build release."
)
class BuildHook(BuildHookInterface):
"""The hatch build hook."""
PLUGIN_NAME = "install"
def initialize(self, version: str, build_data: dict[str, t.Any]) -> None:
"""Initialize the plugin."""
if self.target_name not in ["wheel", "sdist"]:
return
validate_js_version(self.metadata.version)
build_models()