-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild-python-distributions.sh
More file actions
executable file
·242 lines (213 loc) · 7.01 KB
/
Copy pathbuild-python-distributions.sh
File metadata and controls
executable file
·242 lines (213 loc) · 7.01 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
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#!/bin/bash
set -euo pipefail
usage() {
cat <<'USAGE'
Usage: scripts/build-python-distributions.sh [--dry-run] [--skip-clean-install] [--dist-root DIR] PACKAGE_DIR...
Build and validate Wright Python package release candidates.
Options:
--dry-run Validate package metadata without building artifacts.
--skip-clean-install Build artifacts but skip isolated pip install/import validation.
--dist-root DIR Directory for built artifacts (default: dist/python-packages).
-h, --help Show this help message.
USAGE
}
ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
DIST_ROOT="$ROOT_DIR/dist/python-packages"
DRY_RUN=0
SKIP_CLEAN_INSTALL="${WRIGHT_SKIP_CLEAN_INSTALL:-0}"
PACKAGES=()
while [ "$#" -gt 0 ]; do
case "$1" in
--dry-run)
DRY_RUN=1
shift
;;
--skip-clean-install)
SKIP_CLEAN_INSTALL=1
shift
;;
--dist-root)
if [ "$#" -lt 2 ]; then
echo "--dist-root requires a directory" >&2
exit 2
fi
DIST_ROOT="$2"
shift 2
;;
-h|--help)
usage
exit 0
;;
--*)
echo "Unknown option: $1" >&2
usage >&2
exit 2
;;
*)
PACKAGES+=("$1")
shift
;;
esac
done
if [ "${#PACKAGES[@]}" -eq 0 ]; then
echo "At least one package directory is required." >&2
usage >&2
exit 2
fi
python_bin="${PYTHON:-python3}"
validate_metadata() {
local package_dir="$1"
"$python_bin" - "$package_dir" <<'PY'
from __future__ import annotations
import pathlib
import sys
try:
import tomllib
except ModuleNotFoundError: # pragma: no cover
import tomli as tomllib
package_dir = pathlib.Path(sys.argv[1])
pyproject = package_dir / "pyproject.toml"
if not pyproject.exists():
raise SystemExit(f"{package_dir}: missing pyproject.toml")
with pyproject.open("rb") as fh:
data = tomllib.load(fh)
project = data.get("project", {})
required = ["name", "version", "description", "readme", "requires-python", "license", "dependencies"]
missing = [key for key in required if key not in project]
if missing:
raise SystemExit(f"{pyproject}: missing required metadata fields: {', '.join(missing)}")
readme = package_dir / str(project["readme"])
if not readme.exists():
raise SystemExit(f"{pyproject}: readme file does not exist: {readme}")
urls = project.get("urls", {})
for key in ("Homepage", "Source", "Issues", "Documentation", "Releases"):
if key not in urls or not str(urls[key]).startswith("https://"):
raise SystemExit(f"{pyproject}: missing https project URL {key}")
deps = [str(dep) for dep in project.get("dependencies", [])]
name = project.get("name")
if name == "wright-tool-registry":
if not any(dep.startswith("wright-core>=") and ",<" in dep for dep in deps):
raise SystemExit("wright-tool-registry must depend on a bounded wright-core release")
print(f"metadata ok: {name} {project.get('version')}")
PY
}
built_artifacts=()
built_import_modules=()
for package in "${PACKAGES[@]}"; do
package_dir="$package"
if [ ! -d "$package_dir" ]; then
package_dir="$ROOT_DIR/$package"
fi
if [ ! -d "$package_dir" ]; then
echo "Package directory not found: $package" >&2
exit 1
fi
validate_metadata "$package_dir"
if [ "$DRY_RUN" = "1" ]; then
continue
fi
package_name="$($python_bin - "$package_dir/pyproject.toml" <<'PY'
from __future__ import annotations
import pathlib, re, sys
try:
import tomllib
except ModuleNotFoundError:
import tomli as tomllib
with pathlib.Path(sys.argv[1]).open('rb') as fh:
data = tomllib.load(fh)
print(re.sub(r'[^A-Za-z0-9_.-]+', '-', data['project']['name']), end="")
PY
)"
case "$package_name" in
wright-engineering) built_import_modules+=("wright_engineering") ;;
wright-core) built_import_modules+=("core") ;;
wright-tool-registry) built_import_modules+=("tool_registry") ;;
wright-data-vault) built_import_modules+=("data_vault") ;;
wright-agent-adapters) built_import_modules+=("agent_adapters") ;;
wright-workspace-service) built_import_modules+=("workspace_service") ;;
*) built_import_modules+=("${package_name//-/_}") ;;
esac
out_dir="$DIST_ROOT/$package_name"
rm -rf "$out_dir"
mkdir -p "$out_dir"
if [ "$package_name" = "wright-engineering" ]; then
native_args=(
"$ROOT_DIR/scripts/build-native-runtime.py"
--output "$out_dir"
--evidence "$out_dir/native-build-evidence.json"
)
if [ "${WRIGHT_NATIVE_SKIP_FRONTEND_BUILD:-0}" = "1" ]; then
native_args+=(--skip-frontend-build)
fi
"$python_bin" "${native_args[@]}"
else
"$python_bin" -m build --sdist --wheel --outdir "$out_dir" "$package_dir"
fi
wheel="$(find "$out_dir" -maxdepth 1 -type f -name '*.whl' | sort | tail -1)"
sdist="$(find "$out_dir" -maxdepth 1 -type f \( -name '*.tar.gz' -o -name '*.zip' \) | sort | tail -1)"
if [ -z "$wheel" ] || [ -z "$sdist" ]; then
echo "Expected both wheel and source distribution in $out_dir" >&2
exit 1
fi
built_artifacts+=("$wheel" "$sdist")
"$python_bin" - "$ROOT_DIR" "$wheel" "$sdist" <<'PY'
from __future__ import annotations
import pathlib
import sys
root = pathlib.Path(sys.argv[1])
sys.path.insert(0, str(root))
from scripts.release.python_artifacts import artifact_evidence, validate_native_distribution
artifacts = []
for value in sys.argv[2:]:
path = pathlib.Path(value)
evidence, manifest = artifact_evidence(path)
if evidence.filename.lower().startswith("wright_engineering-"):
validate_native_distribution(path)
artifacts.append(evidence)
path.with_name(path.name + ".contents.txt").write_text(
manifest, encoding="utf-8", newline="\n"
)
checksum_path = pathlib.Path(sys.argv[2]).parent / "SHA256SUMS"
checksum_path.write_text(
"".join(f"{item.sha256} {item.filename}\n" for item in artifacts),
encoding="utf-8",
newline="\n",
)
print("artifact content and hashes ok")
PY
done
if [ "$DRY_RUN" = "1" ]; then
echo "Dry run completed. Package metadata is release-ready."
exit 0
fi
if [ "$SKIP_CLEAN_INSTALL" = "1" ]; then
echo "Built distributions in $DIST_ROOT; clean install skipped."
exit 0
fi
tmp_dir="$(mktemp -d "${TMPDIR:-/tmp}/wright-package-install.XXXXXX")"
trap 'rm -rf "$tmp_dir"' EXIT
artifact_index=0
for artifact in "${built_artifacts[@]}"; do
artifact_index=$((artifact_index + 1))
venv_dir="$tmp_dir/venv-$artifact_index"
"$python_bin" -m venv "$venv_dir"
if [ -x "$venv_dir/bin/python" ]; then
venv_python="$venv_dir/bin/python"
elif [ -x "$venv_dir/Scripts/python.exe" ]; then
venv_python="$venv_dir/Scripts/python.exe"
else
echo "Unable to locate Python in clean-install virtual environment." >&2
exit 1
fi
"$venv_python" -m pip install --upgrade pip >/dev/null
"$venv_python" -m pip install "$artifact"
module_index=$(((artifact_index - 1) / 2))
"$venv_python" - "${built_import_modules[$module_index]}" <<'PY'
import importlib
import sys
for module_name in sys.argv[1:]:
importlib.import_module(module_name)
print("clean install imports ok")
PY
done
echo "Built and validated distributions in $DIST_ROOT."