forked from bokeh/bokeh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.py
177 lines (151 loc) · 6.57 KB
/
setup.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#-----------------------------------------------------------------------------
# Copyright (c) Anaconda, Inc., and Bokeh Contributors.
# All rights reserved.
#
# The full license is in the file LICENSE.txt, distributed with this software.
#-----------------------------------------------------------------------------
from __future__ import annotations
# Standard library imports
import os, re, subprocess, sys, time
from itertools import product
from pathlib import Path
from shutil import copy, copytree, rmtree
from textwrap import indent
from typing import NoReturn
# External imports
from setuptools import setup # type: ignore[import]
from setuptools.command.build import build # type: ignore[import]
from setuptools.command.editable_wheel import editable_wheel # type: ignore[import]
from setuptools.command.sdist import sdist # type: ignore[import]
# --- Helpers ----------------------------------------------------------------
try:
import colorama
if sys.platform == "win32": colorama.init()
def bright(text: str) -> str: return f"{colorama.Style.BRIGHT}{text}{colorama.Style.RESET_ALL}"
def dim(text: str) -> str: return f"{colorama.Style.DIM}{text}{colorama.Style.RESET_ALL}"
def red(text: str) -> str: return f"{colorama.Fore.RED}{text}{colorama.Style.RESET_ALL}"
def green(text: str) -> str: return f"{colorama.Fore.GREEN}{text}{colorama.Style.RESET_ALL}"
def yellow(text: str) -> str: return f"{colorama.Fore.YELLOW}{text}{colorama.Style.RESET_ALL}"
except ModuleNotFoundError:
def _plain(text: str) -> str: return text
bright = dim = red = green = yellow = _plain
ROOT = Path(__file__).resolve().parent
SRC_ROOT = ROOT / "src"
BUILD_JS = ROOT / 'bokehjs' / 'build' / 'js'
BUILD_TSLIB = ROOT / 'bokehjs' / 'node_modules' / 'typescript' / 'lib'
PKG_STATIC = SRC_ROOT / 'bokeh' / 'server' / 'static'
PKG_JS = PKG_STATIC / 'js'
PKG_TSLIB = PKG_STATIC / 'lib'
COMPONENTS = ("bokeh", "bokeh-widgets", "bokeh-tables", "bokeh-api", "bokeh-gl", "bokeh-mathjax")
JS_FILES = [f"{c}{m}.js" for c, m in product(COMPONENTS, ("", ".min"))]
PACKAGED = (ROOT / 'PKG-INFO').exists()
def build_js() -> None:
print("\nBuilding BokehJS... ", end="")
try:
t0 = time.time()
proc = subprocess.run(["node", "make", "build"], capture_output=True, cwd="bokehjs")
t1 = time.time()
except OSError as e:
die(BUILD_EXEC_FAIL_MSG.format(exc=e))
if proc.returncode != 0:
out = indent(proc.stdout.decode('ascii', errors='ignore'), ' ')
err = indent(proc.stderr.decode('ascii', errors='ignore'), ' ')
die(BUILD_FAIL_MSG.format(stdout=red(out), stderr=red(err)))
out = proc.stdout.decode('ascii', errors='ignore')
pat = re.compile(r"(\[.*\]) (.*)", re.DOTALL)
msg = []
for line in out.strip().split("\n"):
if m := pat.match(line):
stamp, txt = m.groups()
msg.append(f" {dim(green(stamp))} {dim(txt)}")
print(BUILD_SUCCESS_MSG.format(msg="\n".join(msg)))
print(f"\n Build time: {bright(yellow(f'{t1-t0:0.1f} seconds'))}\n")
print("Build artifact sizes:")
try:
for fn in JS_FILES:
size = (BUILD_JS / fn).stat().st_size / 2**10
print(f" - {fn:<20} : {size:6.1f} KB")
except FileNotFoundError as e:
die(BUILD_SIZE_FAIL_MSG.format(exc=e))
def install_js(packages: list[str]) -> None:
print("\nInstalling BokehJS... ", end="")
missing = [fn for fn in JS_FILES if not (BUILD_JS / fn).exists()]
if missing:
die(BOKEHJS_INSTALL_FAIL.format(missing=", ".join(missing)))
if not PKG_JS.is_symlink():
if PKG_JS.exists():
rmtree(PKG_JS)
copytree(BUILD_JS, PKG_JS)
if not PKG_TSLIB.is_symlink():
if PKG_TSLIB.exists():
rmtree(PKG_TSLIB)
if BUILD_TSLIB.exists():
PKG_TSLIB.mkdir()
for lib_file in BUILD_TSLIB.glob("lib.*.d.ts"):
copy(lib_file, PKG_TSLIB)
new = set(
".".join([*Path(parent).relative_to(SRC_ROOT).parts, d])
for parent, dirs, _ in os.walk(PKG_STATIC) for d in dirs
)
existing = set(packages)
packages.extend(tuple(new-existing))
print(SUCCESS)
def build_or_install_bokehjs(packages: list[str]) -> None:
action = os.environ.get("BOKEHJS_ACTION", "build")
if PACKAGED:
kind, loc = "PACKAGED", "bokeh.server.static"
elif action == "install":
kind, loc = "PREVIOUSLY BUILT", "bokehjs/build"
install_js(packages)
elif action == "build":
kind, loc = "NEWLY BUILT", "bokehjs/build"
build_js()
install_js(packages)
else:
raise ValueError(f"Unrecognized action {action!r}")
print(f"Used {bright(yellow(kind))} BokehJS from {loc}\n")
def check_tags() -> None:
if not PACKAGED:
try:
tags = subprocess.check_output(("git", "tag", "-l")).split()
if len(tags) < 5: # arbitrary "too-low" cutoff, there will always be more than 5 tags, if there are any
die(bright(red(MISSING_TAGS)))
except Exception:
print(bright(yellow("!!! Could not check repo tags. Please ensure full tag history")))
def die(x: str) -> NoReturn:
print(f"{x}\n")
sys.exit(1)
MISSING_TAGS = """
!!! This repository is missing git tags! Full tag history is required to build Bokeh.
!!!
!!! See https://docs.bokeh.org/en/latest/docs/dev_guide/setup.html#troubleshooting
"""
SUCCESS = f"{bright(green('Success!'))}\n"
FAILED = f"{bright(red('Failed.'))}\n"
BUILD_SUCCESS_MSG =f"{SUCCESS}\nBuild output:\n\n{{msg}}"
BUILD_SIZE_FAIL_MSG = f"{FAILED}\nERROR: could not determine sizes:\n\n {{exc}}"
BOKEHJS_INSTALL_FAIL = f"{FAILED}\nERROR: Cannot install BokehJS: files missing in bokehjs/build:\n\n {{missing}}"
BUILD_EXEC_FAIL_MSG = f"{FAILED}\nERROR: 'node make build' failed to execute:\n\n {{exc}}"
BUILD_FAIL_MSG = f"""{FAILED}\nERROR: 'node make build' returned the following
---- on stdout:
{{stdout}}
---- on stderr:
{{stderr}}
"""
# --- Setuptools -------------------------------------------------------------
class Build(build): # type: ignore
def run(self) -> None:
check_tags()
build_or_install_bokehjs(self.distribution.packages)
super().run()
class EditableWheel(editable_wheel): # type: ignore
def run(self) -> None:
check_tags()
build_or_install_bokehjs(self.distribution.packages)
super().run()
class Sdist(sdist): # type: ignore
def run(self) -> None:
check_tags()
build_or_install_bokehjs(self.distribution.packages)
super().run()
setup(cmdclass={"build": Build, "editable_wheel": EditableWheel, "sdist": Sdist})