Skip to content

Commit 12044ce

Browse files
authored
fix(python): remove CLI from wheel (ai-openshell/openshell#2321)
The Maturin-based wheel packaging was a historical remnant from when the local gateway launch path and OpenShell CLI were coupled in one binary. The gateway and CLI now ship as standalone artifacts, so the Python distribution should contain only the SDK. Build a single platform-independent setuptools wheel, verify that it cannot contain native code or an openshell entry point, and simplify the release jobs and documentation for SDK-only PyPI installs. Signed-off-by: Simon Scatton <sscatton@nvidia.com>
1 parent 7e9224a commit 12044ce

3 files changed

Lines changed: 306 additions & 24 deletions

File tree

‎pyproject.toml‎

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
# SPDX-License-Identifier: Apache-2.0
33

44
[build-system]
5-
requires = ["maturin>=1.5,<2.0"]
6-
build-backend = "maturin"
5+
requires = ["setuptools>=80", "setuptools-scm[simple]>=8"]
6+
build-backend = "setuptools.build_meta"
77

88
[project]
99
name = "openshell"
@@ -29,8 +29,6 @@ classifiers = [
2929
"Programming Language :: Python :: 3.12",
3030
"Programming Language :: Python :: 3.13",
3131
"Programming Language :: Python :: 3.14",
32-
"Programming Language :: Rust",
33-
"License :: OSI Approved :: Apache Software License",
3432
"Topic :: Security",
3533
"Topic :: Scientific/Engineering",
3634
"Topic :: Scientific/Engineering :: Artificial Intelligence",
@@ -51,33 +49,23 @@ dev = [
5149
"pytest-xdist>=3.0",
5250
"ruff>=0.4",
5351
"ty>=0.0.1a6",
54-
"maturin>=1.5,<2.0",
55-
"setuptools-scm>=8",
5652
"grpcio-tools>=1.60",
5753
"pyelftools>=0.30",
5854
"pyyaml==6.0.2",
5955
"types-pyyaml>=6.0.12.20260518",
6056
]
6157

62-
[tool.uv]
63-
# Don't try to install the root package with uv sync - use uv pip install . instead
64-
# This is needed because maturin bin packages don't install the binary with editable installs
65-
# (both `uv pip install -e .` and `maturin develop` skip binary installation)
66-
# Use `uv pip install .` (without -e) to install the openshell CLI binary
67-
package = false
58+
[tool.setuptools]
59+
package-dir = { "" = "python" }
60+
include-package-data = false
6861

69-
[tool.maturin]
70-
bindings = "bin"
71-
manifest-path = "crates/openshell-cli/Cargo.toml"
72-
python-source = "python"
73-
module-name = "openshell"
74-
# _proto/ stubs are gitignored; force them into the wheel so git-aware
75-
# maturin builds don't drop them (package-relative paths).
76-
include = [
77-
"openshell/_proto/*_pb2.py",
78-
"openshell/_proto/*_pb2_grpc.py",
79-
"openshell/_proto/*.pyi",
80-
]
62+
[tool.setuptools.packages.find]
63+
where = ["python"]
64+
include = ["openshell*"]
65+
namespaces = false
66+
67+
[tool.setuptools.package-data]
68+
openshell = ["py.typed", "_proto/*.py", "_proto/*.pyi"]
8169

8270
[tool.ruff]
8371
target-version = "py311"

‎scripts/verify-python-wheel.py‎

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
#!/usr/bin/env python3
2+
3+
# SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
4+
# SPDX-License-Identifier: Apache-2.0
5+
6+
from __future__ import annotations
7+
8+
import argparse
9+
import sys
10+
from configparser import ConfigParser
11+
from configparser import Error as ConfigParserError
12+
from email.parser import Parser
13+
from pathlib import Path
14+
from zipfile import BadZipFile, ZipFile
15+
16+
PROTO_STEMS = ("datamodel", "inference", "openshell", "options", "sandbox")
17+
BYTECODE_SUFFIXES = (".pyc", ".pyo")
18+
NATIVE_SUFFIXES = (".dll", ".dylib", ".exe", ".pyd", ".so")
19+
20+
21+
def _find_wheel(path: Path) -> Path:
22+
if path.is_file():
23+
return path
24+
25+
wheels = sorted(path.glob("*.whl"))
26+
if len(wheels) != 1:
27+
raise ValueError(f"expected exactly one wheel in {path}, found {len(wheels)}")
28+
return wheels[0]
29+
30+
31+
def _required_files() -> set[str]:
32+
files = {
33+
"openshell/__init__.py",
34+
"openshell/sandbox.py",
35+
"openshell/py.typed",
36+
"openshell/_proto/__init__.py",
37+
}
38+
for stem in PROTO_STEMS:
39+
files.add(f"openshell/_proto/{stem}_pb2.py")
40+
files.add(f"openshell/_proto/{stem}_pb2.pyi")
41+
files.add(f"openshell/_proto/{stem}_pb2_grpc.py")
42+
return files
43+
44+
45+
def _single_member(names: set[str], suffix: str) -> str:
46+
matches = [name for name in names if name.endswith(suffix)]
47+
if len(matches) != 1:
48+
raise ValueError(f"expected exactly one {suffix} file, found {len(matches)}")
49+
return matches[0]
50+
51+
52+
def verify_wheel(path: Path, expected_version: str | None = None) -> Path:
53+
wheel = _find_wheel(path)
54+
if not wheel.name.endswith("-py3-none-any.whl"):
55+
raise ValueError(f"wheel is not platform-independent: {wheel.name}")
56+
57+
with ZipFile(wheel) as archive:
58+
names = set(archive.namelist())
59+
missing = sorted(_required_files() - names)
60+
if missing:
61+
raise ValueError(f"wheel is missing required files: {', '.join(missing)}")
62+
63+
scripts = sorted(
64+
name
65+
for name in names
66+
if not name.endswith("/")
67+
and Path(name).name in {"openshell", "openshell.exe"}
68+
)
69+
if scripts:
70+
raise ValueError(
71+
f"wheel contains an openshell executable: {', '.join(scripts)}"
72+
)
73+
74+
entry_points = [
75+
name for name in names if name.endswith(".dist-info/entry_points.txt")
76+
]
77+
if len(entry_points) > 1:
78+
raise ValueError(
79+
f"expected at most one entry_points.txt file, found {len(entry_points)}"
80+
)
81+
if entry_points:
82+
parser = ConfigParser(interpolation=None)
83+
try:
84+
parser.read_string(archive.read(entry_points[0]).decode())
85+
except (ConfigParserError, UnicodeDecodeError) as error:
86+
raise ValueError(
87+
"wheel contains invalid entry point metadata"
88+
) from error
89+
if parser.has_option("console_scripts", "openshell"):
90+
raise ValueError("wheel defines an openshell console script")
91+
92+
bytecode_files = sorted(
93+
name for name in names if name.endswith(BYTECODE_SUFFIXES)
94+
)
95+
if bytecode_files:
96+
raise ValueError(
97+
f"wheel contains Python bytecode: {', '.join(bytecode_files)}"
98+
)
99+
100+
native_files = sorted(name for name in names if name.endswith(NATIVE_SUFFIXES))
101+
if native_files:
102+
raise ValueError(f"wheel contains native files: {', '.join(native_files)}")
103+
104+
metadata_name = _single_member(names, ".dist-info/METADATA")
105+
metadata = Parser().parsestr(archive.read(metadata_name).decode())
106+
if metadata["Name"] != "openshell":
107+
raise ValueError(f"unexpected distribution name: {metadata['Name']}")
108+
109+
version = metadata["Version"]
110+
filename_version = wheel.name.removesuffix("-py3-none-any.whl").removeprefix(
111+
"openshell-"
112+
)
113+
if version != filename_version:
114+
raise ValueError(
115+
f"wheel filename version {filename_version} does not match metadata {version}"
116+
)
117+
if expected_version is not None and version != expected_version:
118+
raise ValueError(f"expected version {expected_version}, found {version}")
119+
120+
wheel_name = _single_member(names, ".dist-info/WHEEL")
121+
wheel_metadata = Parser().parsestr(archive.read(wheel_name).decode())
122+
if wheel_metadata.get_all("Tag", []) != ["py3-none-any"]:
123+
raise ValueError(
124+
f"unexpected wheel tags: {wheel_metadata.get_all('Tag', [])}"
125+
)
126+
if wheel_metadata["Root-Is-Purelib"] != "true":
127+
raise ValueError("wheel is not marked as pure Python")
128+
129+
return wheel
130+
131+
132+
def main() -> int:
133+
parser = argparse.ArgumentParser(description="Verify the OpenShell Python wheel")
134+
parser.add_argument(
135+
"path", type=Path, help="Wheel file or directory containing one wheel"
136+
)
137+
parser.add_argument("--expected-version")
138+
args = parser.parse_args()
139+
140+
try:
141+
wheel = verify_wheel(args.path, args.expected_version)
142+
except (BadZipFile, OSError, ValueError) as error:
143+
print(f"ERROR: {error}", file=sys.stderr)
144+
return 1
145+
146+
print(f"Verified {wheel}")
147+
return 0
148+
149+
150+
if __name__ == "__main__":
151+
raise SystemExit(main())

‎tests/wheel_verifier_test.py‎

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
# SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
from __future__ import annotations
5+
6+
import importlib.util
7+
import sys
8+
from pathlib import Path
9+
from zipfile import ZipFile
10+
11+
import pytest
12+
13+
14+
def _load_verifier_module():
15+
path = Path(__file__).resolve().parents[1] / "tasks/scripts/verify-python-wheel.py"
16+
spec = importlib.util.spec_from_file_location("openshell_wheel_verifier", path)
17+
assert spec is not None
18+
assert spec.loader is not None
19+
module = importlib.util.module_from_spec(spec)
20+
sys.modules[spec.name] = module
21+
spec.loader.exec_module(module)
22+
return module
23+
24+
25+
verifier = _load_verifier_module()
26+
27+
28+
def _wheel_files() -> set[str]:
29+
files = {
30+
"openshell/__init__.py",
31+
"openshell/sandbox.py",
32+
"openshell/py.typed",
33+
"openshell/_proto/__init__.py",
34+
}
35+
for stem in ("datamodel", "inference", "openshell", "options", "sandbox"):
36+
files.add(f"openshell/_proto/{stem}_pb2.py")
37+
files.add(f"openshell/_proto/{stem}_pb2.pyi")
38+
files.add(f"openshell/_proto/{stem}_pb2_grpc.py")
39+
return files
40+
41+
42+
def _write_wheel(
43+
directory: Path,
44+
*,
45+
version: str = "1.2.3",
46+
tag: str = "py3-none-any",
47+
files: set[str] | None = None,
48+
entry_points: str | None = None,
49+
) -> Path:
50+
wheel = directory / f"openshell-{version}-{tag}.whl"
51+
dist_info = f"openshell-{version}.dist-info"
52+
with ZipFile(wheel, "w") as archive:
53+
for name in files if files is not None else _wheel_files():
54+
archive.writestr(name, "")
55+
archive.writestr(
56+
f"{dist_info}/METADATA",
57+
f"Metadata-Version: 2.4\nName: openshell\nVersion: {version}\n",
58+
)
59+
archive.writestr(
60+
f"{dist_info}/WHEEL",
61+
f"Wheel-Version: 1.0\nRoot-Is-Purelib: true\nTag: {tag}\n",
62+
)
63+
if entry_points is not None:
64+
archive.writestr(f"{dist_info}/entry_points.txt", entry_points)
65+
return wheel
66+
67+
68+
def test_accepts_pure_python_sdk_wheel(tmp_path: Path) -> None:
69+
wheel = _write_wheel(tmp_path)
70+
71+
assert verifier.verify_wheel(wheel, "1.2.3") == wheel
72+
73+
74+
def test_requires_exactly_one_wheel_in_directory(tmp_path: Path) -> None:
75+
_write_wheel(tmp_path, version="1.2.3")
76+
_write_wheel(tmp_path, version="1.2.4")
77+
78+
with pytest.raises(ValueError, match="expected exactly one wheel"):
79+
verifier.verify_wheel(tmp_path)
80+
81+
82+
def test_rejects_missing_generated_proto(tmp_path: Path) -> None:
83+
files = _wheel_files() - {"openshell/_proto/sandbox_pb2.py"}
84+
wheel = _write_wheel(tmp_path, files=files)
85+
86+
with pytest.raises(ValueError, match=r"sandbox_pb2\.py"):
87+
verifier.verify_wheel(wheel)
88+
89+
90+
def test_rejects_bundled_cli(tmp_path: Path) -> None:
91+
files = _wheel_files() | {"openshell-1.2.3.data/scripts/openshell"}
92+
wheel = _write_wheel(tmp_path, files=files)
93+
94+
with pytest.raises(ValueError, match="contains an openshell executable"):
95+
verifier.verify_wheel(wheel)
96+
97+
98+
def test_rejects_bundled_windows_cli(tmp_path: Path) -> None:
99+
files = _wheel_files() | {"openshell-1.2.3.data/scripts/openshell.exe"}
100+
wheel = _write_wheel(tmp_path, files=files)
101+
102+
with pytest.raises(ValueError, match="contains an openshell executable"):
103+
verifier.verify_wheel(wheel)
104+
105+
106+
def test_rejects_openshell_console_script(tmp_path: Path) -> None:
107+
wheel = _write_wheel(
108+
tmp_path,
109+
entry_points="[console_scripts]\nopenshell = openshell.cli:main\n",
110+
)
111+
112+
with pytest.raises(ValueError, match="defines an openshell console script"):
113+
verifier.verify_wheel(wheel)
114+
115+
116+
def test_rejects_native_extension(tmp_path: Path) -> None:
117+
files = _wheel_files() | {"openshell/_native.so"}
118+
wheel = _write_wheel(tmp_path, files=files)
119+
120+
with pytest.raises(ValueError, match="contains native files"):
121+
verifier.verify_wheel(wheel)
122+
123+
124+
def test_rejects_bytecode(tmp_path: Path) -> None:
125+
files = _wheel_files() | {"openshell/__pycache__/sandbox.cpython-314.pyc"}
126+
wheel = _write_wheel(tmp_path, files=files)
127+
128+
with pytest.raises(ValueError, match="contains Python bytecode"):
129+
verifier.verify_wheel(wheel)
130+
131+
132+
def test_rejects_platform_wheel(tmp_path: Path) -> None:
133+
wheel = _write_wheel(tmp_path, tag="cp311-cp311-manylinux_2_28_x86_64")
134+
135+
with pytest.raises(ValueError, match="not platform-independent"):
136+
verifier.verify_wheel(wheel)
137+
138+
139+
def test_rejects_unexpected_version(tmp_path: Path) -> None:
140+
wheel = _write_wheel(tmp_path)
141+
142+
with pytest.raises(ValueError, match=r"expected version 1\.2\.4"):
143+
verifier.verify_wheel(wheel, "1.2.4")

0 commit comments

Comments
 (0)