-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy path__init__.py
More file actions
127 lines (99 loc) · 4.13 KB
/
Copy path__init__.py
File metadata and controls
127 lines (99 loc) · 4.13 KB
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
"""PEP 517 build backend wrapper.
Wraps setuptools.build_meta to stage the web UI into the wheel.
Before every wheel/sdist build:
1. Honor ``GOBBY_SKIP_UI_BUILD=1`` to skip npm, while still requiring staged
UI assets for wheel builds.
2. If ``web/`` has ``package.json`` and ``npm`` is on PATH, run
``npm ci && npm run build`` in ``web/`` to produce ``web/dist/``.
3. Copy ``web/dist/`` into ``src/gobby/ui/web/dist/`` so the
``ui/web/dist/**/*`` package-data glob picks the assets up.
4. Verify built wheels contain ``gobby/ui/web/dist/index.html`` so release
artifacts cannot silently ship without the production UI.
Editable installs skip the UI build entirely; the dev workflow uses
``gobby ui dev`` against ``web/`` directly.
"""
from __future__ import annotations
import logging
import os
import shutil
import subprocess # nosec B404
import zipfile
from pathlib import Path
from typing import Any
logger = logging.getLogger(__name__)
def _orig() -> Any:
"""Lazy-import setuptools.build_meta.
Kept lazy so importing this module (e.g., from tests that only exercise
`_stage_ui`) does not require setuptools to be installed at runtime.
"""
from setuptools import build_meta
return build_meta
def __getattr__(name: str) -> Any:
"""Forward PEP 517 hook attributes to setuptools.build_meta on first access."""
if name in {
"get_requires_for_build_wheel",
"get_requires_for_build_sdist",
"get_requires_for_build_editable",
"prepare_metadata_for_build_wheel",
"prepare_metadata_for_build_editable",
}:
return getattr(_orig(), name)
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
_REPO_ROOT = Path(__file__).resolve().parent.parent
_WEB_SRC = _REPO_ROOT / "web"
_DIST_SRC = _WEB_SRC / "dist"
_WHEEL_DEST = _REPO_ROOT / "src" / "gobby" / "ui" / "web" / "dist"
_WHEEL_UI_INDEX = "gobby/ui/web/dist/index.html"
def _stage_ui() -> None:
if os.environ.get("GOBBY_SKIP_UI_BUILD") == "1":
logger.info("GOBBY_SKIP_UI_BUILD=1 - skipping UI build step")
return
have_source = (_WEB_SRC / "package.json").exists()
have_npm = shutil.which("npm") is not None
if have_source and have_npm:
logger.info("Building web UI in %s", _WEB_SRC)
subprocess.run(["npm", "ci"], cwd=_WEB_SRC, check=True) # nosec B603 B607
subprocess.run(["npm", "run", "build"], cwd=_WEB_SRC, check=True) # nosec B603 B607
if not _DIST_SRC.exists():
if _WHEEL_DEST.exists():
logger.info("web/dist not available; reusing pre-staged %s", _WHEEL_DEST)
return
logger.warning(
"web/dist not found and npm build not possible - wheel UI asset verification will fail."
)
return
if _WHEEL_DEST.exists():
shutil.rmtree(_WHEEL_DEST)
_WHEEL_DEST.parent.mkdir(parents=True, exist_ok=True)
shutil.copytree(_DIST_SRC, _WHEEL_DEST)
logger.info("Staged web UI assets at %s", _WHEEL_DEST)
def _verify_wheel_contains_ui(wheel_path: Path) -> None:
with zipfile.ZipFile(wheel_path) as wheel:
if _WHEEL_UI_INDEX not in wheel.namelist():
raise RuntimeError(
f"Built wheel is missing {_WHEEL_UI_INDEX}; build web/dist before publishing."
)
def build_wheel(
wheel_directory: str,
config_settings: dict[str, Any] | None = None,
metadata_directory: str | None = None,
) -> str:
_stage_ui()
wheel_name = str(_orig().build_wheel(wheel_directory, config_settings, metadata_directory))
wheel_path = Path(wheel_name)
if not wheel_path.is_absolute():
wheel_path = Path(wheel_directory) / wheel_path
_verify_wheel_contains_ui(wheel_path)
return wheel_name
def build_sdist(
sdist_directory: str,
config_settings: dict[str, Any] | None = None,
) -> str:
_stage_ui()
return str(_orig().build_sdist(sdist_directory, config_settings))
def build_editable(
wheel_directory: str,
config_settings: dict[str, Any] | None = None,
metadata_directory: str | None = None,
) -> str:
return str(_orig().build_editable(wheel_directory, config_settings, metadata_directory))